AngularJS Cordova Media不;不要总是播放下一首歌
AngularJS Cordova Media Doesn't Always Play Next Song
我正在使用带有AngularJS和ngCordova媒体插件的Ionic框架:http://ngcordova.com/docs/plugins/media/
我的控制器目前有两个功能;
-
播放-此功能播放所选媒体src
$scope.play = function(src) { media = $cordovaMedia2.newMedia(src, null, null, mediaStatusCallback); media.play(); $scope.isPlaying = true; $ionicLoading.hide(); } }
-
Next Song-此函数通过查找当前歌曲对象的索引并选择列表中的下一个对象来获取列表中下一首歌曲的src。
$scope.nextSong = function (song_id) { var index = $scope.allsongs.indexOf(song_id); if(index >= 0 && index < $scope.allsongs.length - 1) { $scope.nextsongid = $scope.allsongs[index + 1] $scope.playMusic($scope.nextsongid); //the playMusic function just looks up the src of the song and parses to the play function } }
这是上面调用的playMusic功能:
$scope.playMusic = function(music_id)
{
myService.getSongInfo(music_id)
.then(function(data){
$scope.cursong = data.results;
$scope.play($scope.cursong.src);
});
};
在我的模板中,下一个按钮只是用当前歌曲ID:调用nextSong函数
<div class="next" ng-click="nextSong('{{cursong.id}}')"></div>
我面临的问题是,当我第一次按下下一个按钮时,它会播放下一首歌,但当我再次按下它时,它又会播放同一首歌。"currssong"值似乎没有更新。
这就是"allsongs"数组的样子:
["1631922", "1502059", "1365874", "1607940", "1251204", "1233296", "1151847", "1119737", "1086438", "1078140", "1068172", "1065312", "845339", "799161", "316293", "306073", "299817", "1603940"]
更新:我尝试使用间隔
我有一个音程函数,当时间达到持续时间时,它会跳到下一首歌。我在那里调用nextsong函数,它工作得很好。
$scope.Timer = $interval(function () {
$scope.cursongduration = $scope.cursong.duration;
$scope.cursongid = $scope.cursong.id;
if($scope.currduration >= $scope.cursongduration){
$scope.nextSong($scope.cursongid);
}, 1000);
};
所以我的问题是,为什么通过模板按钮调用nextSong也有效?在我看来,由于某种奇怪的原因,cursong.id在模板中1次后似乎没有更新。我也尝试过使用cursongid变量,它设置在模板按钮的间隔中,但没有成功:
<div class="next" ng-click="nextSong('{{cursongid}}')"></div>
问题不在cordova或媒体插件中,当你播放歌曲时,你必须更新$scope.cursong.id
,这样下次点击就能知道当前歌曲是什么。
$scope.play = function(src) {
$scope.cursong.id = src;
media = $cordovaMedia2.newMedia(src, null, null, mediaStatusCallback);
media.play();
$scope.isPlaying = true;
$ionicLoading.hide();
}
}
我假设您已经在脚本中的某个位置声明了$scope.cursong.id
。
我通过创建一个新函数"PlayNextSong"解决了这个问题,该函数将$scope.cursong.id存储到一个新的作用域"cursongid"中,然后调用nextSong函数。这对我来说效果很好。
功能:
$scope.playNextSong = function() {
$scope.cursongid = $scope.cursong.id;
$scope.nextSong($scope.cursongid);
}
按钮:
<div class="next" ng-click="playNextSong()"></div>
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- Javascript返回值只在循环中返回一次
- Last.fm API发送2个曲目时,我要求一个,如果我正在听一首歌
- HTML5<音频>:点击下一首歌曲时停止加载/缓冲
- 制作一个html5音频播放列表,自动播放一首又一首歌曲
- 带有jQuery的音频HTML5播放下一首曲目
- 对文本区域段落的每一首行应用缩进
- 获取 timeOut() 以每秒从数组中播放一首歌曲
- 我想在歌曲结束后在默认音频播放器中播放一首流行音乐,任何人都可以帮我编写代码
- 音频播放器-一次只播放一首曲目,不起作用
- 通过Spotify App API从队列中获取下一首曲目
- AngularJS Cordova Media不;不要总是播放下一首歌
- 在我的网页上嵌入不止一首歌曲
- 如何通过Soundcloud API重复一首歌
- 音频自动播放下一首歌曲时,上一首完成
- Soundcloud Flash小部件,试图在页面上播放下一首歌曲
- 如何在加载时在中间的jPlayer中开始一首歌
- Web Audio API附加/连接不同的audiobuffer并将它们作为一首歌曲播放
- 我怎么知道一首歌曲是否已经结束或下一个按钮被点击与spotify API