MediaPlayer使用,简直无敌
作者:互联网
<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
android:id="@+id/Button04"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“停止播放”>
MainActivity代码:
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);
s
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
kb_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 (vbtn_stop_audio || vbtn_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
标签:mMediaPlayerl,MediaPlayer,无敌,video,private,简直,android,btn,id 来源: https://blog.csdn.net/m0_64319298/article/details/121501174