Android开发简单mp4播放器
Android开发简单mp4播放器
简单的mp4播放器,适用于大作业。商业用还是不太行的。商业用一般接的是第三方播放器的。
一、思路:
用MediaPlayer和SurfaceView结合
二、效果图:
看视频直观点:
Android开发教程案例源码分享-简单mp4播放器
三、关键代码:
public class Mp4MainActivity extends AppCompatActivity {
private SurfaceView surface_view;
private SeekBar musicProgress;
private TextView currentTime, totalTime;
private ImageView prevBtn, playPauseBtn, nextBtn;
private MediaPlayer player;
private int currentPlaying = 0; //用作ArrayList下表,当前播放的歌曲、
private ArrayList<Integer> playList = new ArrayList<>();
private SurfaceHolder surface_holder;
private boolean isPausing, isPlaying; //音乐暂停状态,音乐第一次播放之后变为true
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
preparePlayList();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (isPlaying) {
updateTimer();
}
}
};
new Timer().scheduleAtFixedRate(timerTask, 0, 500);
}
private void init() {
surface_view = findViewById(R.id.surface_view);
musicProgress = findViewById(R.id.sb_progress);
currentTime = findViewById(R.id.tv_progress_current);
totalTime = findViewById(R.id.tv_progress_total);
prevBtn = findViewById(R.id.btn_prev);
playPauseBtn = findViewById(R.id.btn_play_pause);
nextBtn = findViewById(R.id.btn_next);
View.OnClickListener onClick = new OnClickControl();
prevBtn.setOnClickListener(onClick);
playPauseBtn.setOnClickListener(onClick);
nextBtn.setOnClickListener(onClick);
OnSeekBarChangeControl onSbChange = new OnSeekBarChangeControl();
musicProgress.setOnSeekBarChangeListener(onSbChange);
}
private void preparePlayList() {
Field[] field = R.raw.class.getFields();
for (int count = 0; count < field.length; count++) {
Log.i("Raw Asset", field[count].getName());
try {
int resId = field[count].getInt(field[count]);
playList.add(resId);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
private void prepareMedia() {
try {
if (isPlaying) {
player.stop();
player.reset();
}
player = MediaPlayer.create(getApplicationContext(), playList.get(currentPlaying));
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (surface_holder == null){
surface_holder = surface_view.getHolder();
surface_holder.setFixedSize(1080, 1920);
}
player.setDisplay(surface_holder); /* 设置播放载体 */
int musicDuration = player.getDuration();
musicProgress.setMax(musicDuration);
int sec = musicDuration / 1000;
int min = sec / 60;
sec -= min * 60;
String musicTime = String.format("%02d:%02d", min, sec);
totalTime.setText(musicTime);
player.start();
}catch (Exception e){
e.printStackTrace();
}
}
private void updateTimer() {
runOnUiThread(() -> {
int currentMs = player.getCurrentPosition();
int sec = currentMs / 1000;
int min = sec / 60;
sec -= min * 60;
String time = String.format("%02d:%02d", min, sec);
musicProgress.setProgress(currentMs);
currentTime.setText(time);
});
}
private class OnClickControl implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_prev:
//重播
playPauseBtn.setImageResource(R.mipmap.ic_pause); //切换成暂停键
if (player != null && !player.isPlaying()) {
currentPlaying = --currentPlaying % playList.size();
}
prepareMedia();
isPausing = false;
isPlaying = true;
break;
case R.id.btn_play_pause:
//开始暂停
if (!isPausing && !isPlaying) { //暂停状态,且从未被播放
//开始播放
playPauseBtn.setImageResource(R.mipmap.ic_pause); //切换成暂停键
prepareMedia();
isPlaying = true;
} else if (!isPausing && isPlaying) { //暂停状态,且被播放过一次
//继续播放
playPauseBtn.setImageResource(R.mipmap.ic_pause);//切换成暂停键
player.start();
} else { //播放状态
四、项目demo源码结构图:
有问题或者需要完整源码demo的私信我