import { TNSPlayer } from 'nativescript-audio-player';
export class YourClass {
private _player: TNSPlayer;
constructor() {
this._player = new TNSPlayer();
this._player.debug = true;
this._player
.initFromFile({
audioFile: '~/audio/song.mp3',
loop: false,
completeCallback: this._trackComplete.bind(this),
errorCallback: this._trackError.bind(this)
})
.then(() => {
this._player.getAudioTrackDuration().then(duration => {
console.log(`song duration:`, duration);
});
});
}
public togglePlay() {
if (this._player.isAudioPlaying()) {
this._player.pause();
} else {
this._player.play();
}
}
private _trackComplete(args: any) {
console.log('reference back to player:', args.player);
console.log('whether song play completed successfully:', args.flag);
}
private _trackError(args: any) {
console.log('reference back to player:', args.player);
console.log('the error:', args.error);
console.log('extra info on the error:', args.extra);
}
}