当前位置:操作系统 > 安卓/Android >>

android之VideoView和视频播放View的扩展

1.概念及扩展
  VideoView 是android 系统提供的一个媒体播放显示和控制的控件。其结构层次如下:
  原型:VideoView extends Su易做图ceView implements MediaController.MediaPlayerControl
  类结构:
      java.lang.Object
        ↳ android.view.View
          ↳ android.view.Su易做图ceView
            ↳ android.widget.VideoView
  通过VideoView 的原型可知:如果构建更为复杂和有特色个性的视频View,需要继承Su易做图ceView 和实现MediaPlayerControl接口。其中Su易做图ceView 为显示提供支持,MediaPlayerControl则为媒体控制提供了支持。
2.案例
1)VideoView案例
(我们没有管理MediaPalyer的各种状态,这些状态都让VideoView给封装了,并且,当VideoView创建的时候,MediaPalyer对象将会创建,当VideoView对象销毁的时候,MediaPlayer对象将会释放。)
布局文件
 
<?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">
<VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_centerInParent="true" />
</LinearLayout>
主程序:
public class VideoPlayer extends Activity implements MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
    public static final String TAG = "VideoPlayer";
    private VideoView mVideoView;
    private Uri mUri;
    private int mPositionWhenPaused = -1;

    private MediaController mMediaController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //Set the screen to landscape.
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mVideoView = (VideoView)findViewById(R.id.video_view);

        //Video file
        mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/1.3gp");

        //Create media controller,组件可以控制视频的播放,暂停,回复,seek等操作,不需要你实现
        mMediaController = new MediaController(this);
        mVideoView.setMediaController(mMediaController);
    }

    public void onStart() {
        // Play Video
        mVideoView.setVideoURI(mUri);
        mVideoView.start();

        super.onStart();
    }

    public void onPause() {
        // Stop video when the activity is pause.
        mPositionWhenPaused = mVideoView.getCurrentPosition();
        mVideoView.stopPlayback();

        super.onPause();
    }

    public void onResume() {
        // Resume video player
        if(mPositionWhenPaused >= 0) {
            mVideoView.seekTo(mPositionWhenPaused);
            mPositionWhenPaused = -1;
        }

        super.onResume();
    }

    public boolean onError(MediaPlayer player, int arg1, int arg2) {
        return false;
    }

    public void onCompletion(MediaPlayer mp) {
        this.finish();
    }
}
2)自定义VideoView
和VideoView实现类似,继承了Su易做图ceView并且实现了MediaPlayerControl。
public class CustomerVideoView extends Su易做图ceView implements
        MediaPlayerControl {
    private static String TAG = "customer.videoplayer";
    private boolean pause;
    private boolean seekBackward;
    private boolean seekForward;
    private Uri videoUri;
    private MediaPlayer mediaPlayer;
    private Context context;
    private OnPreparedListener onPreparedListener;
    private int videoWidth;
    private int videoHeight;
    private MediaController mediaController;
    protected Su易做图ceHolder su易做图ceHolder;
    private Callback su易做图ceHolderCallback = new Su易做图ceHolder.Callback() {
        public void su易做图ceChanged(Su易做图ceHolder holder, int format, int w,
                int h) {
        }
        public void su易做图ceCreated(Su易做图ceHolder holder) {
            su易做图ceHolder = holder;
            if (mediaPlayer != null) {
                mediaPlayer.setDisplay(su易做图ceHolder);
                resume();
            } else {
                openVideo();
            }
        }
        public void su易做图ceDestroyed(Su易做图ceHolder holder) {
            su易做图ceHolder = null;
            if (mediaController != null) {
                mediaController.hide();
            }
            release(true);
        }
    };
    private void release(boolean cleartargetstate) {
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
  

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,