如何使用PhoneGap连续播放音频?

时间:2013-01-13 06:26:40

标签: javascript html5 cordova phonegap-plugins

我有几个Media文件可供使用。但他们必须一个接一个地玩。 像:

var play_media;
play_media= new Media("../record/01.mp3",Success,Error);
play_media.play();
//And as the `../record/01.mp3` media file is done. 
//I will release it and create another one to play. 
play_media= new Media("../record/02.mp3",Success,Error);

但似乎当一个媒体的播放完成后,它什么都不返回。
我该怎么办?

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

var media = {"../record/01.mp3","../record/02.mp3","../record/03.mp3"};
var next = 0, play_media;
for(int i=0; i<media.lenght; i++){
    play_media= new Media(media[next]);
    play_media.play();
    if(soundTimer == null){
        soundTimer = setInterval(function(){
            vpSound.getCurrentPosition(
                function(position){
                    if(position > -1){
                        //Do something if you want 
                    }
                }
            );
        }, 1000);
        next = next + 1;
    }
}

答案 1 :(得分:0)

首先使用media.getDuration找到音频剪辑长度: 并使用setTimeout函数播放下一首歌曲。 通过这种方式,您将能够连续播放多首歌曲

答案 2 :(得分:0)

使用Media.getCurrentPosition()和javascript setInterval并将其设置为每秒间隔(1000)以获取正在播放的曲目的位置。然后检查曲目的位置是否等于-0.001意味着它已经结束。从那里你可以移动到下一首曲目或如果它是最后一首曲目使用Media.release()

           var mediaTimer = setInterval(function () {
               media.getCurrentPosition(function (position) {
                     if (position > -0.001) {
                        //Do something like update a progress bar and set times.
                     }
                     else if (position === -0.001) {
                           selectedIndex = selectedIndex + 1; //selectedIndex is the index of the song chosen in a list.
                          if (selectedIndex > playlistLength) {
                              media.release();
                          } 
                          else {
                            var nextTrack = app.FolderList.tracks[selectedIndex]; //get the next track from a list of tracks using the selectedIndex
                            playPause(nextTrack.SongPath);//playPause() is used to initialize a new Media object with the new track and then plays it.
                          }
                     },
                     function () {
                     console.log('Unable to retrieve media position: ' + error.code);
                       });
                    },
                    1000);