Android Open Source - AdvancedPlayer Advanced Player Simple






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   www . ja  v  a  2s . c  o m
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.huilan.library_videoplay.util.LightnessController;
import com.huilan.library_videoplay.util.ViewController;
import com.huilan.library_videoplay.view.SuperVideoView;

import java.util.Formatter;
import java.util.Locale;

/**
 * ??SurfaceView??????,???
 * path:?????
 * ????????  0--255
 * ?????  0--15
 */
public class AdvancedPlayerSimple extends Activity implements OnClickListener, SuperVideoView.PlayController, SeekBar.OnSeekBarChangeListener {
    private int screenWidth;
    private int screenHeight;
    private float startY;
    private AudioManager mAudioManager;
    /**
     * ??????????
     */
    private int minMetrics;
    private ImageButton playOrPause;
    private SuperVideoView superVideoView;
    private SeekBar progress;
    private Button back;
    private TextView videoName;
    private TextView nowTime;
    private TextView totalTime;
    private StringBuilder mFormatBuilder;
    private Formatter mFormatter;
    private View videoTop;
    private View videoBottom;
    private int state;
    private boolean isShow = true;
    private boolean isClick = true;
    /**
     * 0:??,1:??
     */
    private int mWhich;
    private boolean isStartController;
    private boolean isFullScreen;
    private int initializeVoice;
    private int initializeLight;
    private View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    handler.removeMessages(101);
                    float startX = event.getX();
                    startY = event.getY();
                    if (startX > screenWidth / 2) {
                        mWhich = 1;
                    } else {
                        mWhich = 0;
                    }
                    isStartController = false;
                    initializeVoice = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                    initializeLight = LightnessController.getLightness(getApplicationContext());
                    break;
                case MotionEvent.ACTION_MOVE:

                    float currentY = event.getY();
                    float deltaY;
                    if (isStartController) {
                        deltaY = startY - currentY;
                        if (mWhich == 0) {
                            lightChange(deltaY);
                        } else {
                            volumeChange(deltaY);
                        }
                    } else {
                        deltaY = startY - currentY;
                        float absDeltaY = Math.abs(deltaY);
                        if (absDeltaY > minMetrics) {
                            isStartController = true;
                            isClick = false;
                        } else {
                            isClick = true;
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isStartController = false;
                    if (isClick) {
                        autoShowOrHiden();
                        if (isShow) {
                            handler.sendEmptyMessageDelayed(101, 5000);
                        }
                    }
                    break;
            }
            return true;
        }
    };
    private String TAG = "AdvancedPlayer>>";
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 101:
                    autoShowOrHiden();
                    break;
            }
        }
    };
    private int maxVolume;
    private ViewController viewControllerLight;
    private ViewController viewControllerVolume;
    private boolean isLoading=true;
    private View loadingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_advancedplayer_simple);
        String path = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(path)) {
            if (isDebug()) {
                Log.i(TAG, "video play url = " + path);
            }
            Toast.makeText(getApplicationContext(), getString(R.string.video_urlerror), Toast.LENGTH_SHORT).show();
            return;
        }
        initView();

        superVideoView.setOnProgressListener(this);

        superVideoView.setVideoPath(path);

        superVideoView.start();
        handler.sendEmptyMessageDelayed(101, 5000);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
        minMetrics = (int) (20 * displayMetrics.density + 0.5f);

        superVideoView.setOnTouchListener(touchListener);

        if (LightnessController.isAutoBrightness(getApplicationContext())) {
            LightnessController.stopAutoBrightness(getApplicationContext());
        }
        initializeVoice = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        initializeLight = LightnessController.getLightness(getApplicationContext());
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        Log.i(TAG, screenWidth + "," + screenHeight + "," + minMetrics + "," + initializeLight + "," + initializeVoice);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LightnessController.startAutoBrightness(getApplicationContext());
        if (superVideoView != null) {
            superVideoView.onDestroy();
            superVideoView = null;
        }
        if (viewControllerVolume != null) {
            viewControllerVolume.cancel();
            viewControllerVolume = null;
        }
        if (viewControllerLight != null) {
            viewControllerLight.cancel();
            viewControllerLight = null;
        }

    }

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

    private void initView() {
        superVideoView = (SuperVideoView) findViewById(R.id.vv_video_view);
        back = (Button) findViewById(R.id.btn_back);
        playOrPause = (ImageButton) findViewById(R.id.btn_PlayOrPause);
        progress = (SeekBar) findViewById(R.id.seekBar_progress);

        videoName = (TextView) findViewById(R.id.tv_name);
        nowTime = (TextView) findViewById(R.id.tv_nowTime);
        totalTime = (TextView) findViewById(R.id.tv_totalTime);
        loadingView = findViewById(R.id.pb_loadingProgressBar);

        playOrPause.setOnClickListener(this);
        back.setOnClickListener(this);
        progress.setOnSeekBarChangeListener(this);

        videoTop = findViewById(R.id.in_videoTop);
        videoBottom = findViewById(R.id.in_videoBottom);

        mFormatBuilder = new StringBuilder();
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

        progress.setMax(1000);
        superVideoView.setOnTouchListener(touchListener);
    }

    /**
     * ????????????????
     */
    private synchronized void autoShowOrHiden() {
        if (state != 0) {
            return;
        }
        Animation topAnimation;
        Animation bottomAnimation;
        if (isShow) {//??
            isFullScreen = true;
            topAnimation = animationFactory(0, 0, 0, -1, 500);
            bottomAnimation = animationFactory(0, 0, 0, 1, 500);
        } else {//??
            isFullScreen = false;
            topAnimation = animationFactory(0, 0, -1, 0, 500);
            bottomAnimation = animationFactory(0, 0, 1, 0, 500);
            handler.sendEmptyMessageDelayed(101, 5000);
        }
        isShow = !isShow;
        topAnimation.setFillAfter(true);
        bottomAnimation.setFillAfter(true);
        topAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                state += 1;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                if (!isShow) {
//                    setFullScreen(isFullScreen);
//                }
                state -= 1;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        bottomAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                state += 1;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                state -= 1;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        videoTop.startAnimation(topAnimation);
        videoBottom.startAnimation(bottomAnimation);
//        if (isShow) {
//            setFullScreen(isFullScreen);
//        }
    }

    private Animation animationFactory(float fromX, float toX, float fromY, float toY, int duration) {
        Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX, Animation.RELATIVE_TO_SELF,
                toX, Animation.RELATIVE_TO_SELF, fromY, Animation.RELATIVE_TO_SELF, toY);
        animation.setDuration(duration);
        return animation;
    }


    public void setFullScreen(boolean enable) {
        if (enable) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_back) {
            this.finish();
        } else if (i == R.id.btn_PlayOrPause) {
            if (superVideoView.isPlaying()) {
                superVideoView.pause();
                playOrPause.setBackgroundResource(android.R.drawable.ic_media_play);
            } else {
                superVideoView.start();
                playOrPause.setBackgroundResource(android.R.drawable.ic_media_pause);
            }
        }
    }

    @Override
    public void currentProgress(int current, int total) {
        if (current > 0 && current < total) {
            progress.setMax(total);
            progress.setProgress(current);
            int bufferPercentage = superVideoView.getBufferPercentage();
            progress.setSecondaryProgress(bufferPercentage * total / 100);
            nowTime.setText(stringForTime(current));
            totalTime.setText(stringForTime(total));
            if (isLoading) {
                loadingView.setVisibility(View.GONE);
                isLoading = false;
            }
//            Log.i(TAG, "????:" + bufferPercentage);
        } else if (current == total) {
            superVideoView.seekTo(0);
            playOrPause.setImageResource(android.R.drawable.ic_media_play);
        }
    }

    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    private void setProgress(int current, int total) {
        if (progress != null) {
            if (total > 0) {
                // use long to avoid overflow
                long pos = 1000L * current / total;
                progress.setProgress((int) pos);
            }
            int percent = superVideoView.getBufferPercentage();
            progress.setSecondaryProgress(percent * 10);
        }

        if (totalTime != null)
            totalTime.setText(stringForTime(total));
        if (nowTime != null)
            nowTime.setText(stringForTime(current));
    }

    /**
     * ????
     */
    private void volumeChange(float deltaY) {

        int delta = (int) ((deltaY / screenHeight) * maxVolume);
        int volume;
        if (deltaY > 0) {
            volume = Math.min(initializeVoice + delta, maxVolume);
        } else {
            volume = Math.max(initializeVoice + delta, 0);
        }
        Log.i(TAG, "??=" + volume + "," + delta + "," + (int) deltaY);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
        if (viewControllerVolume == null) {
            viewControllerVolume = new ViewController(getApplicationContext(), R.drawable.v_supreme_brightness, 15);
        }
        viewControllerVolume.show(volume);
    }

    /**
     * ????
     */
    private void lightChange(float deltaY) {
        int delta = (int) (deltaY * 255 / screenHeight);
        int tLight = initializeLight + delta;
        tLight = tLight < 0 ? 0 : (tLight > 255 ? 255 : tLight);
        LightnessController.setLightness(this, tLight);
        Log.i(TAG, "??=" + tLight + "," + delta + "," + (int) deltaY);
        if (viewControllerLight == null) {
            viewControllerLight = new ViewController(getApplicationContext(), R.drawable.v_supreme_volume, 255);
        }
        viewControllerLight.show(tLight);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.i(TAG, "onProgressChanged-->" + progress + "," + fromUser);
        if (fromUser) {
            superVideoView.seekTo(progress);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Log.i(TAG, "onStartTrackingTouch-->" + seekBar);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Log.i(TAG, "onStartTrackingTouch-->" + seekBar);
    }

}




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