| | |
| | | |
| | | import com.google.android.exoplayer2.ExoPlayer; |
| | | import com.google.android.exoplayer2.MediaItem; |
| | | import com.google.android.exoplayer2.PlaybackException; |
| | | import com.google.android.exoplayer2.Player; |
| | | import com.google.android.exoplayer2.SimpleExoPlayer; |
| | | import com.google.android.exoplayer2.source.MediaSource; |
| | | import com.sinata.xqmuse.utils.exo.ExoMediaPlayer; |
| | | import com.sinata.xqmuse.utils.exo.ExoMediaPlayerFactory; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | |
| | | public static final int MAX_LENGTH = 1000 * 600;// 最大录音时长十分钟,实际行程设置为5分钟上传一次 |
| | | |
| | | private OnAudioStatusUpdateListener audioStatusUpdateListener; |
| | | private MediaPlayer mMediaPlayer; |
| | | private SimpleExoPlayer mMediaPlayer; |
| | | |
| | | private float volume = 0.5f; //播放音量 |
| | | |
| | |
| | | if (mMediaPlayer != null) { |
| | | stopPlayMusic(false); |
| | | } |
| | | mMediaPlayer = new MediaPlayer(); |
| | | mMediaPlayer.setVolume(volume,volume); |
| | | mMediaPlayer = new SimpleExoPlayer.Builder(context).build(); |
| | | mMediaPlayer.setVolume(volume); |
| | | isPause = false; |
| | | try { |
| | | Log.e("mmp", "开始播放 path:" + filePath); |
| | | |
| | | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); |
| | | mMediaPlayer.setDataSource(context, Uri.parse(filePath)); |
| | | mMediaPlayer.prepareAsync(); |
| | | mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
| | | MediaItem mediaItem = MediaItem.fromUri(Uri.parse(filePath)); |
| | | mMediaPlayer.setMediaItem(mediaItem); |
| | | mMediaPlayer.prepare(); |
| | | mMediaPlayer.play(); |
| | | mMediaPlayer.addListener(new Player.Listener(){ |
| | | @Override |
| | | public void onPrepared(MediaPlayer mp) { |
| | | // 装载完毕回调 |
| | | mMediaPlayer.start(); |
| | | if (audioStatusUpdateListener != null){ |
| | | audioStatusUpdateListener.onGetDuration(mMediaPlayer.getDuration()); |
| | | audioStatusUpdateListener.onStartPlay(); |
| | | } |
| | | public void onPlayerError(PlaybackException error) { |
| | | Log.e("mmp","播放错误:"+error.getMessage()); |
| | | } |
| | | |
| | | |
| | | }); |
| | | mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { |
| | | @Override |
| | | public void onCompletion(MediaPlayer mp) { |
| | | stopPlayMusic(true); |
| | | // baseEvent.setMsg(filePath); |
| | | // EventBus.getDefault().post(baseEvent); |
| | | public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { |
| | | if (playbackState == Player.STATE_BUFFERING) |
| | | Log.e("mmp","缓冲中...STATE_BUFFERING"); |
| | | else if (playbackState == Player.STATE_READY){ |
| | | Log.e("mmp","缓冲完成,音频准备就绪:STATE_READY"); |
| | | |
| | | } else if (playbackState == Player.STATE_ENDED){ |
| | | Log.e("mmp","播放完成:STATE_ENDED"); |
| | | stopPlayMusic(true); |
| | | } else if (playbackState == Player.STATE_IDLE){ |
| | | Log.e("mmp","播放器空闲:STATE_IDLE"); |
| | | } |
| | | } |
| | | }); |
| | | } catch (Exception e) { |
| | |
| | | if (mMediaPlayer != null) { |
| | | stopPlayMusic(false); |
| | | } |
| | | mMediaPlayer = new MediaPlayer(); |
| | | mMediaPlayer.setVolume(volume,volume); |
| | | mMediaPlayer = new SimpleExoPlayer.Builder(context).build(); |
| | | mMediaPlayer.setVolume(volume); |
| | | isPause = false; |
| | | try { |
| | | Log.e("mmp", "循环播放 path:" + filePath); |
| | | mMediaPlayer.setDataSource(context, Uri.parse(filePath)); |
| | | mMediaPlayer.prepareAsync(); |
| | | mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
| | | @Override |
| | | public void onPrepared(MediaPlayer mp) { |
| | | // 装载完毕回调 |
| | | mMediaPlayer.start(); |
| | | if (audioStatusUpdateListener != null) |
| | | audioStatusUpdateListener.onStartPlay(); |
| | | } |
| | | }); |
| | | mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { |
| | | @Override |
| | | public void onCompletion(MediaPlayer mp) { |
| | | // Log.e("mmp", "播放完成 从新循环 path:" + filePath); |
| | | mMediaPlayer.start(); |
| | | if (isPause){ |
| | | // Log.e("mmp", "循环播放异步暂停"); |
| | | mMediaPlayer.pause(); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | MediaItem mediaItem = MediaItem.fromUri(Uri.parse(filePath)); |
| | | mMediaPlayer.setMediaItem(mediaItem); |
| | | mMediaPlayer.prepare(); |
| | | mMediaPlayer.setRepeatMode(Player.REPEAT_MODE_ONE); |
| | | mMediaPlayer.play(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | |
| | | public void resume(){ |
| | | if (mMediaPlayer != null) { |
| | | mMediaPlayer.start(); |
| | | mMediaPlayer.play(); |
| | | isPause = false; |
| | | } |
| | | } |
| | |
| | | public void setVolume(float volume){ |
| | | this.volume = volume; |
| | | if (mMediaPlayer!=null){ |
| | | mMediaPlayer.setVolume(volume,volume); |
| | | mMediaPlayer.setVolume(volume); |
| | | } |
| | | } |
| | | |
| | | public int getCurrentPosition(){ |
| | | public long getCurrentPosition(){ |
| | | if (mMediaPlayer!=null){ |
| | | return mMediaPlayer.getCurrentPosition(); |
| | | } |
| | |
| | | * @param filePath |
| | | * @return |
| | | */ |
| | | public int getMusicTime(Context context, String filePath) { |
| | | if (mMediaPlayer != null) { |
| | | stopPlayMusic(false); |
| | | } |
| | | mMediaPlayer = new MediaPlayer(); |
| | | try { |
| | | mMediaPlayer.setDataSource(context, Uri.parse(filePath)); |
| | | mMediaPlayer.prepare(); |
| | | int duration = mMediaPlayer.getDuration(); |
| | | if (0 != duration) { |
| | | mMediaPlayer.release(); |
| | | return duration / 1000; |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return 0; |
| | | } |
| | | // public int getMusicTime(Context context, String filePath) { |
| | | // if (mMediaPlayer != null) { |
| | | // stopPlayMusic(false); |
| | | // } |
| | | // mMediaPlayer = new MediaPlayer(); |
| | | // try { |
| | | // mMediaPlayer.setDataSource(context, Uri.parse(filePath)); |
| | | // mMediaPlayer.prepare(); |
| | | // int duration = mMediaPlayer.getDuration(); |
| | | // if (0 != duration) { |
| | | // mMediaPlayer.release(); |
| | | // return duration / 1000; |
| | | // } |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // return 0; |
| | | // } |
| | | |
| | | } |