ฉันใช้ Ionic Framework กับ AngularJS และ ngCordova Media Plugin: http://ngcordova.com/docs/plugins/media/
ขณะนี้ฉันมีสองฟังก์ชันในคอนโทรลเลอร์ของฉัน
Play - ฟังก์ชั่นนี้เล่น src สื่อที่เลือก
$scope.play = function(src) { media = $cordovaMedia2.newMedia(src, null, null, mediaStatusCallback); media.play(); $scope.isPlaying = true; $ionicLoading.hide(); } }
เพลงถัดไป - ฟังก์ชั่นนี้จะรับ 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);
});
};
และในเทมเพลตของฉัน ปุ่มถัดไปเพียงเรียกใช้ฟังก์ชัน nextSong เช่นนั้นด้วย ID เพลงปัจจุบัน:
<div class="next" ng-click="nextSong('{{cursong.id}}')"></div>
ปัญหาที่ผมเจอคือเวลาผมกดปุ่มถัดไปครั้งแรกมันจะเล่นเพลงถัดไปแต่พอผมกดอีกครั้งมันก็เล่นเพลงเดิมอีกครั้ง ดูเหมือนว่าค่า 'currsong' จะไม่ได้รับการอัปเดต
นี่คือลักษณะของอาร์เรย์ 'allsongs':
["1631922", "1502059", "1365874", "1607940", "1251204", "1233296", "1151847", "1119737", "1086438", "1078140", "1068172", "1065312", "845339", "799161", "316293", "306073", "299817", "1603940"]
อัปเดต: ฉันลองใช้ช่วงเวลา
ฉันมีฟังก์ชันช่วงเวลาซึ่งจะข้ามไปยังเพลงถัดไปเมื่อถึงเวลาที่กำหนด ฉันเรียกใช้ฟังก์ชันเพลงถัดไปที่นั่น และทำงานได้อย่างสมบูรณ์แบบ
$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>