Android Open Source - Viz Video Player






From Project

Back to project page Viz.

License

The source code is released under:

GNU General Public License

If you think the Android project Viz 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

/*
 * Copyright 2012-2014, First Three LLC//from  www . j a  va  2  s  .  co m
 *
 * This file is a part of Viz.
 *
 * Viz is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Viz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Viz.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.first3.viz.players;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.widget.MediaController;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.first3.viz.Preferences;
import com.first3.viz.R;
import com.first3.viz.VizApp;
import com.first3.viz.ui.VizMediaPlayer;
import com.first3.viz.utils.FragmentParent;
import com.first3.viz.utils.Log;
import com.first3.viz.provider.VizContract.Resources;
import com.first3.viz.provider.VizContract;
import com.first3.viz.utils.Utils;

public class VideoPlayer extends FragmentParent implements VizMediaPlayer.VizMediaPlayerEventListener {
    private VizMediaPlayer mVideoView;
    private EventListener mListener;
    private Uri mCurrentUri;
    private View videoLayoutView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        videoLayoutView = (View) inflater.inflate(R.layout.videoplayer, container, false);
        return videoLayoutView;
    }

    private VizMediaPlayer getVideoView() {
        if (mVideoView == null) {
            mVideoView = (VizMediaPlayer) getActivity().findViewById(R.id.videoview);
        }
        return mVideoView;
    }

    @Override
    public void onStart() {
        Log.d();
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.d();
        super.onResume();
    }

    private void resumeFromPosition(int startPosition) {
        int newpos = startPosition - 1000 > 0 ? startPosition - 1000 : startPosition;
        CharSequence text = getString(R.string.resuming_from_position) + " " +
            Utils.msecs_toReadableForm(newpos);
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
        getVideoView().seekTo(newpos);
        getVideoView().start();
    }

    private void resumeDialog(String title, final int startPosition) {
        new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.ic_launcher)
            .setTitle(VizApp.getResString(R.string.videoplayer_resumeplaying))
            .setMessage(title)
            .setPositiveButton(R.string.videoplayer_resumeplaying,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            resumeFromPosition(startPosition);
                        }
                    })
            .setNegativeButton(R.string.videoplayer_noresume,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            getVideoView().start();
                    }
                })
        .create()
        .show();
    }

    public void start(Uri video) {
        Log.d("start playing " + video);
        mCurrentUri = video;

        VizMediaPlayer vv = getVideoView();
        vv.setVizMediaPlayerEventListener(this);
        vv.setMediaController(new MediaController(getActivity()));
        vv.setVideoURI(video);
        videoLayoutView.setVisibility(View.VISIBLE);
        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d();
                saveCurrentPosition(getCurrentPosition());
                if (mListener != null) {
                    mListener.onVideoCompleted();
                }
            }
        });

        /*
         * This function could be called with a filetype URI (from the
         * downloads page) in which case we should get a null cursor.
         */
        int startPosition = 0;
        int duration = 0;
        String title = null;
        String filename = null;


        if (isResourceUri(mCurrentUri)) {
            Cursor cursor = VizApp.getResolver().query(mCurrentUri, new String[] { Resources._ID,
                    Resources.POSITION, Resources.DURATION, Resources.TITLE, Resources.FILENAME },
                    null, null, null);
            if (cursor.moveToFirst()) {
                startPosition  = cursor.getInt(cursor.getColumnIndex(Resources.POSITION));
                duration = cursor.getInt(cursor.getColumnIndex(Resources.DURATION));
                title = cursor.getString(cursor.getColumnIndex(Resources.TITLE));
                filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME));
            }
            cursor.close();
        }

        Log.d("start(position= " + Utils.msecs_toReadableForm(startPosition) + ", duration=" +
                Utils.msecs_toReadableForm(duration) + ")");

        // consider the video finished with 2 seconds left.  Who watches the
        // end?  Names, boring.
        if (duration > 0 && startPosition > 0 && startPosition < (duration - 2000)) {
            if (TextUtils.isEmpty(title)) {
                title = filename;
            }
            boolean autoResume = VizApp.getPrefs().getBoolean(Preferences.AUTO_RESUME, false);
            if (autoResume) {
                resumeFromPosition(startPosition);
            } else {
                resumeDialog(title, startPosition);
            }
        } else {
            vv.start();
        }
    }

    private boolean isResourceUri(Uri uri) {
        return mCurrentUri.getPath().contains(VizContract.PATH_RESOURCES);
    }

    public Uri getUri() {
        return mCurrentUri;
    }

    public int getCurrentPosition() {
        return getVideoView().getCurrentPosition();
    }

    public void stop() {
        Log.d("stopping playback");
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (getVideoView().isPlaying()) {
            saveCurrentPosition(getCurrentPosition());
            getVideoView().stopPlayback();
        }
        videoLayoutView.setVisibility(View.GONE);
    }

    @Override
    public void onVideoPlay() {
        Log.d();
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("bla", "Value1");
        Log.d();
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Log.d();

        saveCurrentPosition(getCurrentPosition());

        if (mListener != null) {
            mListener.onVideoPaused();
        }
    }

    @Override
    public void onVideoPause() {
        Log.d();
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    public void addEventListener(EventListener l) {
        mListener = l;
    }

    private void saveCurrentPosition(final int position) {
        final Uri uri = getUri();
        if (uri == null || !isResourceUri(uri)) {
            return;
        }

        Thread t = new Thread() {
            @Override
            public void run() {
                ContentValues map = new ContentValues();
                map.put(Resources.POSITION, position);
                VizApp.getResolver().update(uri, map, null, null);
            }
        };
        Utils.threadStart(t, "Error updating video position");
    }

    public interface EventListener {
        public void onVideoCompleted();
        public void onVideoPaused();
    }
}




Java Source Code List

com.actionbarsherlock.BuildConfig.java
com.first3.viz.Config.java
com.first3.viz.Config.java
com.first3.viz.Constants.java
com.first3.viz.Preferences.java
com.first3.viz.VersionChangeNotifier.java
com.first3.viz.VizApp.java
com.first3.viz.browser.Browser.java
com.first3.viz.browser.VizWebChromeClient.java
com.first3.viz.browser.VizWebViewClient.java
com.first3.viz.builders.BlinkxResourceBuilder.java
com.first3.viz.builders.CombinedResourceBuilder.java
com.first3.viz.builders.ContainerResourceBuilder.java
com.first3.viz.builders.DailyMotionResourceBuilder.java
com.first3.viz.builders.FlashPlayerResourceBuilder.java
com.first3.viz.builders.FunnyOrDieResourceBuilder.java
com.first3.viz.builders.GenericResourceBuilder.java
com.first3.viz.builders.GoGoAnimeResourceBuilder.java
com.first3.viz.builders.JSResourceBuilder.java
com.first3.viz.builders.LiveleakResourceBuilder.java
com.first3.viz.builders.MetacafeResourceBuilder.java
com.first3.viz.builders.NovamovResourceBuilder.java
com.first3.viz.builders.Play44ResourceBuilder.java
com.first3.viz.builders.PornHubBuilder.java
com.first3.viz.builders.RedtubeBuilder.java
com.first3.viz.builders.ResourceBuilder.java
com.first3.viz.builders.VevoResourceBuilder.java
com.first3.viz.builders.Video44ResourceBuilder.java
com.first3.viz.builders.VideoFunResourceBuilder.java
com.first3.viz.builders.VidzurResourceBuilder.java
com.first3.viz.builders.VimeoResourceBuilder.java
com.first3.viz.builders.YouruploadResourceBuilder.java
com.first3.viz.content.ContentSource.java
com.first3.viz.content.ContentSources.java
com.first3.viz.content.ContentType.java
com.first3.viz.content.ContentTypes.java
com.first3.viz.download.Container.java
com.first3.viz.download.DownloadManager.java
com.first3.viz.download.StringContainer.java
com.first3.viz.models.Favorite.java
com.first3.viz.models.Resource.java
com.first3.viz.players.VideoPlayer.java
com.first3.viz.provider.VizContract.java
com.first3.viz.provider.VizDatabase.java
com.first3.viz.provider.VizProvider.java
com.first3.viz.ui.ActivityDelegate.java
com.first3.viz.ui.DirectoryListAdapter.java
com.first3.viz.ui.DownloadDirectoryDialogPreference.java
com.first3.viz.ui.Downloads.java
com.first3.viz.ui.FastBitmapDrawable.java
com.first3.viz.ui.Favorites.java
com.first3.viz.ui.FileManager.java
com.first3.viz.ui.PinSelectorDialogFragment.java
com.first3.viz.ui.ProgressDialogFragment.java
com.first3.viz.ui.Settings.java
com.first3.viz.ui.VizMediaPlayer.java
com.first3.viz.utils.AbstractPauseHandler.java
com.first3.viz.utils.ActivityParent.java
com.first3.viz.utils.DownloadTask.java
com.first3.viz.utils.FetchContainerTask.java
com.first3.viz.utils.FragmentParent.java
com.first3.viz.utils.IOUtilities.java
com.first3.viz.utils.ImageUtilities.java
com.first3.viz.utils.Lists.java
com.first3.viz.utils.Log.java
com.first3.viz.utils.Maps.java
com.first3.viz.utils.SelectionBuilder.java
com.first3.viz.utils.StringBuffer.java
com.first3.viz.utils.TabsAdapter.java
com.first3.viz.utils.Utils.java
com.first3.viz.utils.VizUtils.java