前言
本文介绍MediaPlayer的使用。MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。 先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.zhjy.hxf.hzmediaplayerorsurfaceview.MainActivity"
tools:showIn="@layout/activity_main">
<SeekBar
android:id="@+id/SeekBar01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音频" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放" />
</LinearLayout>
<SeekBar
android:id="@+id/SeekBar02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></SeekBar>
<SurfaceView
android:id="@+id/surfaceview01"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放视频"></Button>
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放"></Button>
</LinearLayout>
</LinearLayout>
MainActivity代码:
package com.zhjy.hxf.hzmediaplayerorsurfaceview;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayerl;
private Timer mTimer;
private TimerTask mTimerTask;
private boolean isChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突
private Button btn_start_audio;
private Button btn_stop_audio;
private SeekBar skb_audio;
private Button btn_start_video;
private Button btn_stop_video;
private SeekBar skb_video;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
}
private void init() {
mMediaPlayerl = new MediaPlayer();
/**
* 播放结束之后提示
*/
mMediaPlayerl.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "结束", Toast.LENGTH_SHORT).show();
mMediaPlayerl.release();
}
});
//----------定时器记录播放进度---------//
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
if (isChanging == true){
return;
}
if (mMediaPlayerl.isPlaying()){
if (mMediaPlayerl.getVideoHeight() == 0){
skb_audio.setProgress(mMediaPlayerl.getCurrentPosition());
}else{
skb_video.setProgress(mMediaPlayerl.getCurrentPosition());
}
}
}
};
mTimer.schedule(mTimerTask, 0, 10);
btn_start_audio = (Button) this.findViewById(R.id.Button01);
btn_stop_audio = (Button) this.findViewById(R.id.Button02);
skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);
//设置监听事件
btn_start_audio.setOnClickListener(new ClickEvent());
btn_stop_audio.setOnClickListener(new ClickEvent());
skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent());
btn_start_video = (Button) this.findViewById(R.id.Button03);
btn_stop_video = (Button) this.findViewById(R.id.Button04);
skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);
//设置监听事件
btn_start_video.setOnClickListener(new ClickEvent());
btn_stop_video.setOnClickListener(new ClickEvent());
skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent());
mSurfaceView = (SurfaceView)findViewById(R.id.surfaceview01);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setFixedSize(100, 100);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/**
* 按键事件处理
*/
class ClickEvent implements View.OnClickListener{
@Override
public void onClick(View v) {
if (v == btn_start_audio){
mMediaPlayerl.reset();//恢复到未初始化的状态
mMediaPlayerl = MediaPlayer.create(MainActivity.this,R.raw.cjyx);//读取音频文件
skb_audio.setMax(mMediaPlayerl.getDuration());//设置Seekbar的长度
try {
mMediaPlayerl.prepare();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mMediaPlayerl.start();
}else if (v==btn_stop_audio || v==btn_stop_video){
mMediaPlayerl.stop();
}else if (v == btn_start_video){
mMediaPlayerl.reset();//恢复到未初始化的状态
mMediaPlayerl =MediaPlayer.create(MainActivity.this,R.raw.cjyx);//读取视频
skb_video.setMax(mMediaPlayerl.getDuration());//设置SeekBar的长度
mMediaPlayerl.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayerl.setDisplay(mSurfaceHolder);//设置屏幕
try {
mMediaPlayerl.prepare();
} catch (IOException e) {
e.printStackTrace();
}catch (IllegalStateException e) {
e.printStackTrace();
}
mMediaPlayerl.start();
}
}
}
/**
* 设置SeekBar的进度改变事件
*/
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isChanging = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMediaPlayerl.seekTo(seekBar.getProgress());
isChanging = false;
}
}
}