在Android中实现ExoPlayer

时间:2017-12-21 11:40:45

标签: exoplayer

我正在尝试将ExoPlayer集成到在线流媒体音乐中。我在实施ExoPlayer时遇到困难。请帮我解决这个示例网址。

我的代码和我的网址是:

private void initialiseExoplayer() {
    // 1. Create a default TrackSelector
    Handler mainHandler = new Handler();
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
        new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

    // 2. Create the player
    exoPlayer = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector);
}

private void playMusicFromUrl(String mediaUrl) {
    Uri uri = null;

    if (exoPlayer != null) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            URL.setURLStreamHandlerFactory(new java.net.URLStreamHandlerFactory() {
                public URLStreamHandler createURLStreamHandler(String protocol) {
                    Log.e("SongService", "Asking for stream handler for protocol: '" + protocol + "'");
                    return null;
                }
            });
        }

        mediaUrl = mediaUrl.replace("http", "icy");
        URL url = null;

        try {
            url = new URL(mediaUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            uri = Uri.parse(url.toURI().toString());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }else {
        uri = Uri.parse(mediaUrl);
    }
}

2 个答案:

答案 0 :(得分:0)

我已经编写了有关如何在Exo Player中播放视频的简短演示代码。您可以找到它here

答案 1 :(得分:0)

第一:

文件->其他设置->默认设置->取消选中Offilne work

第二个添加这些依赖项:

compile 'com.google.android.exoplayer:exoplayer:2.6.0'
compile 'com.google.android.exoplayer:exoplayer-core:2.6.0'
compile 'com.google.android.exoplayer:exoplayer-dash:2.6.0'
compile 'com.google.android.exoplayer:exoplayer-hls:2.6.0'
compile 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.6.0'
compile 'com.google.android.exoplayer:exoplayer-ui:2.6.0'
private SimpleExoPlayer exoPlayer;

void playChannelUsingExoPlayer(String uri) {
    DefaultRenderersFactory defaultRenderersFactory = new DefaultRenderersFactory(getContext(), null, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF);
    DefaultTrackSelector trackSelector = new DefaultTrackSelector();
    exoPlayer = ExoPlayerFactory.newSimpleInstance(defaultRenderersFactory, trackSelector);
    String useragent = Util.getUserAgent(getContext(), /*Application_name*/);
    //ExtractorMediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(getContext(), useragent), new DefaultExtractorsFactory(), null, null);
    MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(uri), new DefaultDataSourceFactory(getContext(), useragent), new DefaultExtractorsFactory(), null, null);
    exoPlayer.prepare(mediaSource);
    exoPlayer.setPlayWhenReady(true);
}

发布:

void ReleaseExoplayer()
{
    if (exoPlayer != null)
        exoPlayer.stop();
}

在onCreate()中添加:

playChannelUsingExoPlayer(radiosItem.getURL());