自定义VideoView播放网络视频

问题

使用VideoView播放网络视频,需要使用cookie进行验证,但是setVideoURI (Uri uri, Map< String, String > headers)需要api21以上才行。

image

解决

在下面找到了解决办法,使用自定义Videoview来实现在低版本中加入cookie。
http://stackoverflow.com/questions/17730034/custom-videoview-to-send-custom-headers


代码下载

里面包括两个类VideoView和Metadata两个类,可以正常使用setVideoURI (Uri uri, Map< String, String > headers)。如果缺少字符串资源,可以自己添加

<string name="VideoView_error_title">Cannot play video</string>
    <string name="VideoView_error_text_invalid_progressive_playback">Sorry, this video is not valid for streaming to
        this device.
    </string>
    <string name="VideoView_error_text_unknown">Sorry, this video cannot be played.</string>
    <string name="VideoView_error_button">OK</string>
    <string name="mediacontroller_play_pause">Play/Pause</string>

代码中添加cookie

Map<String, String> headers = new HashMap<String, String>();
        headers.put("Cookie", URLs.getCookei(this));
        aVideoView= (com.example.shuili.VideoView) findViewById(R.id.video);
        aVideoView.requestFocus();
        aVideoView.setVideoURI(Uri.parse(path),headers);

        MediaController mc = new MediaController(this);
        aVideoView.setMediaController(mc);
        aVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值