MediaPlayerAdapter无法播放视频

时间:2018-04-15 08:06:09

标签: android android-mediaplayer android-tv leanback

我正在尝试为Android tv(sdk 26)实现一个视频播放器。默认项目模板使用MediaPlayerGlue进行播放控制,并可以播放来自远程信号源的视频(通过http)。

private MediaPlayerGlue mTransportControlGlue;
...
mTransportControlGlue = new MediaPlayerGlue(getContext());

mTransportControlGlue.setMode(MediaPlayerGlue.NO_REPEAT);
mTransportControlGlue.setHost(glueHost);
mTransportControlGlue.setTitle(movie.getTitle());
mTransportControlGlue.addPlayerCallback(
        new PlaybackGlue.PlayerCallback() {
            @Override
            public void onPreparedStateChanged(PlaybackGlue glue) {
                if (glue.isPrepared()) {
                    glue.play();
                }
            }
        });
mTransportControlGlue.setVideoUrl(movie.getVideoUrl());

但是这个样板代码的限制是 - 它不支持seekbar。为了能够使用搜索栏,我将我的胶水更改为PlaybackTransportControlGlue-

private PlaybackTransportControlGlue mTransportControlGlue;
....
MediaPlayerAdapter playerAdapter = new MediaPlayerAdapter(getContext());
playerAdapter.setDataSource(Uri.parse(movie.getVideoUrl()));
mTransportControlGlue = new PlaybackTransportControlGlue(getContext(), playerAdapter);

mTransportControlGlue.setHost(glueHost);
mTransportControlGlue.setTitle(movie.getTitle());
mTransportControlGlue.addPlayerCallback(
        new PlaybackGlue.PlayerCallback() {
            @Override
            public void onPreparedStateChanged(PlaybackGlue glue) {
                if (glue.isPrepared()) {
                    glue.play();
                }
            }
        });

这两种实现有三个不同之处 -

  1. MediaPlayerGlue vs PlaybackTransportControlGlue,

  2. MediaPlayerGlue不需要提供适配器,但是 PlaybackTransportControlGlue确实

  3. 在第一个片段中我们将视频网址指定为字符串到胶水中,而在PlayBackTransportControlGlue中,我们将url 指定为URI 到适配器中,因为没有重载需要字符串。

  4. 现在问题是,第一个实现有效,但第二个没有。它发出警告 -

    W/MediaPlayer:
     Couldn't open file on client side; trying server side: java.io.FileNotFoundException:
     No content provider: http://example.com/sdj1/english-movies3/2017//A%20Mermaid's%20Tale%20(2016)%20DVDRip//A%20Mermaid's%20Tale%20(2016)%20DVDRip.avi
    

    然后抛出异常 -

     java.lang.NullPointerException:
    Attempt to invoke virtual method
    'void android.support.v17.leanback.media.PlayerAdapter$Callback.onBufferingStateChanged(android.support.v17.leanback.media.PlayerAdapter, boolean)' on a null object reference
       at android.support.v17.leanback.media.MediaPlayerAdapter.notifyBufferingStartEnd(MediaPlayerAdapter.java:129)
       at android.support.v17.leanback.media.MediaPlayerAdapter.prepareMediaForPlaying(MediaPlayerAdapter.java:362)
       at android.support.v17.leanback.media.MediaPlayerAdapter.setDataSource(MediaPlayerAdapter.java:338)
    at com.example.ftptest.CustomPlaybackVideoFragment.onCreate(CustomPlaybackVideoFragment.java:47)
    

    网址有效,在浏览器上点击它会播放视频。我也尝试了不同的格式(avi,mp4),结果相同。

    任何想法如何让我的第二个实现工作?

0 个答案:

没有答案