android 视频开发 集成vitamio 播放rtsp m3u8 视频 有图有demo

Posted 六道对穿肠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 视频开发 集成vitamio 播放rtsp m3u8 视频 有图有demo相关的知识,希望对你有一定的参考价值。

线上效果图把:

实现了 播放 m3u8 rtsp 视频 并且 可以手势调节声音和亮度

下面两张图片 是之前的 改过的代码可以显示全屏

下面贴一下主要代码

package vitimio.my.newvitamio;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Build;
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.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

public class PlayActivity extends Activity implements Runnable 
    public static final String TAG = "PlayActivity";
    /**
     * 使用vitamio 封装的播放器

     当切换到全屏模式,有时候会出现播放的画面不是全屏的情况,

     全屏时,画面只占左半部分并出现拉伸效果,还显示不全,等等其他情况
     mVideoLayout = VideoView.VIDEO_LAYOUT_ORIGIN;//原始画面
     mVideoLayout = VideoView.VIDEO_LAYOUT_SCALE;//全屏
     mVideoLayout = VideoView.VIDEO_LAYOUT_STRETCH;//拉伸
     mVideoLayout = VideoView.VIDEO_LAYOUT_ZOOM;//裁剪

     mVideoView.setVideoLayout(mVideoLayout, 0);//第二个参数为【宽高比】,为0将自动检测
     */

    /**
     * 当前缩放模式
     */
    private int mLayout = VideoView.VIDEO_LAYOUT_STRETCH;
    private VideoView mVideoView;
    private MediaController mMediaController;
    private MyMediaController myMediaController;
    private TextView tv_loading_progress;

    private boolean VitamioRun = true;
    private ProgressBar mLoadingView;
    //String path1 =  Environment.getExternalStorageDirectory() + "/"+ "xiaomi.mp4";
    String path1 = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
    private static final int TIME = 0;
    private static final int BATTERY = 1;

    private Handler mHandler = new Handler() 
        @Override
        public void handleMessage(Message msg) 
            switch (msg.what) 
                case TIME:
                    myMediaController.setTime(msg.obj.toString());
                    break;
                case BATTERY:
                    myMediaController.setBattery(msg.obj.toString());
                    break;
            
        
    ;


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

        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = PlayActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        //设置视频解码监听
        toggleHideyBar();
        if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
            return;
        setContentView(R.layout.activity_play);

        String path = getIntent().getStringExtra("path");


        mVideoView = (VideoView) findViewById(R.id.surface_view);
        mLoadingView = (ProgressBar) findViewById(R.id.loading_progress);
        tv_loading_progress = (TextView) findViewById(R.id.tv_loading_progress);


        if (!TextUtils.isEmpty(path)) 
            path1 = path;
        

        mVideoView.setVideoPath(path1);
        mMediaController = new MediaController(this);
        myMediaController = new MyMediaController(this, mVideoView, this);
        mMediaController.show(5000);
        //mVideoView.setBufferSize(bufSize);//设缓冲大小的代码

        //mVideoView.setMediaController(mMediaController);
        mVideoView.setMediaController(myMediaController);
        mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//高画质
        mVideoView.requestFocus();
        if (mVideoView != null)
            mVideoView.setVideoLayout(mLayout, 0);
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
            @Override
            public void onCompletion(MediaPlayer mp) 
                finish();
            
        );
        mVideoView.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() 

            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) 

                tv_loading_progress.setText("正在缓冲" + percent + "%");
                if (percent >= 99) 
                    tv_loading_progress.setVisibility(View.GONE);
                 else 
                    tv_loading_progress.setVisibility(View.VISIBLE);
                

            
        );
        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() 
            /** 是否需要自动恢复播放,用于自动暂停,恢复播放 */
            private boolean needResume;

            @Override
            public boolean onInfo(MediaPlayer arg0, int arg1, int arg2) 
                switch (arg1) 
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                        //开始缓存,暂停播放
                        if (isPlaying()) 
                            PausePlayer();
                            needResume = true;
                        
                        mLoadingView.setVisibility(View.VISIBLE);
                        break;
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //缓存完成,继续播放
                        if (needResume) 
                            LogUtils.d("播放了");
                        
                        startPlayer();
                        mLoadingView.setVisibility(View.GONE);
                        break;
                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
                        //显示 下载速度
                        LogUtils.d("当前网速" + arg2 + "kb/s");
                        break;
                
                return true;
            
        );


        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() 

            public boolean onError(MediaPlayer mp, int what, int extra) 
//                LogUtil.i(TAG, " ---出错了Error: " + what + "," + extra);
//                if(isCheckButton&&!Utils.isErrorNum )
//                    LogUtil.i(TAG, " ---再次播放了: " + what + "," + extra);
//                    Utils.isErrorNum=true;
                    startSelfPlayer();
//                    ConfirmExit();
//
//
//                else
//                    LogUtil.i(TAG, " ---要报错了: " + what + "," + extra);
//                    isError = true;
//                    if (isError) 
//                        if (mVideoBuffer != null) 
//                            mVideoBuffer.setVisibility(View.GONE);
//                        
//                    
//                    errorType = what;
//                    LogUtil.i(TAG, "Error: " + what + "," + extra);
//                    if (uri != null) 
//                        ConfirmExit();
//                        // errorsInformationDialog();
//                    
//                    mHandler.sendEmptyMessage(SET_PAUSE_BUTTON);
//                

                finish();
                return true;

            

        );

        registerBoradcastReceiver();
        new Thread(this).start();

    

    private void PausePlayer() 
        if (mVideoView != null)
            mVideoView.pause();
    

    private void startPlayer() 
        if (mVideoView != null)
            mVideoView.start();
    

    private boolean isPlaying() 
        return mVideoView != null && mVideoView.isPlaying();
    

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
        //当屏幕切换时 设置全屏
        if (mVideoView != null) 
            mVideoView.setVideoLayout(mLayout, 0);
        
        super.onConfigurationChanged(newConfig);
    

    @Override
    protected void onDestroy() 
        super.onDestroy();
        try 
            VitamioRun = false;
            unregisterReceiver(batteryBroadcastReceiver);
         catch (IllegalArgumentException ex) 

        
    

    private BroadcastReceiver batteryBroadcastReceiver = new BroadcastReceiver() 
        @Override
        public void onReceive(Context context, Intent intent) 
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) 
                //获取当前电量
                int level = intent.getIntExtra("level", 0);
                //电量的总刻度
                int scale = intent.getIntExtra("scale", 100);
                //把它转成百分比
                //tv.setText("电池电量为"+((level*100)/scale)+"%");
                Message msg = new Message();
                msg.obj = (level * 100) / scale + "";
                msg.what = BATTERY;
                mHandler.sendMessage(msg);
            
        
    ;

    public void registerBoradcastReceiver() 
        //注册电量广播监听
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryBroadcastReceiver, intentFilter);

    

    @Override
    public void run() 
        // TODO Auto-generated method stub
        while (VitamioRun) 
            //时间读取线程
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String str = sdf.format(new Date());
            Message msg = new Message();
            msg.obj = str;
            msg.what = TIME;
            mHandler.sendMessage(msg);
            try 
                Thread.sleep(1000);
             catch (InterruptedException e) 
                e.printStackTrace();
            

        
    

    //隐藏状态栏
    public void toggleHideyBar() 

        // BEGIN_INCLUDE (get_current_ui_flags)
        // BEGIN_INCLUDE (get_current_ui_flags)
        // getSystemUiVisibility() gives us that bitfield.
        int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        // END_INCLUDE (get_current_ui_flags)
        // BEGIN_INCLUDE (toggle_ui_flags)
        boolean isImmersiveModeEnabled =
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
        if (isImmersiveModeEnabled) 
            Log.i(TAG, "Turning immersive mode mode off. ");
         else 
            Log.i(TAG, "Turning immersive mode mode on.");
        

        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.VERSION.SDK_INT >= 14) 
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        

        // Status bar hiding: Backwards compatible to Jellybean
        if (Build.VERSION.SDK_INT >= 16) 
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        

        // Immersive mode: Backward compatible to KitKat.
        // Note that this flag doesn't do anything by itself, it only augments the behavior
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
        // all three flags are being toggled together.
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
        // Sticky immersive mode differs in that it makes the navigation and status bars
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
        // the screen.
        if (Build.VERSION.SDK_INT >= 18) 
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        

        getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        //END_INCLUDE (set_ui_flags)
    



这个代码超过70M 了 没法上传CSDN上面 我只有发百度网盘了

http://pan.baidu.com/s/1eR31cB4

给有需要的小伙伴 如果对你有帮助 请支持下留个言 谢谢

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于android 视频开发 集成vitamio 播放rtsp m3u8 视频 有图有demo的主要内容,如果未能解决你的问题,请参考以下文章

Android视频框架 Vitamio 打造自己的万能播放器

集成Vitamio实现万能播放器

手机影音第十二天,集成vitamio万能播放器

使用 Vitamio 在 Android 中支持 Youtube 和 Vimeo 视频

玩转Android之在线视频播放控件Vitamio的使用

无法让 Vitamio VideoView 播放普通 Android VideoView 会播放的视频文件