-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.ts
115 lines (100 loc) · 2.45 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import * as fs from 'tns-core-modules/file-system';
import { isString } from 'tns-core-modules/utils/types';
import { AudioPlayerOptions } from './options';
export class TNSPlayerUtil {
public static debug: boolean = false;
}
export const TNS_Player_Log = (...args) => {
if (TNSPlayerUtil.debug) {
console.log('NativeScript-Audio - TNSPlayer', args);
}
};
export interface TNSPlayerI {
/**
* native instance getters
*/
readonly ios?: any;
readonly android?: any;
/**
* Volume getter/setter
*/
volume: any;
/**
* Starts playing audio file from local app files.
*/
playFromFile(options: AudioPlayerOptions): Promise<any>;
/**
* Starts playing audio file from url
*/
playFromUrl(options: AudioPlayerOptions): Promise<any>;
/**
* Play audio file.
*/
play(): Promise<boolean>;
/**
* Pauses playing audio file.
*/
pause(): Promise<boolean>;
/**
* Seeks to specific time.
*/
seekTo(time: number): Promise<boolean>;
/**
* Releases resources from the audio player.
*/
dispose(): Promise<boolean>;
/**
* Check if the audio is actively playing.
*/
isAudioPlaying(): boolean;
/**
* Get the duration of the audio file playing.
*/
getAudioTrackDuration(): Promise<string>;
/**
* current time
*/
readonly currentTime: number;
}
/**
* Helper function to determine if string is a url.
* @param value [string]
*/
export function isStringUrl(value: string): boolean {
// check if artURL is a url or local file
let isURL = false;
if (value.indexOf('://') !== -1) {
if (value.indexOf('res://') === -1) {
isURL = true;
}
}
if (isURL === true) {
return true;
} else {
return false;
}
}
/**
* Will determine if a string is a url or a local path. If the string is a url it will return the url.
* If it is a local path, then the file-system module will return the file system path.
* @param path [string]
*/
export function resolveAudioFilePath(path: string) {
if (path) {
const isUrl = isStringUrl(path);
// if it's a url just return the audio file url
if (isUrl === true) {
return path;
} else {
let audioPath;
let fileName = isString(path) ? path.trim() : '';
if (fileName.indexOf('~/') === 0) {
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace('~/', ''));
audioPath = fileName;
} else {
audioPath = fileName;
}
return audioPath;
}
}
}