Exoplayer不在本地存储上播放本地文件

时间:2017-11-01 00:07:46

标签: java android uri exoplayer

我正在尝试在此设备中播放本地文件

String filePath="/storage/emulated/0/MyFiles/Media/Video/Xvid1509401908940.mp4"
File file=new File(filePath);
Uri localUri=Uri.fromFile(file);
exoPlayer_main.initializePlayer(localUri);

获取ClassCastException

11-01 04:45:23.356 27145-29468/com.example.com.pro_working1 E/LoadTask: Unexpected exception loading stream
                                                                         java.lang.ClassCastException: libcore.net.url.FileURLConnection cannot be cast to java.net.HttpURLConnection
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:393)
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:350)
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:192)
                                                                             at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:692)
                                                                             at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:315)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                             at java.lang.Thread.run(Thread.java:818)
11-01 04:45:23.357 27145-29468/com.example.com.pro_working1 E/LoadTask: Unexpected exception loading stream
                                                                         java.lang.ClassCastException: libcore.net.url.FileURLConnection cannot be cast to java.net.HttpURLConnection
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:393)
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:350)
                                                                             at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:192)
                                                                             at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:692)
                                                                             at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:315)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                             at java.lang.Thread.run(Thread.java:818)

我用于初始化exoplayer的ExoPlayer方法

 public void initializePlayer(Uri uri){
        Log.d(TAG,"Init Player Calling");

        if (player==null){
            Log.d(TAG,"Player Is Null Setting Up Player");
            player= ExoPlayerFactory.newSimpleInstance(
                    new DefaultRenderersFactory(context),
                    new DefaultTrackSelector(),
                    new DefaultLoadControl());
            playerView.setPlayer(player);
            player.addListener(componentListener);
            player.setPlayWhenReady(playWhenReady);
            player.seekTo(currentWindow,playbackPosition);

            MediaSource mediaSource=buildMediaSource(uri);
            player.prepare(mediaSource,true,false);
        }
    }

    private MediaSource buildMediaSource(Uri uri){
        return new ExtractorMediaSource(uri,
                new DefaultHttpDataSourceFactory("ua"),
                new DefaultExtractorsFactory(),null,null);

    }

我尝试Uri.parse(path)并按上述方式提交,但它仍然给我ClassCastException。请指导我如何在exoplayer中播放本地存储文件。

1 个答案:

答案 0 :(得分:6)

我从谷歌官方Exoplayer存储库中得到了答案

  

DefaultDataSource支持本地和Http源。它会自动检测使用哪一个。

所以我改变了

private MediaSource buildMediaSource(Uri uri){
        return new ExtractorMediaSource(uri,
                new DefaultHttpDataSourceFactory("ua"),
                new DefaultExtractorsFactory(),null,null);

    }

到这个

private MediaSource buildMediaSource(Uri uri){    
        return new ExtractorMediaSource(uri,
                new DefaultDataSourceFactory(context,"ua"),
                new DefaultExtractorsFactory(),null,null);

    }

DefaultDataSourceFactory自动检测流式传输或本地存储文件,其中DefaultHttpDataSourceFactory仅适用于流请求。

更新:随着新版本的推出(2.9.0)。某些类也被弃用了媒体资源。我正在使用新版媒体源,几乎播放所有类型的文件(MP3,MP4,AVI等)

 private MediaSource buildMediaSourceNew(Uri uri,int buildType){    
                DataSource.Factory datasourceFactroy = new DefaultDataSourceFactory(context, Util.getUserAgent(context,"Your App Name"));
                return new ExtractorMediaSource.Factory(datasourceFactroy).createMediaSource(uri);
}