Play video on exo player, from my youtube live streaming

946 Views Asked by At

I want to broadcast my Youtube live streaming videos to my android mobile app, i.e. when i go live on Youtube that video i want to play in exo player. This is my code of exoplayer to play live video url. I have used the youtube extractor implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'

  new YouTubeExtractor(this) {
        @Override
        public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta vMeta) {
            if (ytFiles != null) {
                String url = ytFiles.get(22).getUrl();
                playVideo(url);
            }
        }
    }.extract("https://www.youtube.com/watch?v=LCfuylCR5ZQ", true, true);


 private void playVideo() {
    //Creating default track selector
    TrackSelection.Factory adaptiveTrackSelection = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
    player = ExoPlayerFactory.newSimpleInstance(
            new DefaultRenderersFactory(ExoPlayerJavaActivity.this),
            new DefaultTrackSelector(adaptiveTrackSelection),
            new DefaultLoadControl());
    playerView.setPlayer(player);
    DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(ExoPlayerJavaActivity.this,
            Util.getUserAgent(ExoPlayerJavaActivity.this, "Employee Management"), defaultBandwidthMeter);

    Uri uri = Uri.parse("https://www.youtube.com/watch?v=LCfuylCR5ZQ");
    Handler mainHandler = new Handler();
    MediaSource mediaSource = new HlsMediaSource(uri,
            dataSourceFactory, mainHandler, null);
    player.prepare(mediaSource);
    player.setPlayWhenReady(playWhenReady);
    player.addListener(new Player.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
        }

        @Override
        public void onLoadingChanged(boolean isLoading) {
        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            switch (playbackState) {
                case ExoPlayer.STATE_READY:
                    loading.setVisibility(View.GONE);
                    break;
                case ExoPlayer.STATE_BUFFERING:
                    loading.setVisibility(View.VISIBLE);
                    break;
            }
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {
        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
        }

        @Override
        public void onPositionDiscontinuity(int reason) {
        }

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
        }

        @Override
        public void onSeekProcessed() {
        }
    });
    player.seekTo(currentWindow, playbackPosition);
    player.prepare(mediaSource, true, false);
}

Still i am getting this error. I just wanted to play my channels live video on exo player.

E/ExoPlayerImplInternal: Source error.
com.google.android.exoplayer2.source.UnrecognizedInputFormatException: Input does not start with the #EXTM3U header.
    at com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser.parse(HlsPlaylistParser.java:149)
    at com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser.parse(HlsPlaylistParser.java:48)
    at com.google.android.exoplayer2.upstream.ParsingLoadable.load(ParsingLoadable.java:138)
    at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:320)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
0

There are 0 best solutions below