Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | package android.speech.tts; |
| 17 | |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.media.MediaPlayer; |
| 20 | import android.net.Uri; |
| 21 | import android.os.ConditionVariable; |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 22 | import android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher; |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 23 | import android.util.Log; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 24 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 25 | class AudioPlaybackQueueItem extends PlaybackQueueItem { |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 26 | private static final String TAG = "TTS.AudioQueueItem"; |
| 27 | |
| 28 | private final Context mContext; |
| 29 | private final Uri mUri; |
| 30 | private final int mStreamType; |
| 31 | |
| 32 | private final ConditionVariable mDone; |
| 33 | private MediaPlayer mPlayer; |
| 34 | private volatile boolean mFinished; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 35 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 36 | AudioPlaybackQueueItem(UtteranceProgressDispatcher dispatcher, |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 37 | Object callerIdentity, |
| 38 | Context context, Uri uri, int streamType) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 39 | super(dispatcher, callerIdentity); |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 40 | |
| 41 | mContext = context; |
| 42 | mUri = uri; |
| 43 | mStreamType = streamType; |
| 44 | |
| 45 | mDone = new ConditionVariable(); |
| 46 | mPlayer = null; |
| 47 | mFinished = false; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 48 | } |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 49 | @Override |
| 50 | public void run() { |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 51 | final UtteranceProgressDispatcher dispatcher = getDispatcher(); |
| 52 | |
| 53 | dispatcher.dispatchOnStart(); |
| 54 | mPlayer = MediaPlayer.create(mContext, mUri); |
| 55 | if (mPlayer == null) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 56 | dispatcher.dispatchOnError(TextToSpeechClient.Status.ERROR_OUTPUT); |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
| 62 | @Override |
| 63 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 64 | Log.w(TAG, "Audio playback error: " + what + ", " + extra); |
| 65 | mDone.open(); |
| 66 | return true; |
| 67 | } |
| 68 | }); |
| 69 | mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { |
| 70 | @Override |
| 71 | public void onCompletion(MediaPlayer mp) { |
| 72 | mFinished = true; |
| 73 | mDone.open(); |
| 74 | } |
| 75 | }); |
| 76 | mPlayer.setAudioStreamType(mStreamType); |
| 77 | mPlayer.start(); |
| 78 | mDone.block(); |
| 79 | finish(); |
| 80 | } catch (IllegalArgumentException ex) { |
| 81 | Log.w(TAG, "MediaPlayer failed", ex); |
| 82 | mDone.open(); |
| 83 | } |
| 84 | |
| 85 | if (mFinished) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 86 | dispatcher.dispatchOnSuccess(); |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 87 | } else { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 88 | dispatcher.dispatchOnStop(); |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | private void finish() { |
| 93 | try { |
| 94 | mPlayer.stop(); |
| 95 | } catch (IllegalStateException ex) { |
| 96 | // Do nothing, the player is already stopped |
| 97 | } |
| 98 | mPlayer.release(); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 102 | void stop(int errorCode) { |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 103 | mDone.open(); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 104 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 105 | } |