布局:
下面有两个主类,第一个MainActivity是加播放进度条的,shipin这个是最简单的,就单独的一个播放视频
MainActivity:
package com.example.day05zhengxuanbo;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
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;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private SurfaceView surfaceView;
private Button play;
private Button pause;
private Button stop;
private MediaPlayer mediaPlayer;
private Button tiaozhuan;
private SeekBar seekBar;
private int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mediaPlayer = new MediaPlayer();
//设置surfaceView缓冲区
surfaceView.getHolder().setType((SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS));
//设置屏幕保持常亮的状态
surfaceView.getHolder().setKeepScreenOn(true);
//方法的回调
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
play();
mediaPlayer.seekTo(position);
seekBar.setProgress(position);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
int currentPosition = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
final float percent = currentPosition / (float) duration;
if (mediaPlayer.getCurrentPosition() == mediaPlayer.getDuration()) {
cancel();
mediaPlayer.stop();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar.setProgress((int) (100 * percent));
}
});
}
};
timer.schedule(timerTask, 6000, 500);
}
private void initView() {
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
tiaozhuan = (Button) findViewById(R.id.tiaozhuan);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
tiaozhuan.setOnClickListener(this);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
play();
break;
case R.id.pause:
//如果视频正在播放,就让他暂停(pause这个方法表示暂停)
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {//如果已经暂停了,在点一下就开始播放了
mediaPlayer.start();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
break;
case R.id.tiaozhuan:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
}
}
private void play() {
mediaPlayer.reset();//重置
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
//设置需要播放的视频
mediaPlayer.setDataSource(MainActivity.this, uri);
//把视频画面输出到surfaceview
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.prepare();//预准备
mediaPlayer.start();//开始播放
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onPause() {
super.onPause();
if (mediaPlayer.isPlaying()) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
}
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
效果:
shipin:
package com.example.day05zhengxuanbo;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class shipin extends Activity implements View.OnClickListener {
private SurfaceView surfaceView;
private Button play;
private Button pause;
private Button stop;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mediaPlayer = new MediaPlayer();
holder = surfaceView.getHolder();
}
private void initView() {
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
play();
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
break;
}
}
private void play() {
mediaPlayer.reset();//重置
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//以music的形式播放
try {
Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");//网络地址
mediaPlayer.setDataSource(shipin.this, uri);//设置视频源
mediaPlayer.setDisplay(surfaceView.getHolder());//绑定surfaceview
mediaPlayer.prepare();//必须有 预准备
mediaPlayer.start();//启动播放
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果:
一定不要忘了加网络权限: