Android Open Source - VideoPlayer Video Playing Activity






From Project

Back to project page VideoPlayer.

License

The source code is released under:

MIT License

If you think the Android project VideoPlayer 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 net.ralphpina.famigo.videoplayer.app;
//  w  w w . j a  v a 2s  . com
import android.annotation.TargetApi;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import net.ralphpina.famigo.videoplayer.app.util.SystemUiHider;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */
public class VideoPlayingActivity extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true;

    /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

    /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider;

    private VideoView mVideoView;
    private MediaController mMediaController;
    private VideosManager mVideosManager;
    private Video mVideo;

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

        setContentView(R.layout.activity_video_playing);

        mVideosManager = VideosManager.getInstance();
        mVideo = mVideosManager.getCurrentVideo();

        setupActionBar();

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

        // Set up an instance of SystemUiHider to control the system UI for
        // this activity.
        mSystemUiHider = SystemUiHider.getInstance(this, mVideoView, HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider
                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                    public void onVisibilityChange(boolean visible) {
                        if (visible && AUTO_HIDE) {
                            // Schedule a hide().
                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        }
                    }
                });

        mVideoView.setOnCompletionListener(new PlayerOnCompleteVideo());
        mMediaController = new MediaController(this);
        mMediaController.setMediaPlayer(mVideoView);
        mMediaController.setPrevNextListeners(new NextOnClickListener(), new PreviousOnClickListener());
        mVideoView.setMediaController(mMediaController);
        mVideoView.requestFocus();

        playVideo();

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setTitle(mVideo.getTitle());
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            // TODO: If Settings has multiple levels, Up should navigate up
            // that hierarchy.
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            mSystemUiHider.hide();
        }
    };

    /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }

    private class PlayerOnCompleteVideo implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mVideo = mVideosManager.getNextVideo();
            setupActionBar();
            mVideoView.stopPlayback();
            playVideo();
        }
    }

    private class PreviousOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            mVideo = mVideosManager.getPreviousVideo();
            setupActionBar();
            mVideoView.stopPlayback();
            playVideo();
        }
    }

    private class NextOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            mVideo = mVideosManager.getNextVideo();
            setupActionBar();
            mVideoView.stopPlayback();
            playVideo();
        }
    }

    private void playVideo() {
        mVideoView.setVideoURI(Uri.parse(mVideo.getContentData()));
        mVideoView.start();
    }

}




Java Source Code List

net.ralphpina.famigo.videoplayer.app.MainActivity.java
net.ralphpina.famigo.videoplayer.app.VideoListAdapter.java
net.ralphpina.famigo.videoplayer.app.VideoListFragmentFragment.java
net.ralphpina.famigo.videoplayer.app.VideoPlayingActivity.java
net.ralphpina.famigo.videoplayer.app.VideoService.java
net.ralphpina.famigo.videoplayer.app.Video.java
net.ralphpina.famigo.videoplayer.app.VideosManager.java
net.ralphpina.famigo.videoplayer.app.util.SystemUiHiderBase.java
net.ralphpina.famigo.videoplayer.app.util.SystemUiHiderHoneycomb.java
net.ralphpina.famigo.videoplayer.app.util.SystemUiHider.java