'Exoplayer only showing black screen not playing the video

When I open the app empty player with black screen is showing and the play button is changing to pause button after a second or two seconds, as per below I have tried

Dependencies I have used in app gradle file

implementation 'com.google.android.exoplayer:exoplayer-core:2.9.6 
implementation 'com.google.android.exoplayer:exoplayer-dash:2.9.6'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.6'
implementation 'com.google.android.exoplayer:exoplayer:2.9.6'

MY LAYOUT FILE-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exoplayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

MainActivity class

public class MainActivity extends AppCompatActivity {

    private PlayerView playerView;
    private SimpleExoPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView=findViewById(R.id.exoplayer);
    }


    @Override
    protected void onStart() {
        super.onStart();
        player= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());

        playerView.setPlayer(player);
        DefaultDataSourceFactory dataSourceFactory=new DefaultDataSourceFactory(this,
                Util.getUserAgent(this,"exo-demo"));


        ExtractorMediaSource mediaSource=new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse("https://www.youtube.com/watch?v=uMArzRFRgvw&list=WL&index=2&t=0s"));


        player.prepare(mediaSource);
        player.setPlayWhenReady(true);

    }


    @Override
    protected void onStop() {
        super.onStop();
        playerView.setPlayer(null);
        player.release();
        player=null;
    }
}


Solution 1:[1]

private MediaSource buildMediaSource(Context context, String uri) {
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, "ishant sharma");//exoplayer-codelab
    String mediaUrl = "";
    if (uri.contains("http")) {
        HttpProxyCacheServer proxy = ReplayMeApp.getProxy(context);
        String proxyUrl = proxy.getProxyUrl(uri);
        mediaUrl = proxyUrl;
    }
    return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(mediaUrl));
}

Solution 2:[2]

The issue was that the theme used in the manifest had background defined as

<item name="android:background">@color/black</item>

which was making the video kind of invisible and instead showing a black screen. Removing this line solved the problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Buck
Solution 2 Sonu Sourav