suwu150
suwu150
采纳率44.3%
2018-08-01 04:53 阅读 1.1k

react-native访问安卓原生VideoView组件报错,错误如下所示

5

react-native访问安卓原生VideoView组件报错,错误如下所示,请问什么原因??

08-01 12:54:24.034 4321-4321/com.react_native_video I/MediaPlayer: constructor
08-01 12:54:24.038 4321-4321/com.react_native_video I/MediaPlayer: setDataSource:http://ohe65w0xx.bkt.clouddn.com/test3.mp4
08-01 12:54:24.038 4321-4321/com.react_native_video W/MediaPlayer: Couldn't open http://ohe65w0xx.bkt.clouddn.com/test3.mp4: java.io.FileNotFoundException: No content provider: http://ohe65w0xx.bkt.clouddn.com/test3.mp4
08-01 12:54:24.038 4321-4321/com.react_native_video I/MediaPlayer: setDataSource:http://ohe65w0xx.bkt.clouddn.com/test3.mp4
08-01 12:54:24.039 4321-4321/com.react_native_video V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@63789e0): Cookies: null
08-01 12:54:24.043 4321-4351/com.react_native_video V/MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@4e82499
    makeHTTP

我的环境信息如下:

     npmPackages:
      react: 16.4.1 => 16.4.1 
      react-native: 0.56.0 => 0.56.0 
    npmGlobalPackages:
      react-native-cli: 2.0.1

并且,我的视频地址
http://ohe65w0xx.bkt.clouddn.com/test3.mp4
也是可以访问到的,
下面是我的原生代码

package com.react_native_video;
import android.net.Uri;
import javax.annotation.Nullable;

import android.os.Build;
import android.widget.VideoView;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class VideoViewManager extends SimpleViewManager<VideoView> {
    @Override
    public String getName() {// 组件名称
        return "VideoView";
    }

    @Override
    protected VideoView createViewInstance(ThemedReactContext reactContext) {
        VideoView video = new VideoView(reactContext);
        return video;
    }


    @Override
    public void onDropViewInstance(VideoView view) {//对象销毁时
        super.onDropViewInstance(view);
        view.stopPlayback();//停止播放
    }

//    @ReactProp(name="source")
//    public void setSource(VideoView videoView, @Nullable String source) {
//        if (source!= null) {
//            videoView.setVideoURI(Uri.parse(source));
//            videoView.start();
//        }
//    }

    @ReactProp(name="source")
    public void setSource(VideoView videoView, @Nullable ReadableMap source) {
        if (source!= null) {
            if (source.hasKey("url")) {
                String url = source.getString("url");
                System.out.println("========================" + url);
                FLog.e(VideoViewManager.class, "url=" + url);
                HashMap<String, String> headerMap = new HashMap<>();
                if (source.hasKey("headers")) {
                    ReadableMap headers =source.getMap("headers");
                    ReadableMapKeySetIterator iter = headers.keySetIterator();
                    while(iter.hasNextKey()) {
                        String key = iter.nextKey();
                        String value = headers.getString(key);
                        FLog.e(VideoViewManager.class, key+"=" + value);
                        System.out.println("========================");
                        System.out.println(key+"=" + value);
                        System.out.println("========================");
                        headerMap.put(key, value);
                    }
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    // videoView.setVideoURI(Uri.parse(url));
                    // Uri.fromFile(new File(url)))
                    System.out.println("========+++++================");
                    System.out.println(Uri.fromFile(new File(url)));
                    System.out.println(Uri.parse(url));
                    System.out.println("========+++===================");
                    videoView.setVideoURI(Uri.parse(url), headerMap);
                } else {
                    try {
                        // Uri.fromFile(new File(your image path))
                        Method setVideoURIMethod = videoView.getClass()
                                .getMethod("setVideoURI", Uri.class, Map.class);
                        setVideoURIMethod.invoke(videoView, Uri.parse(url), headerMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("=============videoView===========");
                System.out.println(videoView);
                videoView.start();
                //设置获取焦点
                videoView.setFocusable(true);
            }
        }
    }

}



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • dabocaiqq dabocaiqq 2018-08-03 09:17
  • qq_33756493 昵称_Justin 2018-08-04 07:08

    你换个本地的视频应该可以,我记得setDatasource(),如果是在线的视频要传一个new Hashmap();

    点赞 评论 复制链接分享

相关推荐