我有一個活動會創建一個「Audio」類,並嘗試使用android Text to Speech API讀取一些文本。如果不支持該語言,則會嘗試使用MediaPlayer從服務器播放自定義mp3文件。最後,如果MediaPlayer的失敗,它採用Nuance的SpeechKit文本閱讀:銷燬Nuance會話
我的問題是,當我破壞活動,我要摧毀/停止Nuance的聲音太,我不知道如何關閉Nuance音頻。
Activity類
private Audio audio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
audio = new Audio(this).play("my text to read");
}
@Override
protected void onPause() {
audio.pause();
super.onPause();
}
@Override
protected void onDestroy() {
audio.destroy();
super.onDestroy();
}
音頻類
private TextToSpeech tts;
private MediaPlayer player;
private Session session;
public void play(String text) {
// check if supported
if (supported) tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
else mediaPlayer(text);
}
private void mediaPlayer(String text) {
// make some queries on server to find the file url
if (queryFoundFile) {
player = new MediaPlayer();
player.setDataSource(myFileUrl);
player.setAudioStreamType(3);
player.prepare();
player.start();
} else nuancePlayer(text);
}
private void nuancePlayer(String text) {
Transaction.Options options = new Transaction.Options();
options.setLanguage(new Language("eng-USA"));
session = Session.Factory.session(activity, myServer, appKey);
session.speakString(text, options, new Transaction.Listener() {
@Override
public void onError(Transaction transaction, String s, TransactionException e) {
e.printStackTrace()
}
});
// it reaches here and nuance plays the audio
}
// these are the methods I call when the activity is paused or destroyed
public void pause() {
if (tts != null) tts.stop();
if (player != null) player.stop();
if (nuance != null) nuance.getAudioPlayer().stop(); // don't work
}
public void destroy() {
if (tts != null) tts.shutdown();
if (player != null) player.release();
if (nuance != null) nuance.getAudioPlayer().stop(); // don't work
}
如果我使用文本到語音或MediaPlayer的,如果我毀掉我的活動,音頻立即銷燬。但是,如果是Nuance播放,我似乎無法破壞音頻。它只是繼續說話。
我做了一些調試,並調用了pause()和destroy()方法。此外,nuance.getAudioPlayer不爲null,並且是AudioPlayer播放。當我打電話給他的方法stop()時,我無法找到他沒有停下來的原因。
什麼是Nuance?
這是我第一次使用Nuance,所以我沒有那種經驗。基本上我認爲它是Android文本到語音的替代品。
爲什麼我有這個在我的項目?
我的項目有4種主要語言,我需要有文本到語音功能來閱讀一些文本。問題是,android text to speech不支持Nuance支持的這些語言。
爲什麼Nuance是我最後的選擇?
因爲Nuance有成本。我嘗試使用android TTS或MediaPlayer。只有這兩個失敗,我使用Nuance。這是閱讀我的文本的最後手段!
如果您有一段時間,我會很樂意聽到'Nuance'。對於你使用什麼以及這有幫助嗎?我沒有任何線索 -__- 。喜歡聽到它:)也許你也可以得到一個+1 –
我編輯了一些問題,我現在可以回答!希望有所幫助!謝謝! – Ravers
很酷感謝您的支持,即使您處於問題狀態,也能真正感謝您+1 –