Java Code Examples for android.view.SurfaceHolder#getSurface()

The following examples show how to use android.view.SurfaceHolder#getSurface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaPlayer.java    From MediaPlayer-Extended with Apache License 2.0 6 votes vote down vote up
/**
 * @see android.media.MediaPlayer#setDisplay(android.view.SurfaceHolder)
 */
public void setDisplay(SurfaceHolder sh) {
    mSurfaceHolder = sh;
    if (sh != null) {
        mSurface = sh.getSurface();
    } else {
        mSurface = null;
    }

    if(mDecoders != null && mDecoders.getVideoDecoder() != null) {
        //mDecoders.getVideoDecoder().updateSurface(mSurface);
    }

    if(mPlaybackThread == null) {
        // Player not prepared yet, so we can set the timing mode
        setVideoRenderTimingMode(VideoRenderTimingMode.AUTO);
        updateSurfaceScreenOn();
    } else {
        // Player is already prepared, just change the surface
        mPlaybackThread.setSurface(mSurface);
    }
}
 
Example 2
Source File: SimpleExoPlayer.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public void setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder) {
  verifyApplicationThread();
  removeSurfaceCallbacks();
  if (surfaceHolder != null) {
    clearVideoDecoderOutputBufferRenderer();
  }
  this.surfaceHolder = surfaceHolder;
  if (surfaceHolder == null) {
    setVideoSurfaceInternal(null, /* ownsSurface= */ false);
    maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
  } else {
    surfaceHolder.addCallback(componentListener);
    Surface surface = surfaceHolder.getSurface();
    if (surface != null && surface.isValid()) {
      setVideoSurfaceInternal(surface, /* ownsSurface= */ false);
      Rect surfaceSize = surfaceHolder.getSurfaceFrame();
      maybeNotifySurfaceSizeChanged(surfaceSize.width(), surfaceSize.height());
    } else {
      setVideoSurfaceInternal(/* surface= */ null, /* ownsSurface= */ false);
      maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
    }
  }
}
 
Example 3
Source File: FastPathSurfaceView.java    From walt with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Surface surface = holder.getSurface();
    if (surface == null)
        return;

    try {
        Method setSharedBufferMode = Surface.class.getMethod("setSharedBufferMode", boolean.class);
        setSharedBufferMode.invoke(surface, true);
        displayMessage("Using shared buffer mode.");
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        displayMessage("Shared buffer mode is not supported.");
    }
    Canvas canvas = surface.lockCanvas(null);
    canvas.drawColor(Color.GRAY);
    surface.unlockCanvasAndPost(canvas);
}
 
Example 4
Source File: CameraActivity.java    From VideoFaceDetection with MIT License 6 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
    // We have no surface, return immediately:
    if (surfaceHolder.getSurface() == null) {
        return;
    }
    // Try to stop the current preview:
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // Ignore...
    }

    configureCamera(width, height);
    setDisplayOrientation();
    setErrorCallback();

    // Everything is configured! Finally start the camera preview again:
    mCamera.startPreview();
}
 
Example 5
Source File: SimpleExoPlayer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setVideoSurfaceHolder(SurfaceHolder surfaceHolder) {
  removeSurfaceCallbacks();
  this.surfaceHolder = surfaceHolder;
  if (surfaceHolder == null) {
    setVideoSurfaceInternal(null, false);
    maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
  } else {
    surfaceHolder.addCallback(componentListener);
    Surface surface = surfaceHolder.getSurface();
    if (surface != null && surface.isValid()) {
      setVideoSurfaceInternal(surface, /* ownsSurface= */ false);
      Rect surfaceSize = surfaceHolder.getSurfaceFrame();
      maybeNotifySurfaceSizeChanged(surfaceSize.width(), surfaceSize.height());
    } else {
      setVideoSurfaceInternal(/* surface= */ null, /* ownsSurface= */ false);
      maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
    }
  }
}
 
Example 6
Source File: FaceDetectRGBActivity.java    From FaceDetectCamera with Apache License 2.0 6 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
    // We have no surface, return immediately:
    if (surfaceHolder.getSurface() == null) {
        return;
    }
    // Try to stop the current preview:
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // Ignore...
    }

    configureCamera(width, height);
    setDisplayOrientation();
    setErrorCallback();

    // Create media.FaceDetector
    float aspect = (float) previewHeight / (float) previewWidth;
    fdet = new android.media.FaceDetector(prevSettingWidth, (int) (prevSettingWidth * aspect), MAX_FACE);


    // Everything is configured! Finally start the camera preview again:
    startPreview();
}
 
Example 7
Source File: QRCodeReaderView.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    SimpleLog.d(TAG, "surfaceChanged");

    if (holder.getSurface() == null) {
        SimpleLog.e(TAG, "Error: preview surface does not exist");
        return;
    }

    if (mCameraManager.getPreviewSize() == null) {
        SimpleLog.e(TAG, "Error: preview size does not exist");
        return;
    }

    mPreviewWidth = mCameraManager.getPreviewSize().x;
    mPreviewHeight = mCameraManager.getPreviewSize().y;

    mCameraManager.stopPreview();

    // Fix the camera sensor rotation
    mCameraManager.setPreviewCallback(this);
    mCameraManager.setDisplayOrientation(getCameraDisplayOrientation());

    mCameraManager.startPreview();
}
 
Example 8
Source File: SimpleExoPlayer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setVideoSurfaceHolder(SurfaceHolder surfaceHolder) {
  removeSurfaceCallbacks();
  this.surfaceHolder = surfaceHolder;
  if (surfaceHolder == null) {
    setVideoSurfaceInternal(null, false);
    maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
  } else {
    surfaceHolder.addCallback(componentListener);
    Surface surface = surfaceHolder.getSurface();
    if (surface != null && surface.isValid()) {
      setVideoSurfaceInternal(surface, /* ownsSurface= */ false);
      Rect surfaceSize = surfaceHolder.getSurfaceFrame();
      maybeNotifySurfaceSizeChanged(surfaceSize.width(), surfaceSize.height());
    } else {
      setVideoSurfaceInternal(/* surface= */ null, /* ownsSurface= */ false);
      maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
    }
  }
}
 
Example 9
Source File: CarlifeActivity.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    LogUtil.e(TAG, "surfaceChanged ~ ~");
    mSurface = holder.getSurface();
    DecodeUtil.getInstance().pauseThread();
    DecodeUtil.getInstance().initDecoder(mSurface);
    DecodeUtil.getInstance().resumeThread();
    LogUtil.e(TAG, "surfaceChanged ~ ~ end");

    TouchListenerManager.getInstance().setContainerWidth(width);
    TouchListenerManager.getInstance().setContainerHeight(height);
    LogUtil.e(TAG, "Car Surface View Size: width = " + width + ", height = " + height);
}
 
Example 10
Source File: ExoMedia.java    From QSVideoPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void setDisplay(SurfaceHolder surfaceHolder) {
    try {
        if (simpleExoPlayer != null)
            simpleExoPlayer.setVideoSurfaceHolder(surfaceHolder);
        if (surfaceHolder != null)
            this.surface = surfaceHolder.getSurface();
    } catch (Exception e) {
        e.printStackTrace();
        iMediaCallback.onError(this, 10010, 10010);
    }
}
 
Example 11
Source File: MediaPlayer.java    From qplayer-sdk with MIT License 5 votes vote down vote up
public void SetView(SurfaceView sv) {
	m_SurfaceView = sv;
	if (sv != null) {
		SurfaceHolder sh = sv.getHolder();
		m_NativeSurface = sh.getSurface();
	} else {
		m_NativeSurface = null;
	}
	if (m_NativeContext != 0)
		nativeSetView(m_NativeContext, m_NativeSurface);
}
 
Example 12
Source File: MediaPlayer.java    From dttv-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the SurfaceHolder to use for displaying the video portion of the
 * media. This call is optional. Not calling it when playing back a video will
 * result in only the audio track being played.
 *
 * @param sh the SurfaceHolder to use for video display
 */
public void setDisplay(SurfaceHolder sh) {
    if (sh == null) {
        releaseDisplay();
        return;
    }
    mSurfaceHolder = sh;
    mSurface = sh.getSurface();
    native_set_video_surface(mSurface);
    updateSurfaceScreenOn();
}
 
Example 13
Source File: ExoVlcUtil.java    From Exoplayer_VLC with Apache License 2.0 5 votes vote down vote up
static boolean validSurface(SurfaceHolder holder) {
	if (holder.getSurface() != null) {
		Rect r = holder.getSurfaceFrame();
		System.out.println("ExoVlcUtil.validSurface() r = " + r);
		return (r.width() * r.height()) > 0;
	}
	return false;
}
 
Example 14
Source File: CameraPreview.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    if(surfaceHolder.getSurface() == null) {
        return;
    }
    stopCameraPreview();
    showCameraPreview();
}
 
Example 15
Source File: VideoRecorder.java    From VideoCamera with Apache License 2.0 5 votes vote down vote up
public VideoRecorder(VideoRecorderInterface recorderInterface, CaptureConfiguration captureConfiguration, VideoFile videoFile,
                     CameraWrapper cameraWrapper, SurfaceHolder previewHolder) {
    mCaptureConfiguration = captureConfiguration;
    mRecorderInterface = recorderInterface;
    mVideoFile = videoFile;
    mCameraWrapper = cameraWrapper;
    mPreviewSurface = previewHolder.getSurface();

    initializeCameraAndPreview(previewHolder);
}
 
Example 16
Source File: FaceDetectGrayActivity.java    From FaceDetectCamera with Apache License 2.0 5 votes vote down vote up
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
    // We have no surface, return immediately:
    if (surfaceHolder.getSurface() == null) {
        return;
    }
    // Try to stop the current preview:
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // Ignore...
    }

    configureCamera(width, height);
    setDisplayOrientation();
    setErrorCallback();

    // Create media.FaceDetector
    float aspect = (float) previewHeight / (float) previewWidth;
    fdet = new android.media.FaceDetector(prevSettingWidth, (int) (prevSettingWidth * aspect), MAX_FACE);

    bufflen = previewWidth * previewHeight;
    grayBuff = new byte[bufflen];
    rgbs = new int[bufflen];

    // Everything is configured! Finally start the camera preview again:
    startPreview();
}
 
Example 17
Source File: FimiMediaPlayer.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public void setDisplay(SurfaceHolder sh) {
    Surface surface;
    this.mSurfaceHolder = sh;
    if (sh != null) {
        surface = sh.getSurface();
    } else {
        surface = null;
    }
    _setVideoSurface(surface);
    updateSurfaceScreenOn();
}
 
Example 18
Source File: ExoVlcUtil.java    From Exoplayer_VLC with Apache License 2.0 5 votes vote down vote up
static boolean validSurface(SurfaceHolder holder) {
	if (holder.getSurface() != null) {
		Rect r = holder.getSurfaceFrame();
		System.out.println("ExoVlcUtil.validSurface() r = " + r);
		return (r.width() * r.height()) > 0;
	}
	return false;
}
 
Example 19
Source File: PlayMovieSurfaceActivity.java    From pause-resume-video-recording with Apache License 2.0 4 votes vote down vote up
/**
 * onClick handler for "play"/"stop" button.
 */
public void clickPlayStop(@SuppressWarnings("unused") View unused) {
    if (mShowStopLabel) {
        Log.d(TAG, "stopping movie");
        stopPlayback();
        // Don't update the controls here -- let the task thread do it after the movie has
        // actually stopped.
        //mShowStopLabel = false;
        //updateControls();
    } else {
        if (mPlayTask != null) {
            Log.w(TAG, "movie already playing");
            return;
        }

        Log.d(TAG, "starting movie");
        SpeedControlCallback callback = new SpeedControlCallback();
        SurfaceHolder holder = mSurfaceView.getHolder();
        Surface surface = holder.getSurface();

        // Don't leave the last frame of the previous video hanging on the screen.
        // Looks weird if the aspect ratio changes.
        clearSurface(surface);

        MoviePlayer player = null;
        try {
             player = new MoviePlayer(
                    new File(Environment.getExternalStorageDirectory(), mMovieFiles[mSelectedMovie]), surface, callback);
        } catch (IOException ioe) {
            Log.e(TAG, "Unable to play movie", ioe);
            surface.release();
            return;
        }

        AspectFrameLayout layout = (AspectFrameLayout) findViewById(R.id.playMovie_afl);
        int width = player.getVideoWidth();
        int height = player.getVideoHeight();
        layout.setAspectRatio((double) width / height);
        //holder.setFixedSize(width, height);

        mPlayTask = new MoviePlayer.PlayTask(player, this);

        mShowStopLabel = true;
        updateControls();
        mPlayTask.execute();
    }
}
 
Example 20
Source File: IjkMediaPlayer.java    From IjkPlayerDemo with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the {@link SurfaceHolder} to use for displaying the video portion of
 * the media.
 * 
 * Either a surface holder or surface must be set if a display or video sink
 * is needed. Not calling this method or {@link #setSurface(Surface)} when
 * playing back a video will result in only the audio track being played. A
 * null surface holder or surface will result in only the audio track being
 * played.
 * 
 * @param sh
 *            the SurfaceHolder to use for video display
 */
@Override
public void setDisplay(SurfaceHolder sh) {
    mSurfaceHolder = sh;
    Surface surface;
    if (sh != null) {
        surface = sh.getSurface();
    } else {
        surface = null;
    }
    _setVideoSurface(surface);
    updateSurfaceScreenOn();
}