如何修复错误“无法播放此视频”?(m3u8)文件

huangapple 未分类评论65阅读模式
标题翻译

How to fix error "Can't play this video"? (m3u8) file

问题

我正在使用ExoPlayer和m3u8文件进行工作。每次运行我的应用程序时,都会显示以下信息:

> 无法播放此视频

可能一切都应该是正确的,但我不知道为什么会这样说。

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=".MainActivity">


    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity文件:

    public class MainActivity extends AppCompatActivity {

    VideoView videoView;

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

        VideoView videoView = findViewById(R.id.video_view);

        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("请稍等");
        progressDialog.setCancelable(false);
        progressDialog.show();

        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        

        videoView.setVideoURI(Uri.parse("http://mos.rusiptv.net:8080/live/@celalsonat/784512/94815.m3u8"));
        videoView.start();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
            }
        });
     }
    } 

提前感谢!
英文翻译

I'm working with ExoPlayer and m3u8 file. Every time when I run my app it says that

> Can't play this video

Probably everything should be right but I don't know why it says like that

XML file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  tools:context=&quot;.MainActivity&quot;&gt;


&lt;VideoView
    android:id=&quot;@+id/video_view&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

MainActivity file:

public class MainActivity extends AppCompatActivity {

VideoView videoView;

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

    VideoView videoView = findViewById(R.id.video_view);

    final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage(&quot;Please Wait&quot;);
    progressDialog.setCancelable(false);
    progressDialog.show();

    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);



    videoView.setVideoURI(Uri.parse(&quot;http://mos.rusiptv.net:8080/live/@celalsonat/784512/94815.m3u8&quot;));
    videoView.start();

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            progressDialog.dismiss();
        }
    });
 }
} 

Thanks in advance!

答案1

得分: 0

  1. 如果您的视频文件损坏,视频播放器会显示该对话框。

  2. 如果您的视频已完成且未损坏,请尝试以下操作:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        videoView.start();
        progressDialog.dismiss();
    }
});
  1. 在 Android Studio 中,ExoPlayer 是 VideoView 的最佳替代方案。

只需在多个位置实现以下依赖项:

repositories {
   mavenCentral()
   jcenter()
}

dependencies {
    ...
    compile 'com.google.android.exoplayer:exoplayer:2.8.0'
}

然后你就完成了……
请查阅以下链接以获取更多信息:

https://github.com/google/ExoPlayer

希望这能对您有所帮助!

英文翻译

there are many reasons for that..

1.If your video is corrupted then videoplayer shows that dialog..

2.if your video is completed and not corrupted then try this,

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        videoView.start();
        progressDialog.dismiss();
    }
});

3.Exoplayer is the best alternative of videoview in android studio.

just implement this dependancy, in several places

repositories {
   mavenCentral()
   jcenter()
}


dependencies {
...
compile &#39;com.google.android.exoplayer:exoplayer:2.8.0&#39;

}

and Bam you done...
please check this for more information,

 https://github.com/google/ExoPlayer

Hope this will help you..!!!

答案2

得分: 0

以下是代码的翻译部分:

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

videoPlayer = ExoPlayerFactory.newSimpleInstance(context,trackSelector);

Handler mHandler = new Handler();

String userAgent = Util.getUserAgent(context, "APPLICATION_NAME");

DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
                userAgent, null,
                DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
                1800000,
                true);

HlsMediaSource mediaSource = new HlsMediaSource(Uri.parse(mediaUrl), dataSourceFactory, 1800000, mHandler, null);

if (mediaUrl != null) {
    videoPlayer.prepare(mediaSource);
    videoPlayer.setPlayWhenReady(true);
}
英文翻译

To play .m3u8 file use below code while initializing Exoplayer:-

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

videoPlayer = ExoPlayerFactory.newSimpleInstance(context,trackSelector);

Handler mHandler = new Handler();

String userAgent = Util.getUserAgent(context, &quot;APPLICATION_NAME&quot;);

DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
                userAgent, null,
                DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
                1800000,
                true);

HlsMediaSource mediaSource = new HlsMediaSource(Uri.parse(mediaUrl),dataSourceFactory, 1800000,mHandler, null);

if (mediaUrl != null) {
    videoPlayer.prepare(mediaSource);
    videoPlayer.setPlayWhenReady(true);
}

huangapple
  • 本文由 发表于 2020年3月16日 17:21:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/60703242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定