现在 ,我们来学习一下让SurfaceView和MediaPlayer怎么联合使用:
先给大家看一下效果图:
布局文件:
<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:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <SurfaceView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" android:id="@+id/surfaceview" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <Button android:id="@+id/btn_start" android:text="开始" android:textColor="#ffffff" android:textSize="22sp" android:background="#F29061" android:layout_margin="3dp" android:layout_width="0dp" android:layout_weight="5" android:layout_height="match_parent" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <Button android:id="@+id/btn_pause" android:text="暂停" android:textColor="#ffffff" android:textSize="22sp" android:background="#F29061" android:layout_margin="3dp" android:layout_width="0dp" android:layout_weight="5" android:layout_height="match_parent" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <Button android:id="@+id/btn_stop" android:text="停止" android:textColor="#ffffff" android:textSize="22sp" android:background="#F29061" android:layout_margin="3dp" android:layout_width="0dp" android:layout_weight="5" android:layout_height="match_parent" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
主函数:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private SurfaceView surfaceview; private Button btn_start; private Button btn_pause; private Button btn_stop; private MediaPlayer player; SurfaceHolder holder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); holder = surfaceview.getHolder(); player = new MediaPlayer(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { //开始运行时,就直接播放 Play(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } private void initView() { surfaceview = (SurfaceView) findViewById(R.id.surfaceview); btn_start = (Button) findViewById(R.id.btn_start); btn_pause = (Button) findViewById(R.id.btn_pause); btn_stop = (Button) findViewById(R.id.btn_stop); btn_start.setOnClickListener(this); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_start: Play(); break; case R.id.btn_pause: //暂停 if(player.isPlaying()){ player.pause(); }else{ player.start(); } break; case R.id.btn_stop: //停止 if(player.isPlaying()){ player.stop(); } break; } } private void Play(){ //一定要先重置!!! player.reset(); //播放模式 player.setAudioStreamType(AudioManager.STREAM_MUSIC); try { //放视频 Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"); player.setDataSource(MainActivity.this,uri); //把视频放到surfaceview上 player.setDisplay(holder); //准备工作 player.prepare(); //开始了 player.start(); } catch (Exception e) { e.printStackTrace(); } } }