我正在尝试构建一个简单的苹果音乐网络应用播放器。我成功地得到了一个工作的小例子。我可以根据“类型”和“ id”将专辑,歌曲等加载到音乐播放器队列中。我无法播放,暂停,跳到下一首曲目,上一首曲目等,但是由于某种原因,当一首歌结束并且下一首开始播放时(由API播放器队列自动处理),或者我单击跳到下一首曲目,暂停等它会做其应做的事情,但会弹出错误消息。
例如:我将相册加载到队列中。点击播放。它开始播放音乐,但是有时它会在开始播放后立即随机弹出一条错误消息,并将错误打印到控制台。
这是开发控制台中显示的内容。 “未处理的承诺拒绝:PLAY_ACTIVITY:在没有先前的play()描述符的情况下调用了play stop()方法” https://imgur.com/5MCq08s
我尝试让js在切换曲目之前暂停歌曲,但这不能解决问题。
“未处理的承诺拒绝:PLAY_ACTIVITY:在没有先前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 stop()方法而没有先前的play()描述符 (匿名函数)-musickit.js:1:14539 拒绝-musickit.js:534 promiseReactionJob