Created
June 22, 2013 09:17
-
-
Save ErikHellman/5840101 to your computer and use it in GitHub Desktop.
A simple demo of playing a video on one SurfaceView and switching to another during playback.
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.mediaplayersurfaceswitch; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import android.view.View; | |
@SuppressWarnings("ConstantConditions") | |
public class MainActivity extends Activity { | |
private static final int PICK_VIDEO_REQUEST = 1001; | |
private static final String TAG = "SurfaceSwitch"; | |
private MediaPlayer mMediaPlayer; | |
private SurfaceHolder mFirstSurface; | |
private SurfaceHolder mSecondSurface; | |
private SurfaceHolder mActiveSurface; | |
private Uri mVideoUri; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
SurfaceView first = (SurfaceView) findViewById(R.id.firstSurface); | |
first.getHolder().addCallback(new SurfaceHolder.Callback() { | |
@Override | |
public void surfaceCreated(SurfaceHolder surfaceHolder) { | |
Log.d(TAG, "First surface created!"); | |
mFirstSurface = surfaceHolder; | |
if (mVideoUri != null) { | |
mMediaPlayer = MediaPlayer.create(getApplicationContext(), | |
mVideoUri, mFirstSurface); | |
mActiveSurface = mFirstSurface; | |
mMediaPlayer.start(); | |
} | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { | |
Log.d(TAG, "First surface destroyed!"); | |
} | |
}); | |
SurfaceView second = (SurfaceView) findViewById(R.id.secondSurface); | |
second.getHolder().addCallback(new SurfaceHolder.Callback() { | |
@Override | |
public void surfaceCreated(SurfaceHolder surfaceHolder) { | |
Log.d(TAG, "Second surface created!"); | |
mSecondSurface = surfaceHolder; | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { | |
Log.d(TAG, "Second surface destroyed!"); | |
} | |
}); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mVideoUri = null; | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (mMediaPlayer != null) { | |
mMediaPlayer.release(); | |
mMediaPlayer = null; | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == PICK_VIDEO_REQUEST && resultCode == RESULT_OK) { | |
Log.d(TAG, "Got video " + data.getData()); | |
mVideoUri = data.getData(); | |
} | |
} | |
public void doStartStop(View view) { | |
if (mMediaPlayer == null) { | |
Intent pickVideo = new Intent(Intent.ACTION_PICK); | |
pickVideo.setTypeAndNormalize("video/*"); | |
startActivityForResult(pickVideo, PICK_VIDEO_REQUEST); | |
} else { | |
mMediaPlayer.stop(); | |
mMediaPlayer.release(); | |
mMediaPlayer = null; | |
} | |
} | |
public void doSwitchSurface(View view) { | |
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { | |
mActiveSurface = mFirstSurface == mActiveSurface ? mSecondSurface : mFirstSurface; | |
mMediaPlayer.setDisplay(mActiveSurface); | |
} | |
} | |
} |
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
<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:orientation="vertical" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<Button android:layout_weight="0.2" | |
android:layout_height="0dp" | |
android:layout_width="match_parent" | |
android:onClick="doStartStop" | |
android:text="Start/Stop" /> | |
<Button android:layout_weight="0.2" | |
android:layout_height="0dp" | |
android:layout_width="match_parent" | |
android:onClick="doSwitchSurface" | |
android:text="Switch surface" /> | |
<SurfaceView android:id="@+id/firstSurface" | |
android:layout_margin="3dp" | |
android:layout_height="0dp" | |
android:layout_width="match_parent" | |
android:layout_weight="1" /> | |
<SurfaceView android:id="@+id/secondSurface" | |
android:layout_margin="3dp" | |
android:layout_height="0dp" | |
android:layout_width="match_parent" | |
android:layout_weight="1" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function setTypeAndNormalize("video/*");?