[英]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.