1 package com.netease.nimlib.sdk.media.player;
3 import android.content.Context;
4 import android.media.AudioManager;
5 import android.media.AudioManager.OnAudioFocusChangeListener;
6 import android.media.MediaPlayer;
7 import android.media.MediaPlayer.OnCompletionListener;
8 import android.media.MediaPlayer.OnErrorListener;
9 import android.media.MediaPlayer.OnPreparedListener;
10 import android.os.Handler;
11 import android.os.Message;
12 import android.text.TextUtils;
24 public static final String
TAG =
"AudioPlayer";
26 private MediaPlayer mPlayer;
28 private String mAudioFile;
30 private long mIntervalTime = 500L;
32 private AudioManager audioManager;
36 private int audioStreamType = AudioManager.STREAM_VOICE_CALL;
38 private static final int WHAT_COUNT_PLAY = 0x000;
40 private static final int WHAT_DECODE_SUCCEED = 0x001;
42 private static final int WHAT_DECODE_FAILED = 0x002;
49 this(context, null, null);
59 audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
60 mAudioFile = audioFile;
70 if (!TextUtils.equals(audioFile, mAudioFile)) {
71 NimLog.audio(
"start play audio file" + audioFile);
72 mAudioFile = audioFile;
93 public void start(
int audioStreamType) {
94 this.audioStreamType = audioStreamType;
102 if (mPlayer != null) {
104 if (mListener != null) {
117 return mPlayer != null && mPlayer.isPlaying();
118 }
catch (Throwable e) {
120 NimLog.e(TAG,
"isPlaying error", e);
131 if (mPlayer != null) {
133 return mPlayer.getDuration();
134 }
catch (Throwable e) {
136 NimLog.e(TAG,
"getDuration error", e);
150 if (mPlayer != null) {
152 return mPlayer.getCurrentPosition();
153 }
catch (Throwable e) {
155 NimLog.e(TAG,
"getCurrentPosition error", e);
170 mPlayer.seekTo(msec);
171 }
catch (Throwable e) {
173 NimLog.e(TAG,
"seekTo error", e);
177 private void setVolume(
float leftVolume,
float rightVolume) {
178 if (mPlayer == null) {
182 mPlayer.setVolume(leftVolume, rightVolume);
183 }
catch (Throwable e) {
185 NimLog.e(TAG,
"setVolume error", e);
189 private void startPlay() {
190 NimLog.d(TAG,
"start() called");
197 private void endPlay() {
198 audioManager.abandonAudioFocus(onAudioFocusChangeListener);
200 if (mPlayer != null) {
203 }
catch (Throwable e) {
205 NimLog.e(TAG,
"endPlay error", e);
209 }
catch (Throwable e) {
211 NimLog.e(TAG,
"endPlay error", e);
214 mHandler.removeMessages(WHAT_COUNT_PLAY);
218 private Handler mHandler =
new Handler() {
220 public void handleMessage(Message msg) {
222 case WHAT_COUNT_PLAY:
224 if (mListener != null) {
225 mListener.
onPlaying(mPlayer.getCurrentPosition());
227 }
catch (Throwable e) {
229 NimLog.e(TAG,
" error", e);
231 sendEmptyMessageDelayed(WHAT_COUNT_PLAY, mIntervalTime);
233 case WHAT_DECODE_FAILED: {
235 NimLog.e(TAG,
"convert() error: " + mAudioFile);
238 case WHAT_DECODE_SUCCEED:
246 private void startInner() {
247 mPlayer =
new MediaPlayer();
249 mPlayer.setLooping(
false);
250 mPlayer.setAudioStreamType(audioStreamType);
252 if (audioStreamType == AudioManager.STREAM_MUSIC) {
253 audioManager.setSpeakerphoneOn(
true);
255 audioManager.setSpeakerphoneOn(
false);
258 audioManager.requestAudioFocus(
259 onAudioFocusChangeListener,
261 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
263 mPlayer.setOnPreparedListener(
new OnPreparedListener() {
265 public void onPrepared(MediaPlayer mp) {
266 NimLog.d(TAG,
"player:onPrepared");
267 mHandler.sendEmptyMessage(WHAT_COUNT_PLAY);
268 if (mListener != null) {
274 mPlayer.setOnCompletionListener(
new OnCompletionListener() {
276 public void onCompletion(MediaPlayer mp) {
277 NimLog.d(TAG,
"player:onCompletion");
279 if (mListener != null) {
285 mPlayer.setOnErrorListener(
new OnErrorListener() {
287 public boolean onError(MediaPlayer mp,
int what,
int extra) {
288 NimLog.e(TAG, String.format(
"player:onOnError what:%d extra:%d", what, extra));
290 if (mListener != null) {
291 mListener.
onError(String.format(
"OnErrorListener what:%d extra:%d", what, extra));
297 if (mAudioFile != null) {
298 mPlayer.setDataSource(mAudioFile);
300 if (mListener != null) {
301 mListener.
onError(
"no datasource");
309 NimLog.d(TAG,
"player:start ok---->" + mAudioFile);
310 }
catch (Throwable e) {
312 NimLog.e(TAG,
"player:onOnError Exception\n" + e.toString());
314 if (mListener != null) {
315 mListener.
onError(
"Exception\n" + e.toString());
320 private void deleteOnExit() {
321 File converted =
new File(mAudioFile);
322 if (converted.exists()) {
323 converted.deleteOnExit();
327 OnAudioFocusChangeListener onAudioFocusChangeListener =
new OnAudioFocusChangeListener() {
330 public void onAudioFocusChange(
int focusChange) {
331 switch (focusChange) {
332 case AudioManager.AUDIOFOCUS_GAIN:
335 setVolume(1.0f, 1.0f);
339 case AudioManager.AUDIOFOCUS_LOSS:
344 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
348 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
351 setVolume(0.1f, 0.1f);