注意【音频播放器跟视频播放器共用的是同一个MediaPlayer,所以你在测试的时候如果两个播放器交叉播放在暂停、继续播放时会有bug,这个不必在意,因为实际当中这种交叉播放场景一般没有
】
环境:android API-15
开发工具:android studio
废话不多说,直接上代码:
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.wcl.xydapp.AndroidXone">
<SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<LinearLayout android:id="@+id/LinearLayout02"
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/Button05" 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>
<SurfaceView android:id="@+id/SurfaceView01"
android:layout_width="fill_parent" android:layout_height="500px"/>
<SeekBar android:id="@+id/SeekBar02" android:layout_height="10dp"
android:layout_width="fill_parent"/>
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Button03"
android:text="播放视频"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Button07"
android:text="暂停播放"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04"/>
</LinearLayout>
</LinearLayout>
java代码:
package com.example.wcl.xydapp;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class AndroidXone extends AppCompatActivity {
/** Called when the activity is first created. */
private SeekBar skb_audio=null;
private Button btn_start_audio = null;
private Button btn_suspend_audio = null;
private Button btn_stop_audio = null;
private SeekBar skb_video=null;
private Button btn_start_video = null;
private Button btn_suspend_video = null;
private Button btn_stop_video = null;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer m = null;
private String mp3Path = "/sdcard/Music/Young_For_You.mp3";
private String mp4Path = "/sdcard/Video/mv.mp4";
private boolean isVideo=false;
private boolean isAudio=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_xone);
//----------Media控件设置---------//
m=new MediaPlayer();
//播放结束后释放MediaPlayer
m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer arg0) {
//Toast.makeText(Mplayer.this, "结束", Toast.LENGTH_LONG).show();
m.release();
}
});
btn_start_audio = (Button) this.findViewById(R.id.Button01);
btn_suspend_audio = (Button) this.findViewById(R.id.Button05);
btn_stop_audio = (Button) this.findViewById(R.id.Button02);
btn_start_audio.setOnClickListener(new ClickEvent());
btn_suspend_audio.setOnClickListener(new ClickEvent());
btn_stop_audio.setOnClickListener(new ClickEvent());
skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);
skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent());
btn_start_video = (Button) this.findViewById(R.id.Button03);
btn_suspend_video = (Button) this.findViewById(R.id.Button07);
btn_stop_video = (Button) this.findViewById(R.id.Button04);
btn_start_video.setOnClickListener(new ClickEvent());
btn_suspend_video.setOnClickListener(new ClickEvent());
btn_stop_video.setOnClickListener(new ClickEvent());
skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);
skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent());
surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);
surfaceHolder = surfaceView.getHolder();
//surfaceHolder.setFixedSize(100, 100);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/*
* 按键事件处理
*/
class ClickEvent implements View.OnClickListener{
public void onClick(View v) {
try{
if(v==btn_start_audio) {
//设置audio或video标记,以便控制各自的进度条变化
isAudio = true;
isVideo = false;
//恢复到未初始化的状态
m.reset();
//有两种方式获取资源文件:从工程的resource目录,或是指定路径;鉴于文件比较大,所以本示例均是从SD卡获取
//m=MediaPlayer.create(TestPlayer.this, R.raw.big);//读取音频
m=MediaPlayer.create(AndroidXone.this, R.raw.test);
//设置SeekBar的长度
skb_audio.setMax(m.getDuration());
//每次播放都将进度条重置为0
skb_audio.setProgress(0);
m.start(); //播放
//启动一个新线程用于更新音频的进度条
aseekth as = new aseekth();
as.start();
}else if(v==btn_suspend_audio){
if(btn_suspend_audio.getText().equals("暂停播放")){
isAudio = true;
isVideo = false;
m.pause();
btn_suspend_audio.setText("继续播放");
}else if(btn_suspend_audio.getText().equals("继续播放")){
isAudio = false;
isVideo = true;
m.start();
btn_suspend_audio.setText("暂停播放");
}
}else if(v==btn_stop_audio){
m.stop();
isAudio = false;
btn_suspend_audio.setText("暂停播放");
//设置SeekBar的长度
skb_audio.setMax(m.getDuration());
//每次播放都将进度条重置为0
skb_audio.setProgress(0);
}else if(v==btn_stop_video){
m.stop();
isVideo = false;
//设置SeekBar的长度
skb_video.setMax(m.getDuration());
//每次播放都将进度条重置为0
skb_video.setProgress(0);
btn_suspend_video.setText("暂停播放");
}else if(v==btn_suspend_video){
if(btn_suspend_video.getText().equals("暂停播放")){
m.pause();
isVideo = false;
isAudio = true;
btn_suspend_video.setText("继续播放");
}else if(btn_suspend_video.getText().equals("继续播放")){
isVideo = true;
isAudio = false;
m.start();
btn_suspend_video.setText("暂停播放");
}
}else if(v==btn_start_video) {
isVideo = true;
isAudio = false;
m.reset();
//读取视频
m=MediaPlayer.create(AndroidXone.this, R.raw.testsp);
//设置SeekBar的长度
skb_video.setMax(m.getDuration());
//每次播放都将进度条重置为0
skb_video.setProgress(0);
m.setAudioStreamType(AudioManager.STREAM_MUSIC);
m.setDisplay(surfaceHolder);//设置屏幕
// m.prepare();
m.start();
//启动一个新线程用于更新视频的进度条
vseekth vs = new vseekth();
vs.start();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
/*
* SeekBar进度改变事件
*/
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
m.seekTo(seekBar.getProgress());
}
}
/*
* 音频进度条线程处理
*/
private class aseekth extends Thread {
public void run() {
try {
while(isAudio){
if(skb_audio.getProgress() < m.getCurrentPosition()){
skb_audio.setProgress(m.getCurrentPosition());
sleep(10);
}
}
} catch (Exception e) { }
}
}
/*
* 视频进度条线程处理
*/
private class vseekth extends Thread {
public void run() {
try {
while(isVideo){
if(skb_video.getProgress() < m.getDuration()){
skb_video.setProgress(m.getCurrentPosition());
sleep(10);
}
}
} catch (Exception e) { }
}
}
}