question

ohDear avatar image
ohDear asked

Audioplayer directive and builtin play pause next directives are not working

To begin, please note I am use the hosted environment on alexa (node.js)


I am following this project and trying to make it work with a few changes:


Alexa Skill Sample: NodeJS-Audio-Player


I am not quite sure why, hoping it is a simple fix. I have managed to get the code to play audio. However it doesn't seem to be going to the next item in the play list or pausing or resuming or going next item using the inbuilt intents.


To give an example, I can see that it is not picking up any audioplayer started finished nearly finished playing etc.


Here is the code for audioplayer:


const AudioPlayerEventHandler = {
  canHandle(handlerInput) {
   // return handlerInput.requestEnvelope.request.type.startsWith('AudioPlayer.');
    Alexa.getRequestType(handlerInput.requestEnvelope).startsWith('AudioPlayer.')
  },
  async handle(handlerInput) {
  console.log('audio player event handler')
    const {
      requestEnvelope,
      attributesManager,
      responseBuilder
    } = handlerInput;
    const audioPlayerEventName = requestEnvelope.request.type.split('.')[1];
    const {
      playbackSetting,
      playbackInfo
    } = 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 'PlaybackFailed':
        playbackInfo.inPlaybackSession = false;
        console.log('Playback Failed : %j', handlerInput.requestEnvelope.request.error);
        return;
      default:
        throw new Error('Should never reach here!');
    }

    return responseBuilder.getResponse();
  },
};



here is the custom build:


exports.handler = Alexa.SkillBuilders.custom()
  .addRequestHandlers(
    CheckAudioInterfaceHandler,
    LaunchRequestHandler,
    HelpHandler,
    SystemExceptionHandler,
    SessionEndedRequestHandler,
    YesHandler,
    NoHandler,
    StartPlaybackHandler,
    NextPlaybackHandler,
    PreviousPlaybackHandler,
    PausePlaybackHandler,
    AudioPlayerEventHandler,
    LoopOnHandler,
    LoopOffHandler,
   // ShuffleOnHandler,
   // ShuffleOffHandler,
    StartOverHandler,
    ExitHandler
  )
  .addRequestInterceptors(LoadPersistentAttributesRequestInterceptor,RequestLog)
  .addResponseInterceptors(SavePersistentAttributesResponseInterceptor)
  .withPersistenceAdapter(
         new persistenceAdapter.S3PersistenceAdapter({bucketName:process.env.S3_PERSISTENCE_BUCKET})
     )
  .addErrorHandlers(ErrorHandler)
  //.withAutoCreateTable(true)
  //.withTableName(constants.skill.dynamoDBTableName)
  .lambda();


here is the request being sent:


{
    "version": "1.0",
    "context": {
        "AudioPlayer": {
            "offsetInMilliseconds": 0,
            "token": "0",
            "playerActivity": "PLAYING"
        },
        "System": {
            "application": {
                "applicationId": "amzn1.ask.skill...."
            },
            "user": {
                "userId": "amzn1.ask.account...."
            },
            "device": {
                "deviceId": "amzn1.ask.device....",
                "supportedInterfaces": {
                    "AudioPlayer": {},
                    "Geolocation": {}
                }
            },
            "apiEndpoint": "https://api.eu.amazonalexa.com",
            "apiAccessToken": "eyJ0eXAiOiJKV1QiL..."
        }
    },
    "request": {
        "type": "AudioPlayer.PlaybackStarted",
        "requestId": "amzn1.echo-api.request.a70a463d-1c75-4cd7-9965-9829398f1f4b",
        "timestamp": "2019-11-22T10:46:26Z",
        "locale": "en-GB",
        "token": "0",
        "offsetInMilliseconds": 0
    }
}


I have even logged the intent as an interceptor to see if the value being returned is true:


console.log(Alexa.getRequestType(handlerInput.requestEnvelope).startsWith('AudioPlayer.'))


returns

true


so Now I am lost cannot see why it isn't being picked up.


Package.json:


{
  "name": "hello-world",
  "version": "1.1.0",
  "description": "alexa utility for quickly building skills",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Amazon Alexa",
  "license": "ISC",
  "dependencies": {
    "ask-sdk-core": "^2.7.0",
    "ask-sdk-model": "^1.19.0",
    "aws-sdk": "^2.326.0",
    "ask-sdk-s3-persistence-adapter": "^2.0.0"
  }
}


Here is the error returned by system exception handler:

{
  "version": "1.0",
  "context": {
    "AudioPlayer": {
      "offsetInMilliseconds": 0,
      "token": "0",
      "playerActivity": "PLAYING"
    },
    "System": {
      "application": {
        "applicationId": "amzn1.ask.skill.1e685e32-e05a-4a94-81bb-07312f6b52ef"
      },
      "user": {
        "userId": "amzn1.ask.account.AHYLWDA.."
      },
      "device": {
        "deviceId": "amzn1.ask.device.AG42DMYFVV..",
        "supportedInterfaces": {
          "AudioPlayer": {
            
          },
          "Geolocation": {
            
          }
        }
      },
      "apiEndpoint": "https://api.eu.amazonalexa.com",
      "apiAccessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJS.."
    }
  },
  "request": {
    "type": "System.ExceptionEncountered",
    "requestId": "amzn1.echo-api.request.a065c437-8a62-4ea8-a9fa-cf4bf99fd680",
    "timestamp": "2019-11-22T10:36:53Z",
    "locale": "en-GB",
    "error": {
      "type": "INVALID_RESPONSE",
      "message": "The following directives are not supported: Response may not contain an outputSpeech,Response may not contain an reprompt,Response may not have shouldEndSession set to false"
    },
    "cause": {
      "requestId": "amzn1.echo-api.request.0f7909e9-e5de-4039-8425-2605952110ad"
    }
  }
}
intentsaudioplayerrequesthosted skillplaybackcontroller
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hubbs avatar image
Hubbs answered

Looks like you are not handling the `PlaybackNearlyFinished` audio player event. if you add that code back into your `AudioPlayerEventHandler` switch statement you might have better luck.

1 comment
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I do have it, just didn't complete the code here. The playback started should still work though as should playback finished.


The code is identical to the link in post. I believe it has something to do with the "startsWith()" because I created a dummy handler using === 'playbackstarted' and it worked.

0 Likes 0 ·
Anand@Amazon avatar image
Anand@Amazon answered

Hello there,

Sorry for late response !!

Can you share skill id for further debugging?

10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-9b01f442-b793-4c94-b215-1c20270124b7 avatar image
newuser-9b01f442-b793-4c94-b215-1c20270124b7 answered

I'm having the same issue. Were you able to fix this?

4 comments
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello and thank you for your message.

Can you please provide the skill ID so I can look further into this?

Regards,
Barry

0 Likes 0 ·

Hi, thank you for your response. Here's my skill id. By the way this skill is still not distributed.

amzn1.ask.skill.0d9511cc-2c3b-4c80-a834-853c583bc8b5

0 Likes 0 ·

I'm the author of this comment by the way. I forgot the account I used.

0 Likes 0 ·

Not sure if your issue is exactly the same but the original code shown above seems to be missing the "return" keyword in the canHandle() block. Can't see it working without that.

i.e. it should be:

return Alexa.getRequestType(handlerInput.requestEnvelope).startsWith('AudioPlayer.');
0 Likes 0 ·