audio-playback.md 4.7 KB
Newer Older
W
wusongqing 已提交
1
# Audio Playback Development
W
wusongqing 已提交
2

W
wusongqing 已提交
3
## When to Use
W
wusongqing 已提交
4

W
wusongqing 已提交
5
You use audio playback APIs to convert audio data into audible analog signals, play the audio signals using output devices, and manage playback tasks.
W
wusongqing 已提交
6

W
wusongqing 已提交
7 8
**Figure 1** Playback status
![](figures/playback-status.png "playback-status")
W
wusongqing 已提交
9 10


W
wusongqing 已提交
11
## Available APIs
W
wusongqing 已提交
12

W
wusongqing 已提交
13
**Table 1** APIs for audio playback 
W
wusongqing 已提交
14

W
wusongqing 已提交
15
| API| Description| 
W
wusongqing 已提交
16
| -------- | -------- |
W
wusongqing 已提交
17 18
| media.createAudioPlayer() | Creates an **AudioPlayer** instance.| 
| AudioPlayer | Provides audio playback features. For details, see the table below.| 
W
wusongqing 已提交
19

W
wusongqing 已提交
20
**Table 2** AudioPlayer methods
W
wusongqing 已提交
21

W
wusongqing 已提交
22
| Method| Description| 
W
wusongqing 已提交
23
| -------- | -------- |
W
wusongqing 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| release() | Releases audio resources.| 
| play() | Starts audio playback.| 
| pause() | Pauses playback.| 
| stop() | Stops playback.| 
| reset()<sup>7+</sup> | Resets the audio source to be played.| 
| setVolume(vol:&nbsp;number) | Sets playback volume.| 
| seek(timeMs:&nbsp;number) | Changes the playback position.| 
| src:string | Defines the URI of an audio file to play.| 
| state:AudioState | Defines the playback state.| 
| currentTime:number | Defines the current playback position.| 
| duration:number | Defines the playback duration. The value **-1** is returned if the data source does not support playback position change, for example, in the real-time streaming media scenario.| 
| loop:boolean | Defines whether to loop audio playback.| 
| on('play',&nbsp;function&nbsp;callback) | Subscribes to the playback start event.| 
| on('pause',&nbsp;function&nbsp;callback) | Subscribes to the playback pause event.| 
| on('stop',&nbsp;function&nbsp;callback) | Subscribes to the playback stop event.| 
| on('reset',&nbsp;function&nbsp;callback) | Subscribes to the playback reset event.| 
| on('finish',function&nbsp;callback) | Subscribes to the playback end event.| 
| on('error',&nbsp;function&nbsp;callback) | Subscribes to the playback error event.| 
| on('dataload',&nbsp;function&nbsp;callback) | Subscribes to the data loading event.| 
| on('volumeChange',&nbsp;function&nbsp;callback) | Subscribes to the volume change event.| 
| on('timeUpdate',&nbsp;function&nbsp;callback) | Subscribes to the progress change event.| 
W
wusongqing 已提交
45 46


W
wusongqing 已提交
47
1. Create an audio player.
W
wusongqing 已提交
48 49 50 51 52
   ```
   import media from '@ohos.multimedia.media';
   var player = media.createAudioPlayer();
   ```

W
wusongqing 已提交
53
2. Set the subscription events.
W
wusongqing 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
   ```
   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) => {
M
mamingshuai 已提交
67
        if (err) {
W
wusongqing 已提交
68
            console.error('Error returned in the pause() callback.');
M
mamingshuai 已提交
69 70
            return;
         }
W
wusongqing 已提交
71
         console.info('Current player status: ' + player.state);
Z
zengyawen 已提交
72
         console.info('Current player time: ' + player.currentTime);
W
wusongqing 已提交
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
         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) => {
M
mamingshuai 已提交
101
        if (err) {
W
wusongqing 已提交
102 103
           console.error('Error returned in the finish() callback.');
           return;
M
mamingshuai 已提交
104
        }
W
wusongqing 已提交
105 106 107 108 109 110 111 112 113 114
        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);
M
mamingshuai 已提交
115
        }
W
wusongqing 已提交
116 117 118 119 120 121 122
           player.stop();
   });
   player.on('error', (err) => {
        console.error('Player error: ${err.message}');
   });
   ```

W
wusongqing 已提交
123
3. Start playback.
W
wusongqing 已提交
124 125 126 127 128
   ```
   var audioSourceMp3 = 'file://test.mp3';
   player.src = audioSourceMp3;
   player.loop = true;
   ```