与系列游戏一起播放离线歌曲



我无法使用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) 

任何人都可以帮助。

我发现自己答案

 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);
}

相关内容

  • 没有找到相关文章

最新更新