Last active
September 26, 2023 09:14
-
-
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/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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