Development Guidelines on Audio Playback
When to Use
You use audio playback APIs to convert audio data into audible analog signals, play the audio signals using output devices, and manage playback tasks.
Available APIs
Table 1 media
Table 2 AudioPlayer
-
Create an audio player.
var player = media.createAudioPlayer();
-
Set the listeners.
player.on('play', (err, action) => { if (err) { console.error('Error returned in the play() callback.'); return; } console.info('Current player duration: '+ player.duration); console.info('Current player time: ' + player.currentTime); console.info('Current player status: '+player.state); console.info('Pause MP3'); player.pause(); }); player.on('pause', (err, action) => { if (err) { console.error('Error returned in the pause() callback.'); return; } console.info('Current player status: ' + player.state); console.info('Current player time: ' + player.currentTime); player.seek(30000); // Seek for 30 seconds. }); player.on('stop', (err, action) => { if (err) { console.error('Error returned in the stop() callback.'); return; } console.info('stop callback invoked. State:' + player.state); player.reset(); }); player.on('dataLoad', (err, action) => { if (err) { console.error('Error returned in the dataLoad() callback.'); return; } console.info('dataLoad callback invoked. Current time: ' + player.currentTime); console.info('Duration of the source:' + player.duration); player.play(); }); player.on('reset', (err, action) => { if (err) { console.error('Error returned in the reset() callback.'); return; } console.info('reset callback invoked.'); player.release(); }); player.on('finish', (err, action) => { if (err) { console.error('Error returned in the finish() callback.'); return; } console.info('finish callback invoked.'); }); player.on('timeUpdate', (seekTime, action) => { console.info('Seek time: ' + seekTime); console.info('Current player time: ' + player.currentTime); var newTime = player.currentTime; if(newTime == 30000) { console.info('Seek succeeded. New time: ' + newTime); } else { console.error('Seek failed: ', + newTime); } player.stop(); }); player.on('error', (err) => { console.error('Player error: ${err.message}'); });
-
Start playback.
var audioSourceMp3 = 'file://test.mp3'; player.src = audioSourceMp3; player.loop = true;