直接上代码
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.net.Uri;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.ViewGroup;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import static android.opengl.GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
/**
* Created by Administrator on 2016/10/11 0011.
*/
public class VRActivity extends Activity {
static GLSurfaceView glSurfaceView;
CustomRenderer customRenderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
setContentView(glSurfaceView);
glSurfaceView.setEGLContextClientVersion(2);
final MyRenderer renderer = new MyRenderer();
glSurfaceView.setRenderer(renderer);
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
new Thread(new Runnable() {
@Override
public void run() {
final MediaPlayerWrapper mediaPlayerWrapper = new MediaPlayerWrapper();
mediaPlayerWrapper.init();
mediaPlayerWrapper.setPreparedListener(new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
mp.setOnPreparedListener(null);
}
});
mediaPlayerWrapper.getPlayer().setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayerWrapper.getPlayer().setSurface(renderer.getSurface());
mediaPlayerWrapper.openRemoteFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/UPDATE/4.mp4");
mediaPlayerWrapper.prepare();
}
}).start();
}
static class MyRenderer implements GLSurfaceView.Renderer {
private static final String VERTEX_SHADER = "uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"attribute vec2 a_texCoord;" +
"varying vec2 v_texCoord;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
" v_texCoord = a_texCoord;" +
"}";
private static final String FRAGMENT_SHADER = "#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;" +
"varying vec2 v_texCoord;" +
"uniform samplerExternalOES s_texture;" +
"void main() {" +
" gl_FragColor = texture2D( s_texture, v_texCoord );" +
"}";
private static final float[] VERTEX = { // in counterclockwise order:
1, 1, 0, // top right
-1, 1, 0, // top left
-1, -1, 0, // bottom left
1, -1, 0, // bottom right
};
private static final short[] VERTEX_INDEX = {0, 1, 2, 2, 0, 3};
private static final float[] UV_TEX_VERTEX = { // in clockwise order:
1, 0, // bottom right
0, 0, // bottom left
0, 1, // top left
1, 1, // top right
};
private final FloatBuffer mVertexBuffer;
private final ShortBuffer mVertexIndexBuffer;
private final FloatBuffer mUvTexVertexBuffer;
private final float[] mProjectionMatrix = new float[16];
private final float[] mCameraMatrix = new float[16];
private final float[] mMVPMatrix = new float[16];
private int mProgram;
private int mPositionHandle;
private int mMatrixHandle;
private int mTexCoordHandle;
private int mTexSamplerHandle;
private int[] mTexNames;
private SurfaceTexture surfaceTexture;
private boolean isAvailiable;
public Surface surface;
MyRenderer() {
mVertexBuffer = ByteBuffer.allocateDirect(VERTEX.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(VERTEX);
mVertexBuffer.position(0);
mVertexIndexBuffer = ByteBuffer.allocateDirect(VERTEX_INDEX.length * 2)
.order(ByteOrder.nativeOrder())
.asShortBuffer()
.put(VERTEX_INDEX);
mVertexIndexBuffer.position(0);
mUvTexVertexBuffer = ByteBuffer.allocateDirect(UV_TEX_VERTEX.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(UV_TEX_VERTEX);
mUvTexVertexBuffer.position(0);
}
private Surface getSurface() {
if (surface != null) {
return surface;
}
int externalTextureId = -1;
mTexNames = new int[1];
GLES20.glGenTextures(1, mTexNames, 0);
externalTextureId = mTexNames[0];
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(
GL_TEXTURE_EXTERNAL_OES,
externalTextureId);
GLES20.glTexParameterf(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
surfaceTexture = new SurfaceTexture(externalTextureId);
surfaceTexture.setDefaultBufferSize(100, 100);
surfaceTexture.setOnFrameAvailableListener(
new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
isAvailiable = true;
glSurfaceView.requestRender();
}
});
surface = new Surface(surfaceTexture);
return surface;
}
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
getSurface();
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
mProgram = GLES20.glCreateProgram();
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
mTexCoordHandle = GLES20.glGetAttribLocation(mProgram, "a_texCoord");
mMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
mTexSamplerHandle = GLES20.glGetUniformLocation(mProgram, "s_texture");
float ratio = (float) height / width;
Matrix.frustumM(mProjectionMatrix, 0, -1, 1, -ratio, ratio, 3, 7);
Matrix.setLookAtM(mCameraMatrix, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mCameraMatrix, 0);
}
@Override
public void onDrawFrame(GL10 unused) {
if (isAvailiable) {
surfaceTexture.updateTexImage();
isAvailiable = false;
}
GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0,
mVertexBuffer);
GLES20.glEnableVertexAttribArray(mTexCoordHandle);
GLES20.glVertexAttribPointer(mTexCoordHandle, 2, GLES20.GL_FLOAT, false, 0,
mUvTexVertexBuffer);
GLES20.glUniformMatrix4fv(mMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniform1i(mTexSamplerHandle, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, VERTEX_INDEX.length,
GLES20.GL_UNSIGNED_SHORT, mVertexIndexBuffer);
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(mTexCoordHandle);
GLES20.glDisableVertexAttribArray(mTexSamplerHandle);
GLES20.glDisableVertexAttribArray(mMatrixHandle);
}
static int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
}