Android Open Source - AdvancedPlayer Video View Play






From Project

Back to project page AdvancedPlayer.

License

The source code is released under:

Apache License

If you think the Android project AdvancedPlayer 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 com.huilan.library_videoplay;
/*from ww  w. j  a  v  a 2 s .  c  o m*/
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.huilan.library_videoplay.util.ParseHtmlToUrl;

import java.io.File;

/**
 * ??VideoView?????????,??????????,????,??????<br/>
 * path:?????<br/>
 * regex:?????(????,???????html?????,????????????????????????)<br/>
 */
public class VideoViewPlay extends Activity {
    private VideoView mVideoView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 116:
                    String videoUrl = (String) msg.obj;
                    if (isVideo(videoUrl)) {
                        startPlay(videoUrl);
                    } else {
                        if (isDebug()) {
                            Log.i("VideoViewPlay", "video play url = " + videoUrl);
                        }
                        Toast.makeText(getApplicationContext(), getString(R.string.video_urlerror), Toast.LENGTH_SHORT).show();
                        VideoViewPlay.this.finish();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_videoview);

        Intent intent = getIntent();
        final String path = intent.getStringExtra("url");
        final String regex = intent.getStringExtra("regex");

        if (isVideo(path)) {
            startPlay(path);
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                    regex = "(/eportal/immovableDir/lszrmzf/resource/cms/)(.{0,50})\\.flv";
                    String videoUrl = new ParseHtmlToUrl().getVideoUrl(path, regex);
                    Message msg = Message.obtain();
                    msg.what = 116;
                    msg.obj = videoUrl;
                    handler.sendMessage(msg);
                }
            }).start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler=null;
        mVideoView = null;
    }

    /**
     * FORMAT_TO_CONTENTTYPE.put( "fla", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "flv", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "wav", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "wmv", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "avi", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "rm", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "rmvb", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "3gp", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "mp4", "video" );
     * FORMAT_TO_CONTENTTYPE.put( "mov", "video" );
     */
    private boolean isVideo(String url) {
        if (TextUtils.isEmpty(url)) {
            Toast.makeText(getApplicationContext(), getString(R.string.video_urlerror), Toast.LENGTH_SHORT).show();
            return false;
        }
        String fe = getFileExtension(url);
        fe = fe.toLowerCase();
        if (fe.equals("flv") || fe.equals("mp4") || fe.equals("3gp") || fe.equals("fla")
                || fe.equals("wav") || fe.equals("wmv") || fe.equals("avi")
                || fe.equals("rm") || fe.equals("rmvb") || fe.equals("mov")) {
            return true;
        } else {
            return false;
        }

    }

    private void startPlay(String path) {

        initView();

        mVideoView.setVideoPath(path);

        MediaController controller = new MediaController(this);

        mVideoView.setMediaController(controller);

        mVideoView.start();
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                findViewById(R.id.rl_loading).setVisibility(View.GONE);
            }
        });
    }

    private String getFileExtension(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return filePath;
        }
        int extenPosi = filePath.lastIndexOf(".");
        int filePosi = filePath.lastIndexOf(File.separator);
        if (extenPosi == -1) {
            return "";
        }
        return (filePosi >= extenPosi) ? "" : filePath.substring(extenPosi + 1);
    }

    private void initView() {
        mVideoView = (VideoView) findViewById(R.id.vv_video_view);
    }

    private boolean isDebug() {
        return (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    }
}




Java Source Code List

com.huilan.library_videoplay.AdvancedPlayerSimple.java
com.huilan.library_videoplay.AdvancedPlayer.java
com.huilan.library_videoplay.VideoViewPlay.java
com.huilan.library_videoplay.sample.LightnessController.java
com.huilan.library_videoplay.sample.MainActivity.java
com.huilan.library_videoplay.sample.TextActivity.java
com.huilan.library_videoplay.util.LightnessController.java
com.huilan.library_videoplay.util.ParseHtmlToUrl.java
com.huilan.library_videoplay.util.ViewController.java
com.huilan.library_videoplay.view.FullScreenVideoView.java
com.huilan.library_videoplay.view.SuperVideoView.java