您的位置:首页 > 移动开发 > Android开发

Android Mediaplayer+sufaceView

2017-11-23 11:15 197 查看
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:weightSum="1"><SurfaceView android:layout_height="220dip" android:layout_gravity="center" android:id="@+id/surface" android:layout_weight="0.25" android:layout_width="320dip"></SurfaceView><LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent"><Button android:text="播放" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="暂停" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="停止" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
</LinearLayout>public class SurfaceActivity extends Activity implements SurfaceHolder.Callback {/** Called when the activity is first created. */MediaPlayer player;SurfaceView surface;SurfaceHolder surfaceHolder;Button play,pause,stop;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);play=(Button)findViewById(R.id.button1);pause=(Button)findViewById(R.id.button2);stop=(Button)findViewById(R.id.button3);surface=(SurfaceView)findViewById(R.id.surface);surfaceHolder=surface.getHolder();  //SurfaceHolder是SurfaceView的控制接口surfaceHolder.addCallback(this);   //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接thissurfaceHolder.setFixedSize(320, 220);  //显示的分辨率,不设置为视频默认surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  //Surface类型play.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {player.start();}});
        pause.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {player.pause();}});
        stop.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {player.stop();}});}@Overridepublic void surface4000Changed(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}@Overridepublic void surfaceCreated(SurfaceHolder arg0) {//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像player=new MediaPlayer();player.setAudioStreamType(AudioManager.STREAM_MUSIC);player.setDisplay(surfaceHolder);//设置显示视频显示在SurfaceView上try {player.setDataSource("/sdcard/3.mp4");player.prepare();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void surfaceDestroyed(SurfaceHolder arg0) {// TODO Auto-generated method stub}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(player.isPlaying()){player.stop();}player.release();//Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音}}
第二种方法:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ffffff"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/filename"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="oppo.mp4"android:id="@+id/filename"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><ImageButton android:layout_width="wrap_content"android:layout_height="fill_parent"android:src="@drawable/play"android:id="@+id/play"/><ImageButton android:layout_width="wrap_content"android:layout_height="fill_parent"android:src="@drawable/pause"android:id="@+id/pause"/><ImageButton android:layout_width="wrap_content"android:layout_height="fill_parent"android:src="@drawable/stop"android:id="@+id/stop"/><ImageButton android:layout_width="wrap_content"android:layout_height="fill_parent"android:src="@drawable/reset"android:id="@+id/reset"/></LinearLayout><SurfaceView android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/surfaceview"/></LinearLayout>public class VodeoPlayActivity extends Activity {/** Called when the activity is first created. */private EditText filenamEditText;private MediaPlayer mediaPlayer;private String filename;private SurfaceView surfaceView;private final static String TAG="VodeoPlayActivity";private int prosition=0;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);filenamEditText=(EditText) this.findViewById(R.id.filename);surfaceView=(SurfaceView)this.findViewById(R.id.surfaceview);surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前surfaceView.getHolder().addCallback(new SurceCallBack());//对surface对象的状态进行监听mediaPlayer=new MediaPlayer();ButtonOnClikListiner buttonOnClikListinero=new ButtonOnClikListiner();ImageButton start=(ImageButton) this.findViewById(R.id.play);ImageButton pause=(ImageButton) this.findViewById(R.id.pause);ImageButton stop=(ImageButton) this.findViewById(R.id.stop);ImageButton replay=(ImageButton) this.findViewById(R.id.reset);start.setOnClickListener(buttonOnClikListinero);pause.setOnClickListener(buttonOnClikListinero);stop.setOnClickListener(buttonOnClikListinero);replay.setOnClickListener(buttonOnClikListinero);}private final class ButtonOnClikListiner implements View.OnClickListener{
        @Overridepublic void onClick(View v) {if(Environment.getExternalStorageState()==Environment.MEDIA_UNMOUNTED){Toast.makeText(VodeoPlayActivity.this, "sd卡不存在", Toast.LENGTH_SHORT).show();return;}filename=filenamEditText.getText().toString();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.reset:if(mediaPlayer.isPlaying()){mediaPlayer.seekTo(0);}else{play();}break;case R.id.stop:if(mediaPlayer.isPlaying()){mediaPlayer.stop();}break;}}}private void play() {try {File file=new File(Environment.getExternalStorageDirectory(),filename);mediaPlayer.reset();//重置为初始状态mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音乐流的类型mediaPlayer.setDisplay(surfaceView.getHolder());//设置video影片以surfaceviewholder播放mediaPlayer.setDataSource(file.getAbsolutePath());//设置路径mediaPlayer.prepare();//缓冲mediaPlayer.start();//播放} catch (Exception e) {Log.e(TAG, e.toString());e.printStackTrace();}}private final class SurceCallBack implements SurfaceHolder.Callback{/*** 画面修改*/@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}/*** 画面创建*/@Overridepublic void surfaceCreated(SurfaceHolder holder) {if(prosition>0&&filename!=null){play();mediaPlayer.seekTo(prosition);prosition=0;}}/*** 画面销毁*/@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if(mediaPlayer.isPlaying()){prosition=mediaPlayer.getCurrentPosition();mediaPlayer.stop();}}}}
网络获取:
String videoPath="http://baobab.kaiyanapp.com/api/v1/playUrl?vid=61069&editionType=default&source=qcloud";mediaPlayer.setDataSource(videoPath);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: