我正在尝试构建一个简单的苹果音乐网络应用播放器。我成功地得到了一个小例子。我可以根据“类型”和“ID”将专辑、歌曲等加载到音乐播放器队列中。我能够播放、暂停、跳到下一首曲目、上一首曲目等,但是由于某种原因,当一首歌曲完成并且下一首歌曲开始时(由 API 播放器队列自动处理)或者我单击跳到下一首曲目、暂停等它会做它应该做的事情,但它会弹出一个错误。
例如:我将专辑加载到队列中。点击播放。它开始播放音乐,但有时它会在开始播放时随机弹出一条错误消息并将错误打印到控制台。
这就是在开发控制台中显示的内容。“未处理的承诺拒绝:PLAY_ACTIVITY:在没有先前 play() 描述符的情况下调用了 play stop() 方法” https://imgur.com/5MCq08s
我试过让 js 在切换曲目之前暂停歌曲,但这并没有解决问题。“未处理的承诺拒绝:PLAY_ACTIVITY:在没有先前的 play() 描述符的情况下调用了 play stop() 方法”
document.addEventListener('musickitloaded', () => {
console.log("musickit loaded :D")
// MusicKit global is now defined
fetch('/token').then(response => response.json()).then(res => {
/***
Configure our MusicKit instance with the signed token from server, returns a configured MusicKit Instance
https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance
***/
const music = MusicKit.configure({
developerToken: res.token,
app: {
name: 'AppleMusicCS115',
build: '0.0.1'
},
declarativeMarkup: true
});
// setup click handlers
document.getElementById('add-to-q-btn').addEventListener('click', () => {
const idInput = document.getElementById('id-input');
const typeInput = document.getElementById('type-input');
/***
Add an item to the playback queue
https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992716-setqueue
***/
music.setQueue({
[typeInput.value]: idInput.value
});
// set fields back to blank fields
idInput.value = '';
typeInput.value = '';
});
document.getElementById('play-btn').addEventListener('click', () => {
/***
Resume or start playback of media item
https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992709-play
***/
music.authorize().then(function() {
music.play();
});// added then function
});
document.getElementById('pause-btn').addEventListener('click', () => {
/***
Pause playback of media item
https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992708-pause
***/
music.authorize().then(function() {
music.pause();
});// added then function
});
document.getElementById('next-trk-btn').addEventListener('click', () => {
/***
* Skip to next item in queue.
* https://developer.apple.com/documentation/musickitjs/musickit/player/2992772-skiptonextitem
*/
music.authorize().then(function() {
music.skipToNextItem();
});// added then function
});
document.getElementById('prev-trk-btn').addEventListener('click', () => {
/***
* https://developer.apple.com/documentation/musickitjs/musickit/player/2992773-skiptopreviousitem
*/
music.authorize().then(function() {
music.pause();;
});
music.authorize().then(function() {
music.skipToPreviousItem();;
});// added then function
})
</head>
<body>
<label>Type (eg 'album' 'song')</label>
<input id="type-input" type="text" value="album">
<br>
<label>id</label>
<input id="id-input" type="text" value="1462319084">
<br>
<button id="add-to-q-btn">Add to Queue</button>
<br>
<hr>
<button id="prev-trk-btn">Prev Track</button>
<button id="play-btn">Play</button>
<button id="pause-btn">Pause</button>
<button id="next-trk-btn">Next Track</button>
<button id="login-btn">Login to Apple Music</button>
<br>
我希望它可以在不显示任何弹出错误的情况下切换歌曲、播放、暂停等。也许以某种方式处理这些异常?控制台输出:
未处理的承诺拒绝:PLAY_ACTIVITY:在没有先前的 play() 描述符(匿名函数)的情况下调用了 play stop() 方法—musickit.js:1:14539 被拒绝—musickit.js:534 promiseReactionJob