Skip to content

Instantly share code, notes, and snippets.

@ochilab
Created March 19, 2024 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/71fc7dfeb30693b65f0f33cc06c0f22f to your computer and use it in GitHub Desktop.
Save ochilab/71fc7dfeb30693b65f0f33cc06c0f22f to your computer and use it in GitHub Desktop.
サウンドを鳴らすだけのAlexaSkill
const playlist_title = '授業チャイム'; // ファイル名・スキル名とは別に表示させたいテキスト(アーティストなど)
const playlist_track = 'SchoolChime2.mp3'; // 再生する音楽ファイル名
const token_prefix = 'mymusic001'; // 画像などを変更したとき、この識別子を変更するとすぐに反映される
const Util = require('util.js');
const Alexa = require('ask-sdk-core');
exports.handler = Alexa.SkillBuilders.custom().addRequestHandlers({
canHandle(input) {
// すべてのリクエストをハンドルできるかの判定
return true;
},
handle(input) {
const meta = {
title: playlist_title
//art: {}, // カバーアートなどの追加が必要な場合はここに情報を記載
//backgroundImage: {} // 背景画像などの追加が必要な場合はここに情報を記載
};
// トークンとオフセットの設定
const token = token_prefix + ":::0";
const offset = 0; // 再生開始位置を0ミリ秒(最初から)に設定
// 再生リクエストの送信
switch(input.requestEnvelope.request.type) {
case 'IntentRequest':
// 特定のインテントまたは起動リクエストに対する処理
switch(input.requestEnvelope.request.intent.name) {
case 'AMAZON.CancelIntent':
case 'AMAZON.StopIntent':
case 'AMAZON.PauseIntent':
return input.responseBuilder
.addAudioPlayerStopDirective()
.withShouldEndSession(true)
.getResponse();
}
break;
case 'AudioPlayer.PlaybackNearlyFinished':
// 再生が終わりかけたとき、何もしない(これ以上再生を続けない)
return input.responseBuilder.withShouldEndSession(true).getResponse();
}
return input.responseBuilder
.addAudioPlayerPlayDirective(
'REPLACE_ALL',
Util.getS3PreSignedUrl('Media/' + playlist_track),
token,
offset,
undefined, // expectedPreviousToken は再生キューを利用しないので未指定
meta
)
.withShouldEndSession(true)
.getResponse();
}
}).addErrorHandlers({
canHandle() {
return true;
},
handle(input, error) {
console.log(`Error handled: ${error.message}`);
return input.responseBuilder
.speak('エラーが発生しました。後ほど再試行してください。')
.withShouldEndSession(true)
.getResponse();
}
}).lambda();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment