Android Open Source - NativeVideoPlayerComponent Base Native Video Player Activity






From Project

Back to project page NativeVideoPlayerComponent.

License

The source code is released under:

MIT License

If you think the Android project NativeVideoPlayerComponent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.anchorer.videoplayer;
/*from  w w  w  .  j  a  v  a 2  s .  c  o m*/
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Base Activity???MediaPlayer????????????
 * ?????????UI???????????????????????????????{@link NativeMediaController.MediaControllerGenerator}???????????
 *
 * Created by Anchorer on 2014/9/23.
 */
public class BaseNativeVideoPlayerActivity extends Activity
        implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener,
        NativeMediaController.MediaPlayerControl, NativeMediaController.MediaControllerGenerator {

    private SurfaceView videoSurface;
    protected MediaPlayer mPlayer;
    protected NativeMediaController mController;
    protected Intent mIntent;
    private SurfaceHolder mVideoHolder;
    private int mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.video_native_activity);
        mIntent = getIntent();

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt("videoPosition", 0);
        }

        videoSurface = (SurfaceView) findViewById(R.id.video_surface);
        mVideoHolder = videoSurface.getHolder();
        mVideoHolder.addCallback(this);

        mController = new NativeMediaController(this);
        mController.setUIGenerator(this);
    }


    @Override
    public BaseMediaControllerHolder generateMediaController() {
        return new BaseMediaControllerHolder();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mController.show();
        return false;
    }

    // Implement SurfaceHolder.Callback
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // ????MediaPlayer??
            mPlayer = new MediaPlayer();
            // ?????????????
            mPlayer.setDataSource(this, Uri.parse(mIntent.getStringExtra("path")));
            // ??AudioStreamType
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // ??????SurfaceView
            mPlayer.setDisplay(mVideoHolder);
            // ??????????????????OnPreparedListener
            mPlayer.prepareAsync();
            // ??????????
            mPlayer.setOnPreparedListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {}

    // Implement VideoMediaCController.MediaPlayerControl
    @Override
    public void start() {
        if (mPlayer != null)
            mPlayer.start();
    }

    @Override
    public void pause() {
        if(mPlayer != null)
        mPlayer.pause();
    }

    @Override
    public int getDuration() {
        if (mPlayer != null) {
            return mPlayer.getDuration();
        } else
            return 0;
    }

    @Override
    public int getCurrentPosition() {
        if (mPlayer != null) {
            return mPlayer.getCurrentPosition();
        } else
            return 0;
    }

    @Override
    public void seekTo(int pos) {
        if (mPlayer != null) {
            mPlayer.seekTo(pos);
        }
    }

    @Override
    public boolean isPlaying() {
        return mPlayer != null && mPlayer.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public boolean isFullScreen() {
        return false;
    }

    @Override
    public void toggleFullScreen() {
    }
    // End Implement VideoMediaCController.MediaPlayerControl

    // Implement MediaPlayer.OnPreparedListener
    @Override
    public void onPrepared(MediaPlayer mp) {
        mController.setMediaPlayer(this);
        mController.setAnchorView((FrameLayout) findViewById(R.id.video_surface_container));
        mController.show();
        mPlayer.start();
        this.seekTo(mCurrentPosition);
        mController.updatePausePlay();
    }
    // End MediaPlayer.OnPreparedListener

    @Override
    protected void onPause() {
        super.onPause();
        mCurrentPosition = this.getCurrentPosition();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("videoPosition", mCurrentPosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMediaPlayer();
    }

    private void releaseMediaPlayer() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}




Java Source Code List

Anchorer.myapplication.ApplicationTest.java
org.anchorer.videoplayer.ApplicationTest.java
org.anchorer.videoplayer.BaseMediaControllerHolder.java
org.anchorer.videoplayer.BaseNativeVideoPlayerActivity.java
org.anchorer.videoplayer.NativeMediaController.java
org.anchorer.videoplayer.example.NativeVideoPlayerActivity.java
org.anchorer.videoplayer.example.StartActivity.java