[英]How to read & play .m3u8 file in Android?
我想在應用程序中播放.ts格式文件。我有一個.m3u8擴展文件的URL http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8 。
當我從網址播放視頻時,播放效果很好。 這是從url播放視頻的代碼...
try
{
String path = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8";
Uri uri = Uri.parse(path);
videoView.setVideoURI(uri);
videoView.start();
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
我檢查過prog_index.m3u8文件是否具有.ts文件列表,並且該文件在通過url運行時可以完美運行。 但是我想要的是自己閱讀.m3u8文件,然后從那里提取.ts文件並播放這些.ts文件。
有可能嗎?
如果無法讀取.m3u8文件,我可以播放.ts文件。如果我將其存儲在本地原始文件夾中或從流中讀取。
MainActivity.java
包com.example.hlsdemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
Button startB;
VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB=(Button) findViewById(R.id.button);
startB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setVideoURI(Uri.parse("http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"));
mVideoView.setMediaController(new MediaController(MainActivity.this));
mVideoView.requestFocus();
mVideoView.postInvalidateDelayed(100);
new Thread(new Runnable() {
public void run() {
mVideoView.start();
}
}).start();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/surface_view"
android:layout_marginRight="62dp"
android:layout_marginTop="16dp"
android:text="Stop" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="34dp"
android:text="Start" />
</RelativeLayout>
並確保AndroidManifest.xml中的Internet權限
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.