Chava Koteswara Rao
Chava Koteswara Rao

Reputation: 33

Exoplayer activity not playing video instead it's showing an empty screen

I am newbie to the android studio, with the help of the developer guide I created this exoplayer activity but it's not playing video instead it's showing an empty screen. I created a button in my MainActivity, when I click that button it should open this player activity and play my hls streaming. Please help

MY Player activity.java

    package com.example.mystream;


import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;


public class playlive extends AppCompatActivity {


    private SimpleExoPlayer player;
    private PlayerView playerView;

    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playlive);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        try {
            this.getSupportActionBar().hide();
        } catch (Exception e) {

        }


    }

    private void play() {
        SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
        playerView = findViewById(R.id.player_view);
        playerView.setPlayer(player);
        uri = Uri.parse("http://localhost:1935/live/mystream/index.m3u8");
        DataSource.Factory dataSourceFactory =
                new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "app-name"));
// Create a HLS media source pointing to a playlist uri.
        HlsMediaSource hlsMediaSource =
                new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);


        player.prepare(hlsMediaSource);
        player.setPlayWhenReady(true);
    }

    public void onStart(){
        super.onStart();
        play();
    }

    public void onStop(){
        super.onStop();
        onBackPressed();
        player.release();
    }

    public void onDestroy(){
        super.onDestroy();
        onBackPressed();
        player.release();
    }
}

My playlive .xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".playlive">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exo_buffering"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        app:resize_mode="fill"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Main activity

package com.example.mystream;


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        try {
            this.getSupportActionBar().hide();
        }catch (Exception e){

        }

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent activity2Intent = new Intent(getApplicationContext(), playlive.class);
                startActivity(activity2Intent);

            }
        });


    }



}

Upvotes: 1

Views: 7292

Answers (2)

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

Here is the detailed desc of your requirement.

Method to Check Net Connection

private boolean checkConnection(Context context) 
    {
        final ConnectivityManager mConnMngr= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (mConnMngr!= null) {
            NetworkInfo mNetworkInfo = mConnMngr.getActiveNetworkInfo();

            if (mNetworkInfo != null) {
                if ((mNetworkInfo .getType() == ConnectivityManager.TYPE_WIFI) {
                    return true;
                } else return mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
            }
        }
        return false;
    }

Permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Usage

if (checkConnection(context)) {
   play(); 
} else {
   Toast.makeText(context,"No internet available!",Toast.LENGTH_LONG).show()
}

Upvotes: 1

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

The issue seems in these two line

HlsMediaSource hlsMediaSource =new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri); 
uri= Uri.parse("http://localhost:1935/live/mystream/index.m3u8");

You are trying to pass uri before initializing which is causing the issue.

try initializing before as below

uri= Uri.parse("http://localhost:1935/live/mystream/index.m3u8");

and then use it

HlsMediaSource hlsMediaSource =new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri); 

This should solve your issue.

Upvotes: 1

Related Questions