AngularJS Cordova Media ไม่เล่นเพลงถัดไปเสมอไป

ฉันใช้ Ionic Framework กับ AngularJS และ ngCordova Media Plugin: http://ngcordova.com/docs/plugins/media/

ขณะนี้ฉันมีสองฟังก์ชันในคอนโทรลเลอร์ของฉัน

  1. Play - ฟังก์ชั่นนี้เล่น src สื่อที่เลือก

      $scope.play = function(src) {
            media = $cordovaMedia2.newMedia(src, null, null, mediaStatusCallback);
            media.play(); 
            $scope.isPlaying = true;
            $ionicLoading.hide();
    
        }
      }
    
  2. เพลงถัดไป - ฟังก์ชั่นนี้จะรับ 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>

person aqq    schedule 14.11.2015    source แหล่งที่มา


คำตอบ (2)


ปัญหาไม่ได้อยู่ที่ Cordova หรือ Media Plugin เมื่อคุณเล่นเพลง คุณต้องอัปเดต $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 ไว้ที่ไหนสักแห่งในสคริปต์ของคุณ

person Alan Rezende    schedule 14.11.2015
comment
ซึ่งดำเนินการในฟังก์ชัน playMusic $scope.cursong ได้รับการอัปเดตด้วยข้อมูลเกี่ยวกับเพลง รวมถึง id และ src โปรดดูคำถามที่อัปเดตพร้อมรหัสสำหรับ playMusic - person aqq; 14.11.2015
comment
คุณช่วยแบ่งปันตัวอย่างการส่งคืน data.results ได้ไหม - person Alan Rezende; 14.11.2015
comment
วัตถุ { id: 1251204 สถานะ: เสร็จสมบูรณ์ stream_only: ใช่} Streaming_url: cloudfrontlink - person aqq; 14.11.2015
comment
ปัญหาที่ผมเจอคือเวลาผมกดปุ่มถัดไปครั้งแรกมันจะเล่นเพลงถัดไปแต่พอผมกดอีกครั้งมันก็เล่นเพลงเดิมอีกครั้ง ดูเหมือนว่าค่า 'currsong' จะไม่ได้รับการอัปเดตหลังจากครั้งที่สอง - person aqq; 14.11.2015
comment
ลองใช้บรรทัดนี้ในฟังก์ชัน nextSong var index = $scope.allsongs.indexOf(String(song_id)); - person Alan Rezende; 14.11.2015
comment
song_id เป็นสตริงอยู่แล้ว ปัญหาเดียวกันกับบรรทัดนั้น - เล่นเพลงถัดไปได้ดีในครั้งแรก จากนั้นเมื่อกดปุ่มเพลงถัดไปอีกครั้ง เพลงจะเล่นเพลงถัดไปแบบเดิม - person aqq; 14.11.2015
comment
โปรดดูคำถามที่อัปเดต การเรียก nextSong ทำงานได้ดีในระหว่างฟังก์ชันช่วงเวลา แต่ด้วยปุ่มเทมเพลต ก็ยังไม่มีโชค - person aqq; 15.11.2015

ฉันได้แก้ไขปัญหานี้แล้วโดยการสร้างฟังก์ชันใหม่ '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>
person aqq    schedule 15.11.2015