blob: 1a1fda81b600d9c1f72179ea8157dab271124113 [file] [log] [blame]
Narayan Kamath8d1fc242011-06-03 18:11:54 +01001/*
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 */
16package android.speech.tts;
17
Narayan Kamathaf802c62011-12-05 11:20:07 +000018import android.content.Context;
19import android.media.MediaPlayer;
20import android.net.Uri;
21import android.os.ConditionVariable;
Narayan Kamath754c72e2011-11-09 14:22:32 +000022import android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher;
Narayan Kamath67ae6bc2011-11-30 14:51:00 +000023import android.util.Log;
Narayan Kamath8d1fc242011-06-03 18:11:54 +010024
Narayan Kamath67ae6bc2011-11-30 14:51:00 +000025class AudioPlaybackQueueItem extends PlaybackQueueItem {
Narayan Kamathaf802c62011-12-05 11:20:07 +000026 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 Kamath8d1fc242011-06-03 18:11:54 +010035
Narayan Kamath67ae6bc2011-11-30 14:51:00 +000036 AudioPlaybackQueueItem(UtteranceProgressDispatcher dispatcher,
Narayan Kamathaf802c62011-12-05 11:20:07 +000037 Object callerIdentity,
38 Context context, Uri uri, int streamType) {
Narayan Kamath492b7f02011-11-29 17:02:06 +000039 super(dispatcher, callerIdentity);
Narayan Kamathaf802c62011-12-05 11:20:07 +000040
41 mContext = context;
42 mUri = uri;
43 mStreamType = streamType;
44
45 mDone = new ConditionVariable();
46 mPlayer = null;
47 mFinished = false;
Narayan Kamath8d1fc242011-06-03 18:11:54 +010048 }
Narayan Kamath67ae6bc2011-11-30 14:51:00 +000049 @Override
50 public void run() {
Narayan Kamathaf802c62011-12-05 11:20:07 +000051 final UtteranceProgressDispatcher dispatcher = getDispatcher();
52
53 dispatcher.dispatchOnStart();
54 mPlayer = MediaPlayer.create(mContext, mUri);
55 if (mPlayer == null) {
56 dispatcher.dispatchOnError();
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) {
86 dispatcher.dispatchOnDone();
87 } else {
88 dispatcher.dispatchOnError();
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 Kamath8d1fc242011-06-03 18:11:54 +010099 }
100
101 @Override
Narayan Kamath67ae6bc2011-11-30 14:51:00 +0000102 void stop(boolean isError) {
Narayan Kamathaf802c62011-12-05 11:20:07 +0000103 mDone.open();
Narayan Kamath8d1fc242011-06-03 18:11:54 +0100104 }
Narayan Kamath8d1fc242011-06-03 18:11:54 +0100105}