SurFaceView播放网络视频

主要代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private String url = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=61069&editionType=low&source=ucloud";

private Button play_btn;

private Button stop_btn;

private MySurfaceView surfaceview;

private Button sd_btn1;

private Button sd_btn2;

private Button sd_btn3;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

play_btn = (Button) findViewById(R.id.play_btn);

stop_btn = (Button) findViewById(R.id.stop_btn);

surfaceview = (MySurfaceView) findViewById(R.id.surfaceview);

//设置分辨率

surfaceview.setPivotY(320);

surfaceview.setPivotX(320);

play_btn.setOnClickListener(this);

stop_btn.setOnClickListener(this);

sd_btn1 = (Button) findViewById(R.id.sd_btn1);

sd_btn1.setOnClickListener(this);

sd_btn2 = (Button) findViewById(R.id.sd_btn2);

sd_btn2.setOnClickListener(this);

sd_btn3 = (Button) findViewById(R.id.sd_btn3);

sd_btn3.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.play_btn:

//点击播放按钮

surfaceview.playVideo(url);

break;

case R.id.stop_btn:

surfaceview.stopVideo();

break;

case R.id.sd_btn1:

surfaceview.setprogress(100);

break;

case R.id.sd_btn2:

surfaceview.setprogress(200);

break;

case R.id.sd_btn3:

surfaceview.setprogress(300);

break;

}

}

}

自定义SurfaceView

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener {

private SurfaceHolder holder;

private MediaPlayer mediaPlayer;

public MySurfaceView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

//快进

public void setprogress(int progress) {

int currentPosition = mediaPlayer.getCurrentPosition();

mediaPlayer.seekTo(currentPosition + progress);

Toast.makeText(getContext(), "快进" + progress, Toast.LENGTH_SHORT).show();

}

//暂停/播放方法

public void stopVideo() {

if (mediaPlayer.isPlaying()) {

mediaPlayer.pause();

} else {

mediaPlayer.start();

}

}

//播放视频方法

public void playVideo(String path) {

if (mediaPlayer == null) {

mediaPlayer = new MediaPlayer();

//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前

mediaPlayer.setOnCompletionListener(this);

}

try {

mediaPlayer.reset();

mediaPlayer.setDataSource(path);

mediaPlayer.setDisplay(holder);

mediaPlayer.prepareAsync();

} catch (IOException e) {

e.printStackTrace();

}

}

private void init() {

holder = this.getHolder();

//重写SurfaceHolder.Callback方法

holder.addCallback(this);

}

@Override

public void surfaceCreated(SurfaceHolder surfaceHolder) {

}

@Override

public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override

public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

if (mediaPlayer != null) {

mediaPlayer.release();

mediaPlayer = null;

}

}

//缓冲完成播放

@Override

public void onCompletion(MediaPlayer mediaPlayer) {

mediaPlayer.start();

}

}

布局


你可能感兴趣的:(SurFaceView播放网络视频)