Android 两种方式播放视频
主MainActivity的布局配置文件
这是activity_main.xml。主要出现两个按钮,按钮一通过mediaplayer播放,按钮二通过videoview播放
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shao.MainActivity" > <Button android:id="@+id/btnSurfaceView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start SurfaceView" /> <Button android:id="@+id/btnVideoView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start VideoView" /> </LinearLayout>
主Activity。用于控制两个按钮,设置布局
package com.shao; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { public Button btnSurfaceView; public Button btnVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.btnSurfaceView = (Button) findViewById(R.id.btnSurfaceView); this.btnVideoView = (Button) findViewById(R.id.btnVideoView); this.btnSurfaceView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SurfaceViewActivity.class); startActivity(intent); } }); this.btnVideoView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, VideoViewActivity.class); startActivity(intent); } }); } }
1、通过MediaPlayer
package com.shao; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class SurfaceViewActivity extends Activity { private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Callback SurfaceHolderCallbac = new Callback() { private MediaPlayer mediaPlay; @Override public void surfaceDestroyed(SurfaceHolder holder) { mediaPlay.release(); } @Override public void surfaceCreated(SurfaceHolder holder) { mediaPlay = MediaPlayer.create(SurfaceViewActivity.this, R.raw.jikexueyuan); try { mediaPlay.prepare(); } catch(Exception e) { e.printStackTrace(); } mediaPlay.setDisplay(surfaceHolder); mediaPlay.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); surfaceView = new SurfaceView(this); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(SurfaceHolderCallbac); setContentView(surfaceView); } }
需要实例CallBack属性,并且覆盖里面的方法。主要的内容还是在onCreate里面,addCallback加入了这个SurfaceHolderCallbac。
2、通过VideoView
package com.shao; import android.app.Activity; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class VideoViewActivity extends Activity { public VideoView videoView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); videoView = new VideoView(this); videoView.setVideoPath("/storage/sdcard1/jikexueyuan.mp4"); videoView.setMediaController(new MediaController(this)); videoView.start(); setContentView(videoView); } }
没啥好注意的,就是把这个VideoView 设置了setContentView里面。然后调用了一个setMediaController,这是一个系统自带的播放控制组件。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。