博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目...
阅读量:5275 次
发布时间:2019-06-14

本文共 5885 字,大约阅读时间需要 19 分钟。

简单的说下一Android的几种视频播放功能:

1、VideoView:最简单的视频播放

private void initView(View v) {        VideoView videoView = v.findViewById(R.id.vv);        String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";        videoView.setVideoPath(uri);        videoView.start();    }

VideoView全屏:

public class SplashVideoView extends VideoView {    public SplashVideoView(Context context) {        super(context);    }    public SplashVideoView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public SplashVideoView(Context context, AttributeSet attrs, int            defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int            heightMeasureSpec) {        //我们重新计算高度        int width = getDefaultSize(0, widthMeasureSpec);        int height = getDefaultSize(0, heightMeasureSpec);        setMeasuredDimension(width, height);    }    @Override    public void    setOnPreparedListener(MediaPlayer.OnPreparedListener l) {        super.setOnPreparedListener(l);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }}

2、SurfaceView+MediaPlayer 播放视频:使用方式

private SurfaceView surfaceView;    private MediaPlayer player;    private SurfaceHolder holder;    private void initView2(View v) {        surfaceView = v.findViewById(R.id.surfaceView);        String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";        player = new MediaPlayer();        try {            player.setDataSource(uri);            holder=surfaceView.getHolder();            holder.addCallback(new MyCallBack());            try {                player.prepare();            } catch (IOException e) {                e.printStackTrace();            }            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {                @Override                public void onPrepared(MediaPlayer mp) {                    player.start();                    player.setLooping(true);                }            });        } catch (IOException e) {            e.printStackTrace();        }    }    private class MyCallBack implements SurfaceHolder.Callback {        @Override        public void surfaceCreated(SurfaceHolder holder) {            player.setDisplay(holder);        }        @Override        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {        }        @Override        public void surfaceDestroyed(SurfaceHolder holder) {        }    }

3、TextureView+MediaPlaye 播放视频:使用方式;

private TextureView textureView;    private MediaPlayer player;    private void initView(View v) {        textureView = v.findViewById(R.id.textureView);        String uri="/storage/emulated/0/Download/APPNEWCARDEMO.mp4";        player = new MediaPlayer();        try {            player.setDataSource(uri);            textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {                @Override                public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {                    player.setSurface(new Surface(surfaceTexture));                    try {                        player.prepare();                    } catch (IOException e) {                        e.printStackTrace();                    }                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {                        @Override                        public void onPrepared(MediaPlayer mp) {                            player.start();                            player.setLooping(true);                        }                    });                }                @Override                public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {                }                @Override                public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {                    return true;                }                @Override                public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {                }            });        } catch (IOException e) {            e.printStackTrace();        }    }

以上是Android基本的视频播放方式,有什么不同我就先不介绍了,看项目需要;

下面介绍几种主流的视频开源项目:

1、ExoPlyer 谷歌的开源框架,使用方式:

private void initializePlayer(View v) {        playerView = (v.findViewById(R.id.video_view));        // 创建带宽        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();       // 创建轨道选择工厂        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);        // 创建轨道选择实例        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);        // 创建播放器实例        SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(getActivity(), trackSelector);        playerView.setPlayer(player);        // 创建加载数据的工厂        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getActivity(), Util.getUserAgent(getActivity(), "MyApplication"), null);        // 创建资源        Uri uri = Uri.parse("/storage/emulated/0/Download/APPNEWCARDEMO.mp4");        ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);        player.prepare(mediaSource, false, true);        player.setPlayWhenReady(true);    }

ExoPlayer项目地址:

2、JiaoZiVideoPlayer 视频播放、使用方式;

JzvdStd jzvdStd = (JzvdStd) findViewById(R.id.videoplayer);jzvdStd.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4"                            , "视频标题", Jzvd.SCREEN_WINDOW_NORMAL);jzvdStd.thumbImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"); //占位图

 

饺子饺子播放器开源地址:

3、ijkplayer

哔哩哔哩开源项目:

转载于:https://www.cnblogs.com/cuichen16/p/10785932.html

你可能感兴趣的文章
JavaScript 变量
查看>>
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>
《TCP/IP 详解 卷一》读书笔记 -----第四章 ARP
查看>>
C# Stream 和 byte[] 之间的转换
查看>>
OMG: daily scrum nine
查看>>
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>