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

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

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


            }

        });



    }




}


UYOU
浏览 68回答 2
2回答

炎炎设计

问题似乎在这两行HlsMediaSource hlsMediaSource =new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);&nbsp;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);&nbsp;这应该可以解决你的问题。

幕布斯7119047

这是您的要求的详细描述。检查网络连接的方法private boolean checkConnection(Context context)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; final ConnectivityManager mConnMngr= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; if (mConnMngr!= null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NetworkInfo mNetworkInfo = mConnMngr.getActiveNetworkInfo();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mNetworkInfo != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((mNetworkInfo .getType() == ConnectivityManager.TYPE_WIFI) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else return mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }权限<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />用法if (checkConnection(context)) {&nbsp; &nbsp;play();&nbsp;} else {&nbsp; &nbsp;Toast.makeText(context,"No internet available!",Toast.LENGTH_LONG).show()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java