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 权限