1、复制音频文件到raw文件夹下
2、实例化音频文件
private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() { // 声音public void onCompletion(MediaPlayer mediaPlayer) {mediaPlayer.seekTo(0);}
};
private static final float BEEP_VOLUME = 9.10f;
private MediaPlayer mediaPlayer;
private void initBeepSound() {if ( mediaPlayer == null) {// The volume on STREAM_SYSTEM is not adjustable, and users found it// too loud,// so we now play on the music stream.mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mediaPlayer.setOnCompletionListener(beepListener);AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.sou);try {mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());file.close();mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);mediaPlayer.prepare();} catch (IOException e) {mediaPlayer = null;}}
}
3、开始播放
mediaPlayer.start();
2、播放云端MP3文件
1、添加音频播放器依赖
//exoplayer播放器
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
// 视频缓存
implementation 'com.danikula:videocache:2.7.0'
implementation 'org.greenrobot:greendao:3.2.2'
2、播放云端MP3——文件链接
PlayerView mExoPlay;
private SimpleExoPlayerHelper mMySimpleExoPlayer;private List<String> mVideoSourseList = new ArrayList<>();
btn_downMP3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String vds = "https://gzlxcdg.https.xiaozhuschool.com/public/uploads/video/20200504/fa530b8ae53f8d2e7698ea0fb22d22d5.mp4";String urlStr = "http://zsxyylsb.app.xiaozhuschool.com/public/uploads/imgs/20200902/b04b350875d486b6957973bf7ea56dfc.mp3";if (!mVideoSourseList.isEmpty()) {mVideoSourseList.clear();}mVideoSourseList.add(urlStr);mVideoSourseList.add(vds);mMySimpleExoPlayer.prepare(mVideoSourseList);mMySimpleExoPlayer.setRepeatMode(SimpleExoPlayerHelper.RepeatMode.REPEAT_MODE_OFF);mMySimpleExoPlayer.start();//播放云MP3}
});
3、播文件夹里面的MP3文件
MediaPlayer medPlay;
medPlay=MediaPlayer.create(this,Uri.parse("/storage/emulated/0/data/cn.dlc.xiaoyao/msc1/海洋第一集.mp3"));
开始
medPlay.start();
暂停
medPlay.pause();
重新开始
medPlay.stop();
medPlay=MediaPlayer.create(MainActivity.this,Uri.parse("/storage/emulated/0/data/cn.dlc.xiaoyao/msc1/海洋第二集.mp3"));medPlay.start();
4、获取MP3文件播放时长
public long getAudioFileVoiceTime(String filePath) {long mediaPlayerDuration = 0L;if (filePath == null || filePath.isEmpty()) {return 0;}MediaPlayer mediaPlayer = new MediaPlayer();try {mediaPlayer.setDataSource(filePath);mediaPlayer.prepare();mediaPlayerDuration = mediaPlayer.getDuration();} catch (IOException ioException) {LogPlus.i("lgq", ioException.getMessage());}if (mediaPlayer != null) {mediaPlayer.stop();mediaPlayer.reset();mediaPlayer.release();}return mediaPlayerDuration;
}
时长转为分钟,秒数显示
int fz = (int)(getAudioFileVoiceTime(files[i].getPath())/1000)/60;
LogPlus.d("lgq==fz ==="+fz+"。。。。余数==="+(getAudioFileVoiceTime(files[i].getPath())/1000)%60);