繁体   English   中英

如何在 Android 上玩 m3u8?

[英]How to play m3u8 on Android?

As i understood, Android 3.0 and above are able for play radio streaming m3u8 - http://developer.android.com/guide/appendix/media-formats.html

我将此链接 - http://content.mobile-tv.sky.com/content/ssna/live/ssnraudio.m3u8放入 MediaPlayer 但在 LogCat 中我得到:

06-01 09:04:44.287: INFO/LiveSession(33): onConnect 'http://content.mobile-tv.sky.com/content/ssna/live/ssnraudio.m3u8'
06-01 09:04:44.287: INFO/NuHTTPDataSource(33): connect to content.mobile-tv.sky.com:80/content/ssna/live/ssnraudio.m3u8 @0
06-01 09:04:44.747: INFO/NuHTTPDataSource(33): connect to content.mobile-tv.sky.com:80/content/ssna/live/ssnraudio.m3u8 @0
06-01 09:04:45.019: INFO/NuHTTPDataSource(33): connect to content.mobile-tv.sky.com:80/content/ssna/live/ssnraudio/ssnr_052311_071632_78731.aac @0
**06-01 09:04:45.817: ERROR/LiveSession(33): This doesn't look like a transport stream...**
06-01 09:04:45.967: INFO/HTTPLiveSource(33): input data EOS reached.

这是我的源代码:

    mp = new MediaPlayer();        
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub              
            try {

                mp.setDataSource("http://content.mobile-tv.sky.com/content/ssna/live/ssnraudio.m3u8");
                mp.prepare();
                mp.start();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    });

    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {

                mp.stop(); 
                mp.reset();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    });
}

这是我如何播放的示例。Android 中的 M3U8 流式传输

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/myVideoView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

主.java

package com.grexample.ooyalalive;

import java.net.URL;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class Main extends Activity {

    private String urlStream;
    private VideoView myVideoView;
    private URL url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vv);//***************
            myVideoView = (VideoView)this.findViewById(R.id.myVideoView);
            MediaController mc = new MediaController(this);
            myVideoView.setMediaController(mc);         
            urlStream = "http://jorgesys.net/i/irina_delivery@117489/master.m3u8";
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    myVideoView.setVideoURI(Uri.parse(urlStream)); 
                }
            });
    }
}

我看到很多人在播放.M3U8 时遇到问题,这取决于用于流式传输的编解码器和与设备的兼容性,例如某些 my.m3u8 文件仅在屏幕为 1200 x800 及更高的设备中支持。

试试ExoMedia ,流媒体就像:

emVideoView.setVideoURI(Uri.parse("https://archive.org/download/Popeye_forPresident/Popeye_forPresident_512kb.mp4"));

我与 m3u8 配合得很好。

按照此链接跟踪: http://code.google.com/p/android/issues/detail?id=14646

->

http://code.google.com/p/android/issues/detail?id=16884

->

http://code.google.com/p/android/issues/detail?id=17118

(啊!)

最后给出答案:

基本上在 Android v2.3 和 v3.0 中,使用非标准 httplive:// 方案,在 3.1 中使用 http:// 但在如何调用媒体框架中的相关方法时有一些代码解决方法。

也许你可以试试 Vitamio 插件, http://vov.io/vitamio/

Vitamio 是适用于所有 Android 设备的多媒体框架。 Vitamio 的工作方式类似于 Android 的默认 MediaPlayer,但它包含更强大的功能。 而且它是完全免费的! 网络协议

音频和视频播放支持以下网络协议:

MMS
RTSP (RTP, SDP)
HTTP progressive streaming
HTTP live streaming (M3U8), for Android 2.1+

您可以使用 FFmpegMediaPlayer:

https://github.com/wseemann/FFmpegMediaPlayer

我还搜索了很多在 Exo_player 中播放 m3u8 视频,如果我们使用普通的 exo 播放器播放 m3u8 类型的视频,它不会为此播放。 我们需要做一些改变,我做了,它对我来说很好。

在 Kotlin 中:

 private var exoPlayer: ExoPlayer? = null
    private val playbackStateListener: Player.Listener = playbackStateListener()
    private var currentItem = 0
    private var playbackPosition = 0L
    var url = ""

    //Call this method from onStart() of the Activity.
    private fun initializePlayer() {
        exoPlayer = ExoPlayer.Builder(this).build()
        videoView.player = exoPlayer
        videoView.setKeepContentOnPlayerReset(true)
        var mediaItem =  MediaItem.Builder().
        setUri("YOUR m3u8 url to play video ")
            .setMimeType(MimeTypes.APPLICATION_M3U8).build()

        exoPlayer?.let { exoPlayer ->
            exoPlayer.setMediaItem(mediaItem)
            exoPlayer.playWhenReady = true
            exoPlayer.seekTo(currentItem, 20L)
   
            exoPlayer.prepare()
        }

    }

现在你需要添加依赖,别忘了在gradle中添加依赖:

 implementation 'com.google.android.exoplayer:exoplayer-hls:2.17.1'

现在为了您的方便,我将展示 xml。

    activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">



    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/videoView"
        app:show_buffering="always"
        app:resize_mode="fit"
        app:keep_content_on_player_reset="false"
        app:use_controller="true"
        android:layout_width="match_parent"
        android:layout_height="480dp" >

    </com.google.android.exoplayer2.ui.StyledPlayerView>


</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM