使用VideoView 或者SurfaceView 实现视频播放功能

在Android中,实现视频的播放有多中方式,其中用到比较多的是使用Video View 或者是surface View来实现


1、使用Video View 实现

1.1 对应的布局文件

<FrameLayout 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"
    tools:context=".VideoViewActivity" >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

</FrameLayout>

1.2 对应的code的实现

       

package com.android.bootvideo;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.view.KeyEvent;
import android.widget.VideoView;

public class VideoViewActivity extends Activity implements MediaPlayer.OnErrorListener,
        MediaPlayer.OnCompletionListener {

    private static final String TAG = "VideoViewActivity";
    private Uri mUri;
    private VideoView mVideoView;
    private final int BOOT_VIDEO_LIMIT = 20 * 1000;

    Handler mHandler = new Handler();

    private Runnable mPlayVideoRunnable = new Runnable() {
        @Override
        public void run() {
            XLog.d(TAG, "mPlayVideoRunnable, run()");
            mHandler.removeCallbacks(mPlayVideoRunnable);
            XLog.v(TAG, "playVideo Current Source is storage");
            playVideoSetUp();
        }
    };

    private Runnable mStopPlayVideoRunnable = new Runnable() {
        @Override
        public void run() {
            XLog.d(TAG, "wait 20s for bootvideo time out!");
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XLog.d(TAG, "onCreate()");
        setContentView(R.layout.activity_video_view);
        mVideoView = (VideoView) findViewById(R.id.videoView);
        playVideoSetUp();
    }

    private void playVideoSetUp() {
        XLog.i(TAG, "playVideoSetUp()");
        String path = SystemProperties.get(Util.BOOT_VIDEO_PATH);
        XLog.i(TAG, "video path: " + path);
        if (Util.bootVideoNotExist()) {
            XLog.i(TAG, path + " file not exist");
            return;
        }

        mUri = Uri.parse(path);
        mVideoView.setVideoURI(mUri);
        mVideoView.start();
        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);
        mHandler.postDelayed(mStopPlayVideoRunnable, BOOT_VIDEO_LIMIT);
    }

    private void playVideoStop() {
        XLog.i(TAG, "stopSetup()");
        mHandler.removeCallbacks(mPlayVideoRunnable);
        mHandler.removeCallbacks(mStopPlayVideoRunnable);
        mVideoView.stopPlayback();
    }

    @Override
    public void onBackPressed() {
        XLog.i(TAG, "onBackPressed()");
        return;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        XLog.i(TAG, "dispatchKeyEvent() keyCode = " + keyCode);
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_VOLUME_MUTE: {
            return super.dispatchKeyEvent(event);
        }
        default:
            break;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        XLog.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        XLog.d(TAG, "onPause ");
        super.onPause();
    }

    @Override
    protected void onStart() {
        super.onStop();
        XLog.d(TAG, "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        XLog.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playVideoStop();
        XLog.d(TAG, "onDestroy");
    }

    @Override
    public boolean onError(MediaPlayer mediaplay, int what, int extra) {
        XLog.d(TAG, "onError() framework_err " + what + " impl_err:" + extra);
        finish();
        if (extra == MediaPlayer.MEDIA_ERROR_TIMED_OUT) {
            return true;
        }
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mediaplay) {
        XLog.i(TAG, "onCompletion()");
        finish();
    }
}


2、使用surface View 实现

2.1 对应的布局文件

  

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.android.bootvideo.SurfaceViewActivity" >

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>

2.2 对应的实现

package com.android.bootvideo;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewActivity extends Activity implements
        MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener,
        MediaPlayer.OnErrorListener {

    private final String TAG = "MainActivity";
    private SurfaceView mSurfaceView = null;
    private SurfaceHolder mSurfaceHolder = null;
    private MediaPlayer mMediaPlayer = null;

    private final int BOOT_VIDEO_LIMIT = 20 * 1000;
    Handler mHandler = new Handler();
    private Runnable mStopPlayVideoRunnable = new Runnable() {
        @Override
        public void run() {
            XLog.d(TAG, "wait 20s for bootvideo time out!");
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XLog.i(TAG, "onCreate()");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_surface_view);
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(new SurfaceHolderCallback());
        playVideoSetUp();
    }

    @Override
    protected void onResume() {
        super.onResume();
        XLog.i(TAG, "onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        XLog.i(TAG, "onPause() ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playVideoStop();
        XLog.i(TAG, "onDestroy()");
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        XLog.i(TAG, "dispatchKeyEvent() keyCode = " + keyCode);
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_VOLUME_MUTE: {
            return super.dispatchKeyEvent(event);
        }
        default:
            break;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        XLog.i(TAG, "onBackPressed()");
        return;
    }

    private void playVideoSetUp() {
        XLog.i(TAG, "playVideoSetUp()");
        String path = SystemProperties.get(Util.BOOT_VIDEO_PATH);
        XLog.i(TAG, "video path: " + path);
        if (Util.bootVideoNotExist()) {
            XLog.i(TAG, path + " file not exist");
            return;
        }

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.prepare();
            mMediaPlayer.seekTo(0);
            mMediaPlayer.start();
        } catch (Exception e) {
            XLog.i(TAG, "exceptipn error!");
            finish();
        } finally {
            mHandler.postDelayed(mStopPlayVideoRunnable, BOOT_VIDEO_LIMIT);
        }
    }

    private void playVideoStop() {
        XLog.i(TAG, "stopSetup()");
        mHandler.removeCallbacks(mStopPlayVideoRunnable);
        mMediaPlayer.stop();
    }

    private class SurfaceHolderCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            XLog.i(TAG, "surfaceCreated()");
            mMediaPlayer.setDisplay(holder);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            XLog.i(TAG, "surfaceChanged()");
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            XLog.i(TAG, "surfaceDestroyed()");
        }
    }

    @Override
    public void onPrepared(MediaPlayer player) {
        XLog.i(TAG, "onPrepared()");
        int width = player.getVideoWidth();
        int height = player.getVideoHeight();
        XLog.i(TAG, "with = " + width + ",height = " + height);
        // mSurfaceView.setLayoutParams(new LinearLayout.LayoutParams(width, height));
    }

    @Override
    public void onCompletion(MediaPlayer player) {
        XLog.i(TAG, "onCompletion()");
        finish();
    }

    @Override
    public boolean onError(MediaPlayer player, int whatError, int extra) {
        XLog.i(TAG, "onError() whatError = " + whatError);
        finish();
        return false;
    }
}


3、 对应的 source code

    Media player

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值