Skip to content

Commit

Permalink
Merge pull request #73 from ashqal/surface-no-destroy
Browse files Browse the repository at this point in the history
Preserve EGLContext OnPause
  • Loading branch information
ashqal authored Jul 6, 2016
2 parents e2b331c + 6770947 commit d21b0ea
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="MDMultiDemoActivity" android:name=".IjkPlayerDemoActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
<activity android:label="MDMultiDemoActivity" android:name=".VideoPlayerActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
<activity android:label="MDMultiDemoActivity" android:name=".BitmapPlayerActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
</application>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/asha/md360player4android/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
data.put(data.size(), "file:///mnt/sdcard/vr/stereo.mp4");
data.put(data.size(), "http://10.240.131.39/vr/570624aae1c52.mp4");
data.put(data.size(), "http://192.168.5.106/vr/570624aae1c52.mp4");
data.put(data.size(), "http://cache.utovr.com/201508270528174780.m3u8");


data.put(data.size(), "file:///mnt/sdcard/vr/AGSK6416.jpg");
Expand Down Expand Up @@ -78,6 +79,18 @@ public void onClick(View v) {
}
}
});

findViewById(R.id.ijk_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = et.getText().toString();
if (!TextUtils.isEmpty(url)){
IjkPlayerDemoActivity.start(DemoActivity.this, Uri.parse(url));
} else {
Toast.makeText(DemoActivity.this, "empty url!", Toast.LENGTH_SHORT).show();
}
}
});
}

private Uri getDrawableUri(@DrawableRes int resId){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.asha.md360player4android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import tv.danmaku.ijk.media.player.IMediaPlayer;


/**
* Created by hzqiujiadi on 16/7/6.
* hzqiujiadi [email protected]
*/
public class IjkPlayerDemoActivity extends Activity implements TextureView.SurfaceTextureListener {

private Surface surface;

private MediaPlayerWrapper mMediaPlayerWrapper = new MediaPlayerWrapper();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no title
requestWindowFeature(Window.FEATURE_NO_TITLE);

// full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_ijkdemo);

mMediaPlayerWrapper.init();
mMediaPlayerWrapper.setPreparedListener(new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
cancelBusy();
}
});

TextureView textureView = (TextureView) findViewById(R.id.video_view);
textureView.setSurfaceTextureListener(this);

Uri uri = getUri();
if (uri != null){
mMediaPlayerWrapper.openRemoteFile(uri.toString());
mMediaPlayerWrapper.prepare();
}

}

public static void start(Context context, Uri uri){
Intent i = new Intent(context,IjkPlayerDemoActivity.class);
i.setData(uri);
context.startActivity(i);
}

protected Uri getUri() {
Intent i = getIntent();
if (i == null || i.getData() == null){
return null;
}
return i.getData();
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
surface = new Surface(surfaceTexture);
mMediaPlayerWrapper.getPlayer().setSurface(surface);

}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mMediaPlayerWrapper.getPlayer().setSurface(null);
this.surface = null;
return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {

}

@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayerWrapper.onDestroy();
}

@Override
protected void onPause() {
super.onPause();
mMediaPlayerWrapper.onPause();
}

@Override
protected void onResume() {
super.onResume();
mMediaPlayerWrapper.onResume();
}

public void cancelBusy(){
findViewById(R.id.progress).setVisibility(View.GONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public void onPrepared(IMediaPlayer mp) {
}

public void onPause() {
mPlayer.setSurface(null);
pause();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public void onPrepared(IMediaPlayer mp) {
cancelBusy();
}
});

mMediaPlayerWrapper.getPlayer().setOnErrorListener(new IMediaPlayer.OnErrorListener() {
@Override
public boolean onError(IMediaPlayer mp, int what, int extra) {
String error = String.format("Play Error what=%d extra=%d",what,extra);
Toast.makeText(VideoPlayerActivity.this, error, Toast.LENGTH_SHORT).show();
return true;
}
});

mMediaPlayerWrapper.getPlayer().setOnVideoSizeChangedListener(new IMediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sar_num, int sar_den) {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
android:id="@+id/bitmap_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:text="asIjkVideo"
android:layout_weight="1"
android:id="@+id/ijk_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_ijkdemo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextureView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:layout_centerInParent="true"
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
3 changes: 3 additions & 0 deletions vrlib/src/main/java/com/asha/vrlib/MD360Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void onDrawFrame(GL10 glUnused){

// Set the OpenGL viewport to the same size as the surface.
GLES20.glViewport(itemWidth * i, 0, itemWidth, mHeight);
GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
GLES20.glScissor(itemWidth * i, 0, itemWidth, mHeight);

// Update Projection
director.updateViewport(itemWidth, mHeight);
Expand All @@ -115,6 +117,7 @@ public void onDrawFrame(GL10 glUnused){
director.shot(mProgram);

object3D.draw();
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}

}
Expand Down
5 changes: 1 addition & 4 deletions vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private void initOpenGL(Context context, GLSurfaceView glSurfaceView, MD360Textu
if (GLUtil.supportsEs2(context)) {
// Request an OpenGL ES 2.0 compatible context.
glSurfaceView.setEGLContextClientVersion(2);
` glSurfaceView.setPreserveEGLContextOnPause(true);
MD360Renderer renderer = MD360Renderer.with(context)
.setTexture(texture)
.setDisplayModeManager(mDisplayModeManager)
Expand Down Expand Up @@ -207,10 +208,6 @@ public void onPause(Context context){
mGLSurfaceView.onPause();
}

if (mTexture != null){
mTexture.destroy();
}

}

public void onDestroy(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected int createTextureId() {
public boolean updateTexture() {
int glSurfaceTexture = getCurrentTextureId();
if (isEmpty(glSurfaceTexture)) return false;
if (mSurfaceTexture == null) return false;

mSurfaceTexture.updateTexImage();
return true;
Expand Down

0 comments on commit d21b0ea

Please sign in to comment.