【问题标题】:How can I play the audio successively with PhoneGap?如何使用 PhoneGap 连续播放音频?
【发布时间】:2013-01-13 06:26:40
【问题描述】:

我有几个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);

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

【问题讨论】:

  • mediaSuccess:(可选)媒体对象完成当前播放/录制或停止操作后调用的回调。这可以触发播放下一首歌曲跨度>
  • 你的意思是 Media(,,,"mediaSuccess") 吗?我会研究它。谢谢。

标签: javascript html cordova phonegap-plugins


【解决方案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;
    }
}

【讨论】:

    【解决方案2】:

    首先用 media.getDuration 找到音频片段的长度: 并使用 setTimeout 函数播放下一首歌曲。 这样你就可以连续播放多首歌曲了

    【讨论】:

      【解决方案3】:

      使用 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-24
        相关资源
        最近更新 更多