ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidsystemimpl/player/MediaPlayerImpl.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.androidsystemimpl.player; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.app.Service; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.content.res.AssetFileDescriptor; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.content.res.AssetManager; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.media.AudioManager; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.media.MediaPlayer; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.telephony.PhoneStateListener; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import android.telephony.TelephonyManager; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import android.text.TextUtils; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | import com.baidu.duer.dcs.androidapp.DcsSampleApplication; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import com.baidu.duer.dcs.systeminterface.IMediaPlayer; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 30 | |
| TMBOY | 45:2aa9f933c8d2 | 31 | import org.json.JSONException; |
| TMBOY | 45:2aa9f933c8d2 | 32 | import org.json.JSONObject; |
| TMBOY | 45:2aa9f933c8d2 | 33 | |
| TMBOY | 45:2aa9f933c8d2 | 34 | import java.io.File; |
| TMBOY | 45:2aa9f933c8d2 | 35 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 36 | import java.io.InputStream; |
| TMBOY | 45:2aa9f933c8d2 | 37 | import java.util.Collections; |
| TMBOY | 45:2aa9f933c8d2 | 38 | import java.util.LinkedList; |
| TMBOY | 45:2aa9f933c8d2 | 39 | import java.util.List; |
| TMBOY | 45:2aa9f933c8d2 | 40 | |
| TMBOY | 45:2aa9f933c8d2 | 41 | /** |
| TMBOY | 45:2aa9f933c8d2 | 42 | * android MediaPlayer -android平台中实现音频的播放逻辑 |
| TMBOY | 45:2aa9f933c8d2 | 43 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 44 | * Created by guxiuzhong@baidu.com on 2017/5/31. |
| TMBOY | 45:2aa9f933c8d2 | 45 | */ |
| TMBOY | 45:2aa9f933c8d2 | 46 | public class MediaPlayerImpl implements IMediaPlayer { |
| TMBOY | 45:2aa9f933c8d2 | 47 | private static final String TAG = MediaPlayerImpl.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 48 | public static final String ASSERT_PREFIX = "assets://"; |
| TMBOY | 45:2aa9f933c8d2 | 49 | private static final String KEY_SP_VOLUME = "currentVolume"; |
| TMBOY | 45:2aa9f933c8d2 | 50 | private static final String KEY_SP_MUTE = "isMute"; |
| TMBOY | 45:2aa9f933c8d2 | 51 | private IMediaPlayer.PlayState mCurrentState = IMediaPlayer.PlayState.IDLE; |
| TMBOY | 45:2aa9f933c8d2 | 52 | private MediaPlayer mMediaPlayer; |
| TMBOY | 45:2aa9f933c8d2 | 53 | private float currentVolume = 0.8f; // 默认音量80% |
| TMBOY | 45:2aa9f933c8d2 | 54 | private boolean isMute; |
| TMBOY | 45:2aa9f933c8d2 | 55 | private boolean isError38; |
| TMBOY | 45:2aa9f933c8d2 | 56 | private float currentPercent; |
| TMBOY | 45:2aa9f933c8d2 | 57 | private int currentSeekMilliseconds; |
| TMBOY | 45:2aa9f933c8d2 | 58 | private IAudioStreamStore audioStreamStore; // stream流数据保存 |
| TMBOY | 45:2aa9f933c8d2 | 59 | private List<IMediaPlayer.IMediaPlayerListener> mediaPlayerListeners; |
| TMBOY | 45:2aa9f933c8d2 | 60 | private boolean isActive; |
| TMBOY | 45:2aa9f933c8d2 | 61 | private TelephonyManager telephonyManager; |
| TMBOY | 45:2aa9f933c8d2 | 62 | |
| TMBOY | 45:2aa9f933c8d2 | 63 | public MediaPlayerImpl() { |
| TMBOY | 45:2aa9f933c8d2 | 64 | mMediaPlayer = new MediaPlayer(); |
| TMBOY | 45:2aa9f933c8d2 | 65 | // set audio stream type |
| TMBOY | 45:2aa9f933c8d2 | 66 | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); |
| TMBOY | 45:2aa9f933c8d2 | 67 | mMediaPlayer.setOnBufferingUpdateListener(bufferingUpdateListener); |
| TMBOY | 45:2aa9f933c8d2 | 68 | mMediaPlayer.setOnErrorListener(errorListener); |
| TMBOY | 45:2aa9f933c8d2 | 69 | mMediaPlayer.setOnPreparedListener(preparedListener); |
| TMBOY | 45:2aa9f933c8d2 | 70 | mMediaPlayer.setOnCompletionListener(completionListener); |
| TMBOY | 45:2aa9f933c8d2 | 71 | mMediaPlayer.setOnSeekCompleteListener(seekCompleteListener); |
| TMBOY | 45:2aa9f933c8d2 | 72 | // 不同的音频源,此回调有的不回调!!! |
| TMBOY | 45:2aa9f933c8d2 | 73 | // mMediaPlayer.setOnInfoListener(infoListener); |
| TMBOY | 45:2aa9f933c8d2 | 74 | |
| TMBOY | 45:2aa9f933c8d2 | 75 | // 读取音量和静音的数据 |
| TMBOY | 45:2aa9f933c8d2 | 76 | currentVolume = (float) MediaPlayerPreferenceUtil.get(DcsSampleApplication.getInstance(), |
| TMBOY | 45:2aa9f933c8d2 | 77 | KEY_SP_VOLUME, 0.8f); |
| TMBOY | 45:2aa9f933c8d2 | 78 | isMute = (boolean) MediaPlayerPreferenceUtil.get(DcsSampleApplication.getInstance(), |
| TMBOY | 45:2aa9f933c8d2 | 79 | KEY_SP_MUTE, false); |
| TMBOY | 45:2aa9f933c8d2 | 80 | // LinkedList |
| TMBOY | 45:2aa9f933c8d2 | 81 | mediaPlayerListeners = Collections.synchronizedList(new LinkedList<IMediaPlayer.IMediaPlayerListener>()); |
| TMBOY | 45:2aa9f933c8d2 | 82 | |
| TMBOY | 45:2aa9f933c8d2 | 83 | // 来电监听 |
| TMBOY | 45:2aa9f933c8d2 | 84 | telephonyManager = (TelephonyManager) |
| TMBOY | 45:2aa9f933c8d2 | 85 | DcsSampleApplication.getInstance().getSystemService(Service.TELEPHONY_SERVICE); |
| TMBOY | 45:2aa9f933c8d2 | 86 | telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); |
| TMBOY | 45:2aa9f933c8d2 | 87 | } |
| TMBOY | 45:2aa9f933c8d2 | 88 | |
| TMBOY | 45:2aa9f933c8d2 | 89 | private PhoneStateListener phoneStateListener = new PhoneStateListener() { |
| TMBOY | 45:2aa9f933c8d2 | 90 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 91 | public void onCallStateChanged(int state, String incomingNumber) { |
| TMBOY | 45:2aa9f933c8d2 | 92 | super.onCallStateChanged(state, incomingNumber); |
| TMBOY | 45:2aa9f933c8d2 | 93 | switch (state) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | // 电话挂断 |
| TMBOY | 45:2aa9f933c8d2 | 95 | case TelephonyManager.CALL_STATE_IDLE: |
| TMBOY | 45:2aa9f933c8d2 | 96 | resume(); |
| TMBOY | 45:2aa9f933c8d2 | 97 | break; |
| TMBOY | 45:2aa9f933c8d2 | 98 | // 等待接电话 |
| TMBOY | 45:2aa9f933c8d2 | 99 | case TelephonyManager.CALL_STATE_RINGING: |
| TMBOY | 45:2aa9f933c8d2 | 100 | pause(); |
| TMBOY | 45:2aa9f933c8d2 | 101 | break; |
| TMBOY | 45:2aa9f933c8d2 | 102 | // 通话中 |
| TMBOY | 45:2aa9f933c8d2 | 103 | case TelephonyManager.CALL_STATE_OFFHOOK: |
| TMBOY | 45:2aa9f933c8d2 | 104 | break; |
| TMBOY | 45:2aa9f933c8d2 | 105 | default: |
| TMBOY | 45:2aa9f933c8d2 | 106 | break; |
| TMBOY | 45:2aa9f933c8d2 | 107 | } |
| TMBOY | 45:2aa9f933c8d2 | 108 | } |
| TMBOY | 45:2aa9f933c8d2 | 109 | }; |
| TMBOY | 45:2aa9f933c8d2 | 110 | |
| TMBOY | 45:2aa9f933c8d2 | 111 | private IAudioStreamStore.OnStoreListener onStoreListener = new IAudioStreamStore.SimpleOnStoreListener() { |
| TMBOY | 45:2aa9f933c8d2 | 112 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 113 | public void onComplete(String path) { |
| TMBOY | 45:2aa9f933c8d2 | 114 | LogUtil.e(TAG, "onStoreListener,path:" + path); |
| TMBOY | 45:2aa9f933c8d2 | 115 | // after down star play |
| TMBOY | 45:2aa9f933c8d2 | 116 | File file = new File(path); |
| TMBOY | 45:2aa9f933c8d2 | 117 | if (file.exists() && file.length() > 0) { |
| TMBOY | 45:2aa9f933c8d2 | 118 | play(path); |
| TMBOY | 45:2aa9f933c8d2 | 119 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 120 | mCurrentState = IMediaPlayer.PlayState.ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 121 | fireOnError("play path not exists or length<0 ", |
| TMBOY | 45:2aa9f933c8d2 | 122 | IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR); |
| TMBOY | 45:2aa9f933c8d2 | 123 | } |
| TMBOY | 45:2aa9f933c8d2 | 124 | } |
| TMBOY | 45:2aa9f933c8d2 | 125 | }; |
| TMBOY | 45:2aa9f933c8d2 | 126 | |
| TMBOY | 45:2aa9f933c8d2 | 127 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 128 | public void play(MediaResource mediaResource) { |
| TMBOY | 45:2aa9f933c8d2 | 129 | if (mediaResource.isStream) { |
| TMBOY | 45:2aa9f933c8d2 | 130 | play(mediaResource.stream); |
| TMBOY | 45:2aa9f933c8d2 | 131 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 132 | play(mediaResource.url); |
| TMBOY | 45:2aa9f933c8d2 | 133 | } |
| TMBOY | 45:2aa9f933c8d2 | 134 | } |
| TMBOY | 45:2aa9f933c8d2 | 135 | |
| TMBOY | 45:2aa9f933c8d2 | 136 | private void play(InputStream stream) { |
| TMBOY | 45:2aa9f933c8d2 | 137 | LogUtil.e(TAG, "play stream"); |
| TMBOY | 45:2aa9f933c8d2 | 138 | if (audioStreamStore == null) { |
| TMBOY | 45:2aa9f933c8d2 | 139 | audioStreamStore = new AudioStreamStoreImpl(); |
| TMBOY | 45:2aa9f933c8d2 | 140 | } |
| TMBOY | 45:2aa9f933c8d2 | 141 | audioStreamStore.setOnStoreListener(onStoreListener); |
| TMBOY | 45:2aa9f933c8d2 | 142 | audioStreamStore.save(stream); |
| TMBOY | 45:2aa9f933c8d2 | 143 | } |
| TMBOY | 45:2aa9f933c8d2 | 144 | |
| TMBOY | 45:2aa9f933c8d2 | 145 | private void play(String url) { |
| TMBOY | 45:2aa9f933c8d2 | 146 | if (TextUtils.isEmpty(url)) { |
| TMBOY | 45:2aa9f933c8d2 | 147 | LogUtil.d(TAG, "play-url is empty"); |
| TMBOY | 45:2aa9f933c8d2 | 148 | fireOnError("play-url is empty.", |
| TMBOY | 45:2aa9f933c8d2 | 149 | IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR); |
| TMBOY | 45:2aa9f933c8d2 | 150 | mCurrentState = IMediaPlayer.PlayState.ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 151 | return; |
| TMBOY | 45:2aa9f933c8d2 | 152 | } |
| TMBOY | 45:2aa9f933c8d2 | 153 | fireOnInit(); |
| TMBOY | 45:2aa9f933c8d2 | 154 | LogUtil.d(TAG, "play-url:" + url); |
| TMBOY | 45:2aa9f933c8d2 | 155 | if (url.startsWith(ASSERT_PREFIX) && url.length() > ASSERT_PREFIX.length()) { |
| TMBOY | 45:2aa9f933c8d2 | 156 | playAsset(url.substring(ASSERT_PREFIX.length())); |
| TMBOY | 45:2aa9f933c8d2 | 157 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 158 | try { |
| TMBOY | 45:2aa9f933c8d2 | 159 | mMediaPlayer.reset(); |
| TMBOY | 45:2aa9f933c8d2 | 160 | mMediaPlayer.setDataSource(url); |
| TMBOY | 45:2aa9f933c8d2 | 161 | // Async |
| TMBOY | 45:2aa9f933c8d2 | 162 | mMediaPlayer.prepareAsync(); |
| TMBOY | 45:2aa9f933c8d2 | 163 | mCurrentState = IMediaPlayer.PlayState.PREPARING; |
| TMBOY | 45:2aa9f933c8d2 | 164 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 165 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 166 | LogUtil.d(TAG, "playPath", e); |
| TMBOY | 45:2aa9f933c8d2 | 167 | mCurrentState = IMediaPlayer.PlayState.ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 168 | fireOnError("IOException play url :" |
| TMBOY | 45:2aa9f933c8d2 | 169 | + url, IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR); |
| TMBOY | 45:2aa9f933c8d2 | 170 | } |
| TMBOY | 45:2aa9f933c8d2 | 171 | } |
| TMBOY | 45:2aa9f933c8d2 | 172 | } |
| TMBOY | 45:2aa9f933c8d2 | 173 | |
| TMBOY | 45:2aa9f933c8d2 | 174 | private void playAsset(String resName) { |
| TMBOY | 45:2aa9f933c8d2 | 175 | LogUtil.d(TAG, "playAsset:" + resName); |
| TMBOY | 45:2aa9f933c8d2 | 176 | try { |
| TMBOY | 45:2aa9f933c8d2 | 177 | AssetManager am = DcsSampleApplication.getInstance().getAssets(); |
| TMBOY | 45:2aa9f933c8d2 | 178 | AssetFileDescriptor afd = am.openFd(resName); |
| TMBOY | 45:2aa9f933c8d2 | 179 | mMediaPlayer.reset(); |
| TMBOY | 45:2aa9f933c8d2 | 180 | mMediaPlayer.setDataSource(afd.getFileDescriptor(), |
| TMBOY | 45:2aa9f933c8d2 | 181 | afd.getStartOffset(), afd.getLength()); |
| TMBOY | 45:2aa9f933c8d2 | 182 | mMediaPlayer.prepareAsync(); |
| TMBOY | 45:2aa9f933c8d2 | 183 | mCurrentState = IMediaPlayer.PlayState.PREPARING; |
| TMBOY | 45:2aa9f933c8d2 | 184 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 185 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 186 | LogUtil.d(TAG, "playAsset", e); |
| TMBOY | 45:2aa9f933c8d2 | 187 | mCurrentState = IMediaPlayer.PlayState.ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 188 | fireOnError("IOException play playAsset", |
| TMBOY | 45:2aa9f933c8d2 | 189 | IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR); |
| TMBOY | 45:2aa9f933c8d2 | 190 | } |
| TMBOY | 45:2aa9f933c8d2 | 191 | } |
| TMBOY | 45:2aa9f933c8d2 | 192 | |
| TMBOY | 45:2aa9f933c8d2 | 193 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 194 | public void pause() { |
| TMBOY | 45:2aa9f933c8d2 | 195 | if (mCurrentState == IMediaPlayer.PlayState.PLAYING |
| TMBOY | 45:2aa9f933c8d2 | 196 | || mCurrentState == IMediaPlayer.PlayState.PREPARED |
| TMBOY | 45:2aa9f933c8d2 | 197 | || mCurrentState == IMediaPlayer.PlayState.PREPARING) { |
| TMBOY | 45:2aa9f933c8d2 | 198 | mMediaPlayer.pause(); |
| TMBOY | 45:2aa9f933c8d2 | 199 | mCurrentState = IMediaPlayer.PlayState.PAUSED; |
| TMBOY | 45:2aa9f933c8d2 | 200 | fireOnPaused(); |
| TMBOY | 45:2aa9f933c8d2 | 201 | } |
| TMBOY | 45:2aa9f933c8d2 | 202 | } |
| TMBOY | 45:2aa9f933c8d2 | 203 | |
| TMBOY | 45:2aa9f933c8d2 | 204 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 205 | public void stop() { |
| TMBOY | 45:2aa9f933c8d2 | 206 | if (mMediaPlayer != null) { |
| TMBOY | 45:2aa9f933c8d2 | 207 | mMediaPlayer.stop(); |
| TMBOY | 45:2aa9f933c8d2 | 208 | mCurrentState = IMediaPlayer.PlayState.STOPPED; |
| TMBOY | 45:2aa9f933c8d2 | 209 | // delete audio file |
| TMBOY | 45:2aa9f933c8d2 | 210 | if (audioStreamStore != null) { |
| TMBOY | 45:2aa9f933c8d2 | 211 | audioStreamStore.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 212 | audioStreamStore.speakAfter(); |
| TMBOY | 45:2aa9f933c8d2 | 213 | } |
| TMBOY | 45:2aa9f933c8d2 | 214 | fireStopped(); |
| TMBOY | 45:2aa9f933c8d2 | 215 | } |
| TMBOY | 45:2aa9f933c8d2 | 216 | } |
| TMBOY | 45:2aa9f933c8d2 | 217 | |
| TMBOY | 45:2aa9f933c8d2 | 218 | |
| TMBOY | 45:2aa9f933c8d2 | 219 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 220 | public void resume() { |
| TMBOY | 45:2aa9f933c8d2 | 221 | if (mCurrentState == IMediaPlayer.PlayState.PAUSED) { |
| TMBOY | 45:2aa9f933c8d2 | 222 | mMediaPlayer.start(); |
| TMBOY | 45:2aa9f933c8d2 | 223 | mCurrentState = IMediaPlayer.PlayState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 224 | firePlaying(); |
| TMBOY | 45:2aa9f933c8d2 | 225 | } |
| TMBOY | 45:2aa9f933c8d2 | 226 | } |
| TMBOY | 45:2aa9f933c8d2 | 227 | |
| TMBOY | 45:2aa9f933c8d2 | 228 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 229 | public void release() { |
| TMBOY | 45:2aa9f933c8d2 | 230 | if (mMediaPlayer != null) { |
| TMBOY | 45:2aa9f933c8d2 | 231 | mMediaPlayer.stop(); |
| TMBOY | 45:2aa9f933c8d2 | 232 | mMediaPlayer.release(); |
| TMBOY | 45:2aa9f933c8d2 | 233 | mMediaPlayer = null; |
| TMBOY | 45:2aa9f933c8d2 | 234 | mCurrentState = IMediaPlayer.PlayState.IDLE; |
| TMBOY | 45:2aa9f933c8d2 | 235 | fireOnRelease(); |
| TMBOY | 45:2aa9f933c8d2 | 236 | } |
| TMBOY | 45:2aa9f933c8d2 | 237 | if (audioStreamStore != null) { |
| TMBOY | 45:2aa9f933c8d2 | 238 | audioStreamStore.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 239 | audioStreamStore.speakAfter(); |
| TMBOY | 45:2aa9f933c8d2 | 240 | } |
| TMBOY | 45:2aa9f933c8d2 | 241 | mediaPlayerListeners.clear(); |
| TMBOY | 45:2aa9f933c8d2 | 242 | } |
| TMBOY | 45:2aa9f933c8d2 | 243 | |
| TMBOY | 45:2aa9f933c8d2 | 244 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 245 | public PlayState getPlayState() { |
| TMBOY | 45:2aa9f933c8d2 | 246 | return mCurrentState; |
| TMBOY | 45:2aa9f933c8d2 | 247 | } |
| TMBOY | 45:2aa9f933c8d2 | 248 | |
| TMBOY | 45:2aa9f933c8d2 | 249 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 250 | public void seekTo(int pos) { |
| TMBOY | 45:2aa9f933c8d2 | 251 | currentSeekMilliseconds = pos; |
| TMBOY | 45:2aa9f933c8d2 | 252 | LogUtil.d(TAG, "seekTo,currentSeekMilliseconds:" + currentSeekMilliseconds); |
| TMBOY | 45:2aa9f933c8d2 | 253 | if (mMediaPlayer != null && mCurrentState == IMediaPlayer.PlayState.PREPARED) { |
| TMBOY | 45:2aa9f933c8d2 | 254 | mMediaPlayer.seekTo(pos); |
| TMBOY | 45:2aa9f933c8d2 | 255 | } |
| TMBOY | 45:2aa9f933c8d2 | 256 | } |
| TMBOY | 45:2aa9f933c8d2 | 257 | |
| TMBOY | 45:2aa9f933c8d2 | 258 | /** |
| TMBOY | 45:2aa9f933c8d2 | 259 | * 设置音量 |
| TMBOY | 45:2aa9f933c8d2 | 260 | * |
| TMBOY | 45:2aa9f933c8d2 | 261 | * @param volume 0.0 -1.0 |
| TMBOY | 45:2aa9f933c8d2 | 262 | */ |
| TMBOY | 45:2aa9f933c8d2 | 263 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 264 | public void setVolume(float volume) { |
| TMBOY | 45:2aa9f933c8d2 | 265 | // 设置音量就不再静音了,比如:说了调衡音量等操作 |
| TMBOY | 45:2aa9f933c8d2 | 266 | isMute = false; |
| TMBOY | 45:2aa9f933c8d2 | 267 | currentVolume = volume; |
| TMBOY | 45:2aa9f933c8d2 | 268 | if (mMediaPlayer != null) { |
| TMBOY | 45:2aa9f933c8d2 | 269 | mMediaPlayer.setVolume(volume, volume); |
| TMBOY | 45:2aa9f933c8d2 | 270 | } |
| TMBOY | 45:2aa9f933c8d2 | 271 | // 保存数据 |
| TMBOY | 45:2aa9f933c8d2 | 272 | MediaPlayerPreferenceUtil.put(DcsSampleApplication.getInstance(), |
| TMBOY | 45:2aa9f933c8d2 | 273 | KEY_SP_VOLUME, currentVolume); |
| TMBOY | 45:2aa9f933c8d2 | 274 | MediaPlayerPreferenceUtil.put(DcsSampleApplication.getInstance(), |
| TMBOY | 45:2aa9f933c8d2 | 275 | KEY_SP_MUTE, isMute); |
| TMBOY | 45:2aa9f933c8d2 | 276 | } |
| TMBOY | 45:2aa9f933c8d2 | 277 | |
| TMBOY | 45:2aa9f933c8d2 | 278 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 279 | public float getVolume() { |
| TMBOY | 45:2aa9f933c8d2 | 280 | return currentVolume; |
| TMBOY | 45:2aa9f933c8d2 | 281 | } |
| TMBOY | 45:2aa9f933c8d2 | 282 | |
| TMBOY | 45:2aa9f933c8d2 | 283 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 284 | public void setMute(boolean mute) { |
| TMBOY | 45:2aa9f933c8d2 | 285 | isMute = mute; |
| TMBOY | 45:2aa9f933c8d2 | 286 | if (mMediaPlayer != null) { |
| TMBOY | 45:2aa9f933c8d2 | 287 | if (mute) { |
| TMBOY | 45:2aa9f933c8d2 | 288 | mMediaPlayer.setVolume(0, 0); |
| TMBOY | 45:2aa9f933c8d2 | 289 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 290 | mMediaPlayer.setVolume(currentVolume, currentVolume); |
| TMBOY | 45:2aa9f933c8d2 | 291 | } |
| TMBOY | 45:2aa9f933c8d2 | 292 | } |
| TMBOY | 45:2aa9f933c8d2 | 293 | // 保存数据 |
| TMBOY | 45:2aa9f933c8d2 | 294 | MediaPlayerPreferenceUtil.put(DcsSampleApplication.getInstance(), |
| TMBOY | 45:2aa9f933c8d2 | 295 | KEY_SP_MUTE, isMute); |
| TMBOY | 45:2aa9f933c8d2 | 296 | } |
| TMBOY | 45:2aa9f933c8d2 | 297 | |
| TMBOY | 45:2aa9f933c8d2 | 298 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 299 | public boolean getMute() { |
| TMBOY | 45:2aa9f933c8d2 | 300 | return isMute; |
| TMBOY | 45:2aa9f933c8d2 | 301 | } |
| TMBOY | 45:2aa9f933c8d2 | 302 | |
| TMBOY | 45:2aa9f933c8d2 | 303 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 304 | public long getCurrentPosition() { |
| TMBOY | 45:2aa9f933c8d2 | 305 | if (mMediaPlayer == null) { |
| TMBOY | 45:2aa9f933c8d2 | 306 | return 0; |
| TMBOY | 45:2aa9f933c8d2 | 307 | } |
| TMBOY | 45:2aa9f933c8d2 | 308 | if (mCurrentState == IMediaPlayer.PlayState.IDLE || mCurrentState == IMediaPlayer.PlayState.ERROR) { |
| TMBOY | 45:2aa9f933c8d2 | 309 | return 0; |
| TMBOY | 45:2aa9f933c8d2 | 310 | } |
| TMBOY | 45:2aa9f933c8d2 | 311 | return mMediaPlayer.getCurrentPosition(); |
| TMBOY | 45:2aa9f933c8d2 | 312 | } |
| TMBOY | 45:2aa9f933c8d2 | 313 | |
| TMBOY | 45:2aa9f933c8d2 | 314 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 315 | public long getDuration() { |
| TMBOY | 45:2aa9f933c8d2 | 316 | if (mMediaPlayer == null) { |
| TMBOY | 45:2aa9f933c8d2 | 317 | return 0; |
| TMBOY | 45:2aa9f933c8d2 | 318 | } |
| TMBOY | 45:2aa9f933c8d2 | 319 | if (mCurrentState == IMediaPlayer.PlayState.IDLE || mCurrentState == IMediaPlayer.PlayState.ERROR) { |
| TMBOY | 45:2aa9f933c8d2 | 320 | return 0; |
| TMBOY | 45:2aa9f933c8d2 | 321 | } |
| TMBOY | 45:2aa9f933c8d2 | 322 | return mMediaPlayer.getDuration(); |
| TMBOY | 45:2aa9f933c8d2 | 323 | } |
| TMBOY | 45:2aa9f933c8d2 | 324 | |
| TMBOY | 45:2aa9f933c8d2 | 325 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 326 | public float getBufferPercentage() { |
| TMBOY | 45:2aa9f933c8d2 | 327 | return currentPercent; |
| TMBOY | 45:2aa9f933c8d2 | 328 | } |
| TMBOY | 45:2aa9f933c8d2 | 329 | |
| TMBOY | 45:2aa9f933c8d2 | 330 | |
| TMBOY | 45:2aa9f933c8d2 | 331 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 332 | public void addMediaPlayerListener(IMediaPlayerListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 333 | if (!mediaPlayerListeners.contains(listener)) { |
| TMBOY | 45:2aa9f933c8d2 | 334 | mediaPlayerListeners.add(listener); |
| TMBOY | 45:2aa9f933c8d2 | 335 | } |
| TMBOY | 45:2aa9f933c8d2 | 336 | } |
| TMBOY | 45:2aa9f933c8d2 | 337 | |
| TMBOY | 45:2aa9f933c8d2 | 338 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 339 | public void removeMediaPlayerListener(IMediaPlayerListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 340 | if (mediaPlayerListeners.contains(listener)) { |
| TMBOY | 45:2aa9f933c8d2 | 341 | mediaPlayerListeners.remove(listener); |
| TMBOY | 45:2aa9f933c8d2 | 342 | } |
| TMBOY | 45:2aa9f933c8d2 | 343 | } |
| TMBOY | 45:2aa9f933c8d2 | 344 | |
| TMBOY | 45:2aa9f933c8d2 | 345 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 346 | public void setActive(boolean isActive) { |
| TMBOY | 45:2aa9f933c8d2 | 347 | this.isActive = isActive; |
| TMBOY | 45:2aa9f933c8d2 | 348 | } |
| TMBOY | 45:2aa9f933c8d2 | 349 | |
| TMBOY | 45:2aa9f933c8d2 | 350 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 351 | public boolean isActive() { |
| TMBOY | 45:2aa9f933c8d2 | 352 | return isActive; |
| TMBOY | 45:2aa9f933c8d2 | 353 | } |
| TMBOY | 45:2aa9f933c8d2 | 354 | |
| TMBOY | 45:2aa9f933c8d2 | 355 | private void fireOnError(String error, ErrorType errorType) { |
| TMBOY | 45:2aa9f933c8d2 | 356 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 357 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 358 | listener.onError(error, errorType); |
| TMBOY | 45:2aa9f933c8d2 | 359 | } |
| TMBOY | 45:2aa9f933c8d2 | 360 | } |
| TMBOY | 45:2aa9f933c8d2 | 361 | } |
| TMBOY | 45:2aa9f933c8d2 | 362 | |
| TMBOY | 45:2aa9f933c8d2 | 363 | private void fireOnInit() { |
| TMBOY | 45:2aa9f933c8d2 | 364 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 365 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 366 | listener.onInit(); |
| TMBOY | 45:2aa9f933c8d2 | 367 | } |
| TMBOY | 45:2aa9f933c8d2 | 368 | } |
| TMBOY | 45:2aa9f933c8d2 | 369 | } |
| TMBOY | 45:2aa9f933c8d2 | 370 | |
| TMBOY | 45:2aa9f933c8d2 | 371 | private void fireOnPaused() { |
| TMBOY | 45:2aa9f933c8d2 | 372 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 373 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 374 | listener.onPaused(); |
| TMBOY | 45:2aa9f933c8d2 | 375 | } |
| TMBOY | 45:2aa9f933c8d2 | 376 | } |
| TMBOY | 45:2aa9f933c8d2 | 377 | } |
| TMBOY | 45:2aa9f933c8d2 | 378 | |
| TMBOY | 45:2aa9f933c8d2 | 379 | private void fireStopped() { |
| TMBOY | 45:2aa9f933c8d2 | 380 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 381 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 382 | listener.onStopped(); |
| TMBOY | 45:2aa9f933c8d2 | 383 | } |
| TMBOY | 45:2aa9f933c8d2 | 384 | } |
| TMBOY | 45:2aa9f933c8d2 | 385 | } |
| TMBOY | 45:2aa9f933c8d2 | 386 | |
| TMBOY | 45:2aa9f933c8d2 | 387 | private void firePlaying() { |
| TMBOY | 45:2aa9f933c8d2 | 388 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 389 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 390 | listener.onPlaying(); |
| TMBOY | 45:2aa9f933c8d2 | 391 | } |
| TMBOY | 45:2aa9f933c8d2 | 392 | } |
| TMBOY | 45:2aa9f933c8d2 | 393 | } |
| TMBOY | 45:2aa9f933c8d2 | 394 | |
| TMBOY | 45:2aa9f933c8d2 | 395 | private void fireOnRelease() { |
| TMBOY | 45:2aa9f933c8d2 | 396 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 397 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 398 | listener.onRelease(); |
| TMBOY | 45:2aa9f933c8d2 | 399 | } |
| TMBOY | 45:2aa9f933c8d2 | 400 | } |
| TMBOY | 45:2aa9f933c8d2 | 401 | } |
| TMBOY | 45:2aa9f933c8d2 | 402 | |
| TMBOY | 45:2aa9f933c8d2 | 403 | private void fireOnPrepared() { |
| TMBOY | 45:2aa9f933c8d2 | 404 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 405 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 406 | listener.onPrepared(); |
| TMBOY | 45:2aa9f933c8d2 | 407 | } |
| TMBOY | 45:2aa9f933c8d2 | 408 | } |
| TMBOY | 45:2aa9f933c8d2 | 409 | } |
| TMBOY | 45:2aa9f933c8d2 | 410 | |
| TMBOY | 45:2aa9f933c8d2 | 411 | private void fireOonBufferingUpdate(int percent) { |
| TMBOY | 45:2aa9f933c8d2 | 412 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 413 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 414 | listener.onBufferingUpdate(percent); |
| TMBOY | 45:2aa9f933c8d2 | 415 | } |
| TMBOY | 45:2aa9f933c8d2 | 416 | } |
| TMBOY | 45:2aa9f933c8d2 | 417 | } |
| TMBOY | 45:2aa9f933c8d2 | 418 | |
| TMBOY | 45:2aa9f933c8d2 | 419 | private void fireOnCompletion() { |
| TMBOY | 45:2aa9f933c8d2 | 420 | for (IMediaPlayer.IMediaPlayerListener listener : mediaPlayerListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 421 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 422 | listener.onCompletion(); |
| TMBOY | 45:2aa9f933c8d2 | 423 | } |
| TMBOY | 45:2aa9f933c8d2 | 424 | } |
| TMBOY | 45:2aa9f933c8d2 | 425 | } |
| TMBOY | 45:2aa9f933c8d2 | 426 | |
| TMBOY | 45:2aa9f933c8d2 | 427 | private MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() { |
| TMBOY | 45:2aa9f933c8d2 | 428 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 429 | public void onPrepared(MediaPlayer mp) { |
| TMBOY | 45:2aa9f933c8d2 | 430 | LogUtil.d(TAG, "onPrepared"); |
| TMBOY | 45:2aa9f933c8d2 | 431 | mCurrentState = IMediaPlayer.PlayState.PREPARED; |
| TMBOY | 45:2aa9f933c8d2 | 432 | isError38 = false; |
| TMBOY | 45:2aa9f933c8d2 | 433 | fireOnPrepared(); |
| TMBOY | 45:2aa9f933c8d2 | 434 | // must be called after prepareAsync or prepare |
| TMBOY | 45:2aa9f933c8d2 | 435 | LogUtil.d(TAG, "currentVolume:" + currentVolume); |
| TMBOY | 45:2aa9f933c8d2 | 436 | LogUtil.d(TAG, "currentSeekMilliseconds:" + currentSeekMilliseconds); |
| TMBOY | 45:2aa9f933c8d2 | 437 | // 一开始就说话让它静音了 |
| TMBOY | 45:2aa9f933c8d2 | 438 | if (isMute) { |
| TMBOY | 45:2aa9f933c8d2 | 439 | mMediaPlayer.setVolume(0, 0); |
| TMBOY | 45:2aa9f933c8d2 | 440 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 441 | setVolume(currentVolume); |
| TMBOY | 45:2aa9f933c8d2 | 442 | } |
| TMBOY | 45:2aa9f933c8d2 | 443 | seekTo(currentSeekMilliseconds); |
| TMBOY | 45:2aa9f933c8d2 | 444 | } |
| TMBOY | 45:2aa9f933c8d2 | 445 | }; |
| TMBOY | 45:2aa9f933c8d2 | 446 | |
| TMBOY | 45:2aa9f933c8d2 | 447 | private MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener = |
| TMBOY | 45:2aa9f933c8d2 | 448 | new MediaPlayer.OnBufferingUpdateListener() { |
| TMBOY | 45:2aa9f933c8d2 | 449 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 450 | public void onBufferingUpdate(MediaPlayer mp, int percent) { |
| TMBOY | 45:2aa9f933c8d2 | 451 | currentPercent = percent * 1.0f; |
| TMBOY | 45:2aa9f933c8d2 | 452 | fireOonBufferingUpdate(percent); |
| TMBOY | 45:2aa9f933c8d2 | 453 | } |
| TMBOY | 45:2aa9f933c8d2 | 454 | }; |
| TMBOY | 45:2aa9f933c8d2 | 455 | private MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() { |
| TMBOY | 45:2aa9f933c8d2 | 456 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 457 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| TMBOY | 45:2aa9f933c8d2 | 458 | LogUtil.d(TAG, "onError:" + what + ", extra:" + extra); |
| TMBOY | 45:2aa9f933c8d2 | 459 | if (what == -38) { |
| TMBOY | 45:2aa9f933c8d2 | 460 | isError38 = true; |
| TMBOY | 45:2aa9f933c8d2 | 461 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 462 | } |
| TMBOY | 45:2aa9f933c8d2 | 463 | isError38 = false; |
| TMBOY | 45:2aa9f933c8d2 | 464 | mCurrentState = IMediaPlayer.PlayState.ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 465 | JSONObject jsonObject = new JSONObject(); |
| TMBOY | 45:2aa9f933c8d2 | 466 | try { |
| TMBOY | 45:2aa9f933c8d2 | 467 | jsonObject.put("msg", "what: " + what + "; extra:" + extra); |
| TMBOY | 45:2aa9f933c8d2 | 468 | } catch (JSONException e) { |
| TMBOY | 45:2aa9f933c8d2 | 469 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 470 | } |
| TMBOY | 45:2aa9f933c8d2 | 471 | IMediaPlayer.ErrorType errorType; |
| TMBOY | 45:2aa9f933c8d2 | 472 | switch (what) { |
| TMBOY | 45:2aa9f933c8d2 | 473 | case MediaPlayer.MEDIA_ERROR_IO: |
| TMBOY | 45:2aa9f933c8d2 | 474 | // Stream服务端返回错误 (bad request, unauthorized, forbidden, not found etc) |
| TMBOY | 45:2aa9f933c8d2 | 475 | errorType = IMediaPlayer.ErrorType.MEDIA_ERROR_INVALID_REQUEST; |
| TMBOY | 45:2aa9f933c8d2 | 476 | break; |
| TMBOY | 45:2aa9f933c8d2 | 477 | case MediaPlayer.MEDIA_ERROR_TIMED_OUT: |
| TMBOY | 45:2aa9f933c8d2 | 478 | // 端无法连接stream服务端 |
| TMBOY | 45:2aa9f933c8d2 | 479 | errorType = IMediaPlayer.ErrorType.MEDIA_ERROR_SERVICE_UNAVAILABLE; |
| TMBOY | 45:2aa9f933c8d2 | 480 | break; |
| TMBOY | 45:2aa9f933c8d2 | 481 | case MediaPlayer.MEDIA_ERROR_UNSUPPORTED: |
| TMBOY | 45:2aa9f933c8d2 | 482 | // 端内部错误 |
| TMBOY | 45:2aa9f933c8d2 | 483 | errorType = IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_DEVICE_ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 484 | break; |
| TMBOY | 45:2aa9f933c8d2 | 485 | case MediaPlayer.MEDIA_ERROR_MALFORMED: |
| TMBOY | 45:2aa9f933c8d2 | 486 | // stream服务端接受请求,但未能正确处理 ????? |
| TMBOY | 45:2aa9f933c8d2 | 487 | errorType = IMediaPlayer.ErrorType.MEDIA_ERROR_INTERNAL_SERVER_ERROR; |
| TMBOY | 45:2aa9f933c8d2 | 488 | break; |
| TMBOY | 45:2aa9f933c8d2 | 489 | default: |
| TMBOY | 45:2aa9f933c8d2 | 490 | // 未知错误 |
| TMBOY | 45:2aa9f933c8d2 | 491 | errorType = IMediaPlayer.ErrorType.MEDIA_ERROR_UNKNOWN; |
| TMBOY | 45:2aa9f933c8d2 | 492 | break; |
| TMBOY | 45:2aa9f933c8d2 | 493 | } |
| TMBOY | 45:2aa9f933c8d2 | 494 | fireOnError(jsonObject.toString(), errorType); |
| TMBOY | 45:2aa9f933c8d2 | 495 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 496 | } |
| TMBOY | 45:2aa9f933c8d2 | 497 | }; |
| TMBOY | 45:2aa9f933c8d2 | 498 | |
| TMBOY | 45:2aa9f933c8d2 | 499 | private MediaPlayer.OnSeekCompleteListener seekCompleteListener = new MediaPlayer.OnSeekCompleteListener() { |
| TMBOY | 45:2aa9f933c8d2 | 500 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 501 | public void onSeekComplete(MediaPlayer mp) { |
| TMBOY | 45:2aa9f933c8d2 | 502 | LogUtil.d(TAG, "onSeekComplete"); |
| TMBOY | 45:2aa9f933c8d2 | 503 | if (mCurrentState == IMediaPlayer.PlayState.PREPARED) { |
| TMBOY | 45:2aa9f933c8d2 | 504 | mp.start(); |
| TMBOY | 45:2aa9f933c8d2 | 505 | mCurrentState = IMediaPlayer.PlayState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 506 | firePlaying(); |
| TMBOY | 45:2aa9f933c8d2 | 507 | } |
| TMBOY | 45:2aa9f933c8d2 | 508 | } |
| TMBOY | 45:2aa9f933c8d2 | 509 | }; |
| TMBOY | 45:2aa9f933c8d2 | 510 | |
| TMBOY | 45:2aa9f933c8d2 | 511 | private MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() { |
| TMBOY | 45:2aa9f933c8d2 | 512 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 513 | public void onCompletion(MediaPlayer mp) { |
| TMBOY | 45:2aa9f933c8d2 | 514 | if (isError38) { |
| TMBOY | 45:2aa9f933c8d2 | 515 | return; |
| TMBOY | 45:2aa9f933c8d2 | 516 | } |
| TMBOY | 45:2aa9f933c8d2 | 517 | LogUtil.d(TAG, "onCompletion"); |
| TMBOY | 45:2aa9f933c8d2 | 518 | // delete audio file |
| TMBOY | 45:2aa9f933c8d2 | 519 | if (audioStreamStore != null) { |
| TMBOY | 45:2aa9f933c8d2 | 520 | audioStreamStore.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 521 | audioStreamStore.speakAfter(); |
| TMBOY | 45:2aa9f933c8d2 | 522 | } |
| TMBOY | 45:2aa9f933c8d2 | 523 | mCurrentState = IMediaPlayer.PlayState.COMPLETED; |
| TMBOY | 45:2aa9f933c8d2 | 524 | fireOnCompletion(); |
| TMBOY | 45:2aa9f933c8d2 | 525 | } |
| TMBOY | 45:2aa9f933c8d2 | 526 | }; |
| TMBOY | 45:2aa9f933c8d2 | 527 | } |
