用exoplayer播放离线歌曲

时间:2017-12-24 12:57:13

标签: android exoplayer exoplayer2.x

我无法使用exoplayer2.2播放离线歌曲。

这是我的代码。

 public void initializePlayer(View view) {
    playerView = view.findViewById(R.id.player_background_view);
    playerView.setPlayer(player);
    player.setPlayWhenReady(true);
    player.seekTo(currentWindow, playbackPosition);
    Uri uri = Uri.parse("file:///storage/sdcard0/Sounds/Music/song.mp3");
    MediaSource mediaSource = buildMediaSource(uri);
    player.prepare(mediaSource, true, true);
}

private MediaSource buildMediaSource(Uri uri) {
    return new ExtractorMediaSource(uri,
            new DefaultHttpDataSourceFactory("exoplayer-codelab"),
            new DefaultExtractorsFactory(), null, null);
}

我收到以下错误。

ExoPlayerImplInternal: Source error. com.google.android.exoplayer2.upstream.Loader$UnexpectedLoaderException: Unexpected ClassCastException: libcore.net.url.FileURLConnection cannot be cast to java.net.HttpURLConnection
                                                                                        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:317)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                        at java.lang.Thread.run(Thread.java:841)
                                                                                     Caused by: 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:640)
                                                                                        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:295)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                        at java.lang.Thread.run(Thread.java:841) 

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

我自己找到了答案

 public void initializePlayer(Song song, View view) {
    String source = "file:///storage/sdcard0/Sounds/Music/song.mp3";
    Context mContext = getApplicationContext();
    if (mExoPlayer == null) {
        mExoPlayer =
                ExoPlayerFactory.newSimpleInstance(
                        new DefaultRenderersFactory(this), new DefaultTrackSelector(), new
                                DefaultLoadControl());
    }
    mExoPlayer.setPlayWhenReady(true);
    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(
                    mContext, Util.getUserAgent(mContext, "Music Player"), null);
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    // The MediaSource represents the media to be played.
    MediaSource mediaSource =
            new ExtractorMediaSource(
                    Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
    mExoPlayer.prepare(mediaSource, true, true);
}