我正在制作音频播放器技能,在测试该技能时没有出现任何错误,但音频已播放,但是AudioPlayer请求(例如AudioPlayer.PlaybackStarted,AudioPlayer.PlabackStopped指令)不起作用。我已经使用下面的函数(与Github中的示例相同)在代码中处理了它们。
const AudioPlayerEventHandler = {
canHandle(handlerInput) {
return
handlerInput.requestEnvelope.request.type.startsWith('AudioPlayer.');
},
async handle(handlerInput) {
const {
requestEnvelope,
attributesManager,
responseBuilder
} = handlerInput;
const audioPlayerEventName = requestEnvelope.request.type.split('.')[1];
const {
playbackSetting,
playbackInfo,
audioData,
timeStamp
} = await attributesManager.getPersistentAttributes();
switch (audioPlayerEventName) {
case 'PlaybackStarted':
playbackInfo.token = getToken(handlerInput);
playbackInfo.index = await getIndex(handlerInput);
playbackInfo.inPlaybackSession = true;
playbackInfo.hasPreviousPlaybackSession = true;
break;
case 'PlaybackFinished':
playbackInfo.inPlaybackSession = false;
playbackInfo.hasPreviousPlaybackSession = false;
playbackInfo.nextStreamEnqueued = false;
break;
case 'PlaybackStopped':
playbackInfo.token = getToken(handlerInput);
playbackInfo.index = await getIndex(handlerInput);
playbackInfo.offsetInMilliseconds = getOffsetInMilliseconds(handlerInput);
break;
case 'PlaybackNearlyFinished':
{
if (playbackInfo.nextStreamEnqueued) {
break;
}
const enqueueIndex = (playbackInfo.index + 1) % audioData.length;
if (enqueueIndex === 0 && !playbackSetting.loop) {
break;
}
playbackInfo.nextStreamEnqueued = true;
const enqueueToken = playbackInfo.playOrder[enqueueIndex];
const playBehavior = 'ENQUEUE';
const podcast = audioData[playbackInfo.playOrder[enqueueIndex]];
const expectedPreviousToken = playbackInfo.token;
const offsetInMilliseconds = 0;
responseBuilder.addAudioPlayerPlayDirective(
playBehavior,
podcast.url,
enqueueToken,
offsetInMilliseconds,
expectedPreviousToken,
);
break;
}
case 'PlaybackFailed':
playbackInfo.inPlaybackSession = false;
console.log('Playback Failed : %j', handlerInput.requestEnvelope.request.error);
return;
default:
throw new Error('Should never reach here!');
}
return responseBuilder.speak('directive is sent').getResponse();
}, };