1

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

            }
        });


    }



}

2 Answers2

1

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.

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
  • It's working but there is another problem with API 28 and API 29, my stream not playing in these 28,29 API's but working very fine for 24,25,26,27. Can you please help? I am updating error message. – Chava Koteswara Rao Oct 30 '19 at 11:17
  • 1
    @ChavaKoteswaraRao That is because of security policy updation. Try adding android:usesCleartextTraffic="true" in application tag in manifest file. – Vir Rajpurohit Oct 30 '19 at 11:38
  • OMG! it's worked like a champ. Thank you so much. you made my day and saved lots of time and pain. – Chava Koteswara Rao Oct 30 '19 at 12:21
  • Great! obliged as been able to help you. Happy Coding! – Vir Rajpurohit Oct 31 '19 at 04:51
  • I need to check internet connectivity before launching exoplayer. I have updated my code, please look at once. My goal is, If there is no internet it should display "please connect the internet" and if internet is there It should play my exoplayer. Please help. – Chava Koteswara Rao Nov 01 '19 at 00:04
  • @ChavaKoteswaraRao I have added another answer. Please check. – Vir Rajpurohit Nov 01 '19 at 03:20
1

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()
}
Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23