在我們?nèi)粘I钪?一些常用的場景例如銀行操作機(jī),人臉識別通行機(jī)等仍劈,每次進(jìn)行一次操作厕倍,則機(jī)器回應(yīng)一點(diǎn)聲音。像人臉識別這種響應(yīng)聲音可能會(huì)更頻繁一點(diǎn)贩疙。
如果您適應(yīng)以下情景讹弯,那么您可以繼續(xù)往下看了:
- 需要多種不同類型聲音排隊(duì)播放
- 需要支持中斷隊(duì)列某種類型聲音,支持立即中斷或者播放完當(dāng)前語音才中斷
- 需要某種類型聲音一直播放
- 需要使用SoundPool這種反應(yīng)比較快的这溅,并且能根據(jù)時(shí)長結(jié)束進(jìn)行下一步動(dòng)作
源碼下載:
https://github.com/zhongjhATC/sound/tree/master
// 初始化幾種類型語音
MediaPlayerQueue mediaPlayerQueue = new MediaPlayerQueue();
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.PASS, loadSound("pass.mp3"));
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.UNAUTHORIZED, loadSound("no_auth.mp3"));
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.NO_MASK, loadSound("no_mask.mp3"));
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.NORMAL_TEMPERATURE, loadSound("temp_normal.mp3"));
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.ABNORMAL_TEMPERATURE, loadSound("temp_exception.mp3"));
mediaPlayerQueue.addSoundPoolPlayer(VoicePromptType.NEAR_MEASURE, loadSound("near_measure_temperature.mp3"));
// 設(shè)置請靠近測溫允許重復(fù)播放
ArrayList<Integer> repetitions = new ArrayList<>();
repetitions.add(VoicePromptType.NEAR_MEASURE);
mediaPlayerQueue.setRepetitions(repetitions);
// 播放請靠近測溫5次
findViewById(R.id.button1).setOnClickListener(v -> {
for (int i = 0; i < 5; i++) {
mediaPlayerQueue.play(VoicePromptType.NEAR_MEASURE);
}
});
// 播放溫度正常组民,播放pass
findViewById(R.id.button2).setOnClickListener(v -> {
mediaPlayerQueue.play(VoicePromptType.NORMAL_TEMPERATURE);
mediaPlayerQueue.play(VoicePromptType.PASS);
});
// 立即停止所有
findViewById(R.id.button3).setOnClickListener(v -> mediaPlayerQueue.clearAll(true));
// 停止所有(除了當(dāng)前正在播放的)
findViewById(R.id.button4).setOnClickListener(v -> mediaPlayerQueue.clearAll(false));
// 立即停止所有 靠近測溫的
findViewById(R.id.button5).setOnClickListener(v -> mediaPlayerQueue.clearAllSpecifyType(VoicePromptType.NEAR_MEASURE));