Skip to content

Instantly share code, notes, and snippets.

@Evin1-
Last active March 21, 2017 03:02
Show Gist options
  • Save Evin1-/1455d910f073c5fbd491c1a728cc8ba6 to your computer and use it in GitHub Desktop.
Save Evin1-/1455d910f073c5fbd491c1a728cc8ba6 to your computer and use it in GitHub Desktop.
Minimal MediaPlayer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<SurfaceView
android:id="@+id/a_main_surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<uses-permission android:name="android.permission.INTERNET" />
public class MainActivity extends AppCompatActivity {
private static final String VIDEO_URL = "http://techslides.com/demos/sample-videos/small.mp4";
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.a_main_surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(VIDEO_URL));
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment