使用 Google 媒体框架 Exoplayer 的视频播放器空指针异常

Video Player Null Pointer Exception using Google Media Framework Exoplayer

我正在使用 GMF 样本在我的应用程序中播放视频,我在其中获取视频标题和 url 来自直播 JSON,这是我的 [=31] 的样本=]:

{
"videos": [ 
{
"title":"Video 1",
"url":"88.mp4"
},
{
"title":"Video 2",
"url":"l5.mp4"
}
]}

这就是我的代码:

MainActivity.java:

  JSONObject jsono = new JSONObject(data);
  JSONArray jarray = jsono.getJSONArray("videos");

  for (int i = 0; i < jarray.length(); i++) {
       JSONObject object = jarray.getJSONObject(i);

       Videos video = new Videos();

       video.setTitle(object.getString("title"));
       video.setUrl(object.getString("url"));

       videosArrayList.add(video);
   }

每当我点击任何列表项播放视频时,得到 NPE,查看完整 Log:

03-17 13:14:34.667 18799-18799/com.google.googlemediaframeworkdemo.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.googlemediaframeworkdemo.demo, PID: 18799
java.lang.NullPointerException
at com.google.android.libraries.mediaframework.exoplayerextensions.RendererBuilderFactory.createRendererBuilder(RendererBuilderFactory.java:34)
at com.google.android.libraries.mediaframework.layeredvideo.LayerManager.<init>(LayerManager.java:78)
at com.google.android.libraries.mediaframework.layeredvideo.SimpleVideoPlayer.<init>(SimpleVideoPlayer.java:112)
at com.google.android.libraries.mediaframework.layeredvideo.SimpleVideoPlayer.<init>(SimpleVideoPlayer.java:81)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:388)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:434)
at com.google.googlemediaframeworkdemo.demo.adplayer.ImaPlayer.<init>(ImaPlayer.java:467)
at com.google.googlemediaframeworkdemo.demo.MainActivity.onItemClick(MainActivity.java:81)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
at android.widget.AbsListView.run(AbsListView.java:3865)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: threadid=17: interp stack at 0x56eb0000
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: init ref table
03-17 13:14:34.803 18799-18861/com.google.googlemediaframeworkdemo.demo D/dalvikvm: init mutex
03-17 13:14:34.903 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: createInstance(): Begin = 9861250
03-17 13:14:34.904 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: create Instance with :  interface com.mediatek.common.telephony.IOnlyOwnerSimSupport
03-17 13:14:34.912 18799-18861/com.google.googlemediaframeworkdemo.demo W/MediatekClassFactory: Tablet not exist!, Get obj from default class
03-17 13:14:34.916 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: create Instance from tablet library :  com.mediatek.tb.telephony.OnlyOwnerSimSupport
03-17 13:14:34.920 18799-18861/com.google.googlemediaframeworkdemo.demo D/MediatekClassFactory: createInstance(): End = 9861267

我按照我认为应该的方式编写了代码,所以我可以知道我遗漏了什么吗?我缺少什么?我该如何解决?

当您在 doInBackgroud 方法中获取响应时,您会创建 Videos objects 并将标题和 url 放入其中,但是当您创建播放器时,您尝试在调用 videosArrayList.get(i).getVideo() 时获取 Video object,但从未设置。那一点就是你的 NullPointerException。

在这部分代码中

Videos video = new Videos();

video.setTitle(object.getString("title"));
video.setUrl(object.getString("url"));

videosArrayList.add(video);

您永远不会在 Videos 对象 video 中分配 video 字段(永远不要调用 public void setVideo(Video video) { this.video = video; })。您使用默认构造函数 public Videos() { } 将字段 video 初始化为 null

要解决此问题,请添加此行

video.setVideo(new Video(object.getString("url"),VideoType.MP4)))