【最短】自前サーバを使いmp3をEchoで再生する方法「AWS(Lambda)未使用」その2

スキルの作成

① alexa developer consoleのスキルページから「スキルの作成」ボタンを押下します。

② 新しいスキルを作成のページに遷移したら、以下を入力および選択して右上の「スキルを作成」ボタンを押下します。
・スキル名
 :作成するスキル名を入力します。
・1.スキルに追加するモデルを選択
 :「カスタム」を選択します。
・2.スキルのバックエンドリソースをホスティングする方法を選択
 :「Alexa-Hosted (Node.js)」を選択します。

③ スキルに追加するテンプレートを選択のページで「Hello Worldスキル」を選択し「選択」ボタンを押下します。

スキルの設定

スキルが作成されたら、スキルの設定を行います。
まず、インターフェースの設定で「Audio Player」の設定を有効にします。

有効にしたら上部にある、「インターフェースを保存」を押下します。

次に、このスキルの呼び出し名を設定します。
お好きな名前で呼び出してください。

スキルの呼び出し名を設定したら、「モデルを保存」「モデルをビルド」を順番に押下します。

スキルコードの修正

ビルドが終わったら、コードエディタの画面に遷移します。

10行目から16行目くらいにある以下の部分を修正します。

    handle(handlerInput) {
        const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }

この部分を以下に変更してください。
handle(handlerInput) {
の前に「async」を入れるのを忘れないでください。
また、「再生させたいファイルURLを指定する」の箇所を対象ファイルのURLに変更してください。

    async handle(handlerInput) {
        const url = "再生させたいファイルURLを指定する";
        const token = "mp3_token";
        return handlerInput.responseBuilder
            .addAudioPlayerPlayDirective('REPLACE_ALL', url, token, 0, null)
            .getResponse();
    }

コード修正したら、「保存」、「デプロイ」の順番にボタンを押下します。

開発中へのステータス変更

コードのデプロイまで終わったら、テスト画面に遷移して、ステータスを「開発中」に変更します。

Audio Playはalexa developer consoleではテストできない

Audio Playインターフェースを使用したテストは「alexa developer console」上ではできないので、実際のAmazon Echoに話しかけてテストしてみましょう。 「alexa developer console」 では以下のエラーメッセージが表示されます。

再生した音楽を停止できない??

「アレクサ 停止して。」とか「アレクサ 止めて。」と言ったら他のEchoアプリは停止してくれるのですが、現段階では止まってくれません。

もう一度、コードエディタに行き、

const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};

「CancelAndStopIntentHandler 」内の処理を以下に変更します。

const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PauseIntent');
    },
    handle(handlerInput) {
        return handlerInput.responseBuilder
            .addAudioPlayerStopDirective()
            .getResponse();
    }
};

① 「Alexa.getIntentName(handlerInput.requestEnvelope」を条件に追加して、「停止」や「ストップ」などのキーワードの際に呼ばれるインテントを追加します。
② addAudioPlayerStopDirective()でAudioPlayerの停止指示を行います。

終わりに

AWSを使用する記事は多くあったのですが、自前サーバがある場合にそちらを使用したい場合の方法が、分からなかったので記事にしてみました。

自前サーバを使いmp3をEchoで再生する方法「AWS(Lambda)未使用」の前準備です。