Skip to content

Commit d21b0ea

Browse files
authored
Merge pull request #73 from ashqal/surface-no-destroy
Preserve EGLContext OnPause
2 parents e2b331c + 6770947 commit d21b0ea

File tree

10 files changed

+168
-5
lines changed

10 files changed

+168
-5
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<category android:name="android.intent.category.LAUNCHER" />
1515
</intent-filter>
1616
</activity>
17+
<activity android:label="MDMultiDemoActivity" android:name=".IjkPlayerDemoActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
1718
<activity android:label="MDMultiDemoActivity" android:name=".VideoPlayerActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
1819
<activity android:label="MDMultiDemoActivity" android:name=".BitmapPlayerActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape"/>
1920
</application>

app/src/main/java/com/asha/md360player4android/DemoActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
3737
data.put(data.size(), "file:///mnt/sdcard/vr/stereo.mp4");
3838
data.put(data.size(), "http://10.240.131.39/vr/570624aae1c52.mp4");
3939
data.put(data.size(), "http://192.168.5.106/vr/570624aae1c52.mp4");
40+
data.put(data.size(), "http://cache.utovr.com/201508270528174780.m3u8");
4041

4142

4243
data.put(data.size(), "file:///mnt/sdcard/vr/AGSK6416.jpg");
@@ -78,6 +79,18 @@ public void onClick(View v) {
7879
}
7980
}
8081
});
82+
83+
findViewById(R.id.ijk_button).setOnClickListener(new View.OnClickListener() {
84+
@Override
85+
public void onClick(View v) {
86+
String url = et.getText().toString();
87+
if (!TextUtils.isEmpty(url)){
88+
IjkPlayerDemoActivity.start(DemoActivity.this, Uri.parse(url));
89+
} else {
90+
Toast.makeText(DemoActivity.this, "empty url!", Toast.LENGTH_SHORT).show();
91+
}
92+
}
93+
});
8194
}
8295

8396
private Uri getDrawableUri(@DrawableRes int resId){
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.asha.md360player4android;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.graphics.SurfaceTexture;
7+
import android.net.Uri;
8+
import android.os.Bundle;
9+
import android.support.annotation.Nullable;
10+
import android.view.Surface;
11+
import android.view.TextureView;
12+
import android.view.View;
13+
import android.view.Window;
14+
import android.view.WindowManager;
15+
16+
import tv.danmaku.ijk.media.player.IMediaPlayer;
17+
18+
19+
/**
20+
* Created by hzqiujiadi on 16/7/6.
21+
* hzqiujiadi [email protected]
22+
*/
23+
public class IjkPlayerDemoActivity extends Activity implements TextureView.SurfaceTextureListener {
24+
25+
private Surface surface;
26+
27+
private MediaPlayerWrapper mMediaPlayerWrapper = new MediaPlayerWrapper();
28+
29+
@Override
30+
protected void onCreate(@Nullable Bundle savedInstanceState) {
31+
super.onCreate(savedInstanceState);
32+
// no title
33+
requestWindowFeature(Window.FEATURE_NO_TITLE);
34+
35+
// full screen
36+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
37+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
38+
39+
setContentView(R.layout.activity_ijkdemo);
40+
41+
mMediaPlayerWrapper.init();
42+
mMediaPlayerWrapper.setPreparedListener(new IMediaPlayer.OnPreparedListener() {
43+
@Override
44+
public void onPrepared(IMediaPlayer mp) {
45+
cancelBusy();
46+
}
47+
});
48+
49+
TextureView textureView = (TextureView) findViewById(R.id.video_view);
50+
textureView.setSurfaceTextureListener(this);
51+
52+
Uri uri = getUri();
53+
if (uri != null){
54+
mMediaPlayerWrapper.openRemoteFile(uri.toString());
55+
mMediaPlayerWrapper.prepare();
56+
}
57+
58+
}
59+
60+
public static void start(Context context, Uri uri){
61+
Intent i = new Intent(context,IjkPlayerDemoActivity.class);
62+
i.setData(uri);
63+
context.startActivity(i);
64+
}
65+
66+
protected Uri getUri() {
67+
Intent i = getIntent();
68+
if (i == null || i.getData() == null){
69+
return null;
70+
}
71+
return i.getData();
72+
}
73+
74+
@Override
75+
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
76+
surface = new Surface(surfaceTexture);
77+
mMediaPlayerWrapper.getPlayer().setSurface(surface);
78+
79+
}
80+
81+
@Override
82+
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
83+
84+
}
85+
86+
@Override
87+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
88+
mMediaPlayerWrapper.getPlayer().setSurface(null);
89+
this.surface = null;
90+
return true;
91+
}
92+
93+
@Override
94+
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
95+
96+
}
97+
98+
@Override
99+
protected void onDestroy() {
100+
super.onDestroy();
101+
mMediaPlayerWrapper.onDestroy();
102+
}
103+
104+
@Override
105+
protected void onPause() {
106+
super.onPause();
107+
mMediaPlayerWrapper.onPause();
108+
}
109+
110+
@Override
111+
protected void onResume() {
112+
super.onResume();
113+
mMediaPlayerWrapper.onResume();
114+
}
115+
116+
public void cancelBusy(){
117+
findViewById(R.id.progress).setVisibility(View.GONE);
118+
}
119+
}

app/src/main/java/com/asha/md360player4android/MediaPlayerWrapper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public void onPrepared(IMediaPlayer mp) {
137137
}
138138

139139
public void onPause() {
140-
mPlayer.setSurface(null);
141140
pause();
142141
}
143142

app/src/main/java/com/asha/md360player4android/VideoPlayerActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public void onPrepared(IMediaPlayer mp) {
2929
cancelBusy();
3030
}
3131
});
32+
33+
mMediaPlayerWrapper.getPlayer().setOnErrorListener(new IMediaPlayer.OnErrorListener() {
34+
@Override
35+
public boolean onError(IMediaPlayer mp, int what, int extra) {
36+
String error = String.format("Play Error what=%d extra=%d",what,extra);
37+
Toast.makeText(VideoPlayerActivity.this, error, Toast.LENGTH_SHORT).show();
38+
return true;
39+
}
40+
});
41+
3242
mMediaPlayerWrapper.getPlayer().setOnVideoSizeChangedListener(new IMediaPlayer.OnVideoSizeChangedListener() {
3343
@Override
3444
public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sar_num, int sar_den) {

app/src/main/res/layout/activity_demo.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
android:id="@+id/bitmap_button"
4444
android:layout_width="match_parent"
4545
android:layout_height="wrap_content" />
46+
47+
<Button
48+
android:text="asIjkVideo"
49+
android:layout_weight="1"
50+
android:id="@+id/ijk_button"
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content" />
4653
</LinearLayout>
4754

4855
</LinearLayout>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent" android:layout_height="match_parent">
4+
<TextureView
5+
android:id="@+id/video_view"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent" />
8+
<ProgressBar
9+
android:layout_centerInParent="true"
10+
android:id="@+id/progress"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content" />
13+
</FrameLayout>

vrlib/src/main/java/com/asha/vrlib/MD360Renderer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public void onDrawFrame(GL10 glUnused){
9696

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

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

117119
object3D.draw();
120+
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
118121
}
119122

120123
}

vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private void initOpenGL(Context context, GLSurfaceView glSurfaceView, MD360Textu
122122
if (GLUtil.supportsEs2(context)) {
123123
// Request an OpenGL ES 2.0 compatible context.
124124
glSurfaceView.setEGLContextClientVersion(2);
125+
` glSurfaceView.setPreserveEGLContextOnPause(true);
125126
MD360Renderer renderer = MD360Renderer.with(context)
126127
.setTexture(texture)
127128
.setDisplayModeManager(mDisplayModeManager)
@@ -207,10 +208,6 @@ public void onPause(Context context){
207208
mGLSurfaceView.onPause();
208209
}
209210

210-
if (mTexture != null){
211-
mTexture.destroy();
212-
}
213-
214211
}
215212

216213
public void onDestroy(){

vrlib/src/main/java/com/asha/vrlib/texture/MD360VideoTexture.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected int createTextureId() {
8888
public boolean updateTexture() {
8989
int glSurfaceTexture = getCurrentTextureId();
9090
if (isEmpty(glSurfaceTexture)) return false;
91+
if (mSurfaceTexture == null) return false;
9192

9293
mSurfaceTexture.updateTexImage();
9394
return true;

0 commit comments

Comments
 (0)