Exoplayer活动不播放视频,而是显示空白屏幕

时间:2019-10-30 04:40:23

标签: java android hlsl exoplayer

我是android studio的新手,在开发人员指南的帮助下,我创建了此exoplayer activity,但它不播放视频,而是显示空白屏幕。我在MainActivity中创建了一个button,当我单击该按钮时,它将打开该播放器活动并播放我的hls流。请帮助

“我的玩家”活动。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();
    }
}

我的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>

主要活动

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 个答案:

答案 0 :(得分:0)

问题似乎在这两行中

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

您正在尝试通过uri进行初始化,这会导致问题。

尝试如下进行初始化

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

然后使用它

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

这应该可以解决您的问题。

答案 1 :(得分:0)

这是您要求的详细说明。

  

检查网络连接的方法

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

权限

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

用法

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