下面列出了android.view.SurfaceHolder#getSurface ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @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);
}
}
@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);
}
}
}
@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);
}
@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);
}
}
}
@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();
}
@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);
}
}
}
@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();
}
@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();
}
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;
}
public void setDisplay(SurfaceHolder sh) {
Surface surface;
this.mSurfaceHolder = sh;
if (sh != null) {
surface = sh.getSurface();
} else {
surface = null;
}
_setVideoSurface(surface);
updateSurfaceScreenOn();
}
@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();
}
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);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
if(surfaceHolder.getSurface() == null) {
return;
}
stopCameraPreview();
showCameraPreview();
}
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;
}
/**
* 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();
}
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);
}
@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);
}
}
@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);
}
/**
* 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();
}
}
/**
* 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();
}