Skip to content

Instantly share code, notes, and snippets.

@justasm
Last active September 26, 2023 09:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justasm/722a7fcb09879decbdd8 to your computer and use it in GitHub Desktop.
Save justasm/722a7fcb09879decbdd8 to your computer and use it in GitHub Desktop.
Android video playback - using MediaPlayer and SurfaceView to play file from res/raw/.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
package com.example.videotest;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class VideoPlaybackActivity2 extends Activity {
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_playback_2);
mp = MediaPlayer.create(this, R.raw.video_file_name);
SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView1);
SurfaceHolder holder = sv.getHolder();
holder.addCallback(new Callback(){
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceCreated(SurfaceHolder holder) {
mp.setDisplay(holder);
mp.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
});
}
@Override
protected void onPause(){
super.onPause();
if(null != mp) mp.release();
mp = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment