代码实现基于Android系统实现多媒体播放器app
时间: 2023-11-28 14:05:02 浏览: 257
要实现一个基于Android系统的多媒体播放器app,需要以下步骤:
1. 创建一个Android项目,在AndroidManifest.xml中添加必要的权限,如读取SD卡权限、网络访问权限等。
2. 在布局文件中添加一个用于显示视频的SurfaceView和一些控制按钮,如开始/暂停、快进/快退、音量调节等。
3. 在代码中初始化MediaPlayer对象,并设置监听器,以便在播放状态发生改变时更新UI。
4. 在按钮的点击事件中调用MediaPlayer的相应方法,如start()、pause()、seekTo()等。
5. 在onPause()和onDestroy()方法中释放MediaPlayer对象,以防止内存泄漏。
6. 如果需要支持播放列表,可以使用RecyclerView或ListView来展示列表。
7. 如果需要支持在线播放,可以使用Android自带的MediaPlayer或第三方库,如ExoPlayer等。
以下是一个简单的示例代码:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
private SurfaceView mSurfaceView;
private Button mBtnPlay, mBtnPause, mBtnRewind, mBtnForward;
private TextView mTvDuration, mTvCurrentTime;
private SeekBar mSeekBar;
private MediaPlayer mMediaPlayer;
private boolean mIsPlaying;
private int mDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = findViewById(R.id.surfaceView);
mBtnPlay = findViewById(R.id.btn_play);
mBtnPause = findViewById(R.id.btn_pause);
mBtnRewind = findViewById(R.id.btn_rewind);
mBtnForward = findViewById(R.id.btn_forward);
mTvDuration = findViewById(R.id.tv_duration);
mTvCurrentTime = findViewById(R.id.tv_current_time);
mSeekBar = findViewById(R.id.seekBar);
mBtnPlay.setOnClickListener(this);
mBtnPause.setOnClickListener(this);
mBtnRewind.setOnClickListener(this);
mBtnForward.setOnClickListener(this);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnErrorListener(this);
SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
if (!mIsPlaying) {
try {
mMediaPlayer.setDataSource("video.mp4");
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case R.id.btn_pause:
if (mIsPlaying) {
mMediaPlayer.pause();
mIsPlaying = false;
mBtnPlay.setText("播放");
}
break;
case R.id.btn_rewind:
int currentPosition = mMediaPlayer.getCurrentPosition();
mMediaPlayer.seekTo(currentPosition - 5000);
break;
case R.id.btn_forward:
int currentPosition = mMediaPlayer.getCurrentPosition();
mMediaPlayer.seekTo(currentPosition + 5000);
break;
}
}
@Override
public void onPrepared(MediaPlayer mp) {
mDuration = mp.getDuration();
mTvDuration.setText(formatTime(mDuration));
mSeekBar.setMax(mDuration);
mMediaPlayer.start();
mIsPlaying = true;
mBtnPlay.setText("暂停");
updateSeekBar();
}
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.seekTo(0);
mIsPlaying = false;
mBtnPlay.setText("播放");
mSeekBar.setProgress(0);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
private void updateSeekBar() {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
mTvCurrentTime.setText(formatTime(mMediaPlayer.getCurrentPosition()));
if (mIsPlaying) {
Runnable runnable = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
mSeekBar.postDelayed(runnable, 1000);
}
}
private String formatTime(int time) {
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
return formatter.format(new Date(time));
}
@Override
protected void onPause() {
super.onPause();
if (mIsPlaying) {
mMediaPlayer.pause();
mIsPlaying = false;
mBtnPlay.setText("播放");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
}
}
```
activity_main.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/surfaceView"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:id="@+id/btn_rewind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快退" />
<Button
android:id="@+id/btn_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快进" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/surfaceView"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/tv_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
</LinearLayout>
</RelativeLayout>
```
注意:以上代码仅为示例代码,实际开发中还需要处理一些异常情况,如没有SD卡或视频文件不存在等。
阅读全文