Android Open Source - SBSPlayer Video Surface View






From Project

Back to project page SBSPlayer.

License

The source code is released under:

Apache License

If you think the Android project SBSPlayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 The Android Open Source Project
 *//  ww  w .jav a  2s .  c om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package im.years.sbs;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.opengl.GLSurfaceView;
import android.view.Surface;

class VideoSurfaceView extends GLSurfaceView {
  private static final String TAG = "VideoSurfaceView";
  private static final int SLEEP_TIME_MS = 1000;
  VideoRender mRenderer;
  public static MediaPlayer mMediaPlayer = null;
  public static VideoSurfaceView that;

  public VideoSurfaceView(Context context, MediaPlayer mp) {
    super(context);
    that = this;
    setEGLContextClientVersion(2);
    mMediaPlayer = mp;
    mRenderer = new VideoRender(context);
    mRenderer.setMediaPlayer(mMediaPlayer);
    setRenderer(mRenderer);
  }

  @Override
  public void onResume() {
    queueEvent(new Runnable() {
      public void run() {
        mRenderer.setMediaPlayer(mMediaPlayer);
      }
    });
    super.onResume();
  }

  public void startTest() throws Exception {
    Thread.sleep(SLEEP_TIME_MS);
    mMediaPlayer.start();
    Thread.sleep(SLEEP_TIME_MS * 5);
    mMediaPlayer.setSurface(null);
    while (mMediaPlayer.isPlaying()) {
      Thread.sleep(SLEEP_TIME_MS);
    }
  }

  /**
   * A GLSurfaceView implementation that wraps TextureRender. Used to render
   * frames from a video decoder to a View.
   */
  private static class VideoRender implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
    private static String TAG = "VideoRender";
    private TextureRender mTextureRender;
    private SurfaceTexture mSurfaceTexture;
    private boolean updateSurface = false;
    private MediaPlayer mMediaPlayer;

    public VideoRender(Context context) {
      mTextureRender = new TextureRender();
    }

    public void setMediaPlayer(MediaPlayer player) {
      mMediaPlayer = player;
    }

    public void onDrawFrame(GL10 glUnused) {
      synchronized (this) {
        if (updateSurface) {
          mSurfaceTexture.updateTexImage();
          updateSurface = false;
        }
      }
      mTextureRender.drawFrame(mSurfaceTexture);
    }

    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    }

    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
      mTextureRender.surfaceCreated();
      /*
       * Create the SurfaceTexture that will feed this textureID, and pass
       * it to the MediaPlayer
       */
      mSurfaceTexture = new SurfaceTexture(mTextureRender.getTextureId());
      mSurfaceTexture.setOnFrameAvailableListener(this);
      Surface surface = new Surface(mSurfaceTexture);
      mMediaPlayer.setSurface(surface);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.start();
      surface.release();
      
      synchronized (this) {
        updateSurface = false;
      }
    }

    synchronized public void onFrameAvailable(SurfaceTexture surface) {
      updateSurface = true;
    }
  } // End of class VideoRender.
} // End of class VideoSurfaceView.





Java Source Code List

im.years.sbs.MainActivity.java
im.years.sbs.TextureRender.java
im.years.sbs.VideoSurfaceView.java