我正在使用一些以编程方式创建Surface
并使用它来显示相机预览的代码。我正在尝试将其修改为显示视频。更改后的代码(下方)播放视频的音频,但没有视频 - 只是黑屏。
相机预览版工作正常,因此我不认为问题在于如何创建或显示SurfaceTexture
或Surface
。
int texid = getTexture(); //native method
mSurfaceTexture = new SurfaceTexture(texid);
Log.e(TAG, "texid is "+texid);
mSurfaceTexture.setOnFrameAvailableListener(new OnFrameAvailableListener() {
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
Log.d(TAG, "onFrameAvailable");
}
});
final Surface surface = new Surface(mSurfaceTexture);
mediaPlayer.setSurface(surface);
String mSourceString = "clips/key_frames_movie_small.mp4";
AssetManager assetManager = mContext.getResources().getAssets();
AssetFileDescriptor clipFd = assetManager.openFd(mSourceString);
mediaPlayer.setDataSource(clipFd.getFileDescriptor(),
clipFd.getStartOffset(),
clipFd.getLength());
clipFd.close();
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "ERROR: "+what + ", " + extra);
return false;
}
});
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "INFO: "+what + ", " + extra);
return false;
}
});
mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.e(TAG, "onVideoSizeChanged: "+width + ", " + height);
}
});
mediaPlayer.prepare();
mediaPlayer.start();
我尝试的事情:
SurfaceView
中播放相同的视频。这很好用,所以我不认为视频本身存在任何问题。 mediaPlayer.setSurface(surface)
之前,使用过的日志验证表面是否已创建。surfaceView.setZOrderOnTop(true)
,surfaceView.setZOrderMediaOverlay(true)
,surfaceView.setVisibility(View.VISIBLE)
以确保视图未被任何内容部分隐藏。没效果。holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
,即使我在6.0.1上运行。如何在不改变曲面纹理或曲面创建方式的情况下让视频在曲面中播放?
修改:
日志看起来像这样:
E/com.package: texid is 1
D/MediaPlayer: setSubtitleAnchor in MediaPlayer
W/MediaPlayer: info/warning (3, 0)
D/MediaPlayer: setSubtitleAnchor in MediaPlayer
E/com.package: onVideoSizeChanged: 960, 540
E/com.package: onVideoSizeChanged: 960, 540
E/com.package: INFO: 3, 0
D/com.package: onFrameAvailable
根据docs,信息代码3表示"播放器只是推送了第一个视频帧进行渲染。"
onFrameAvailable
只被调用一次。如果我在surfaceTexture.updateTexImage();
回调中onFrameAvailable()
onFrameAvailable()
,则/**
* ALARM_MANAGER_GPS_TIME_INTERVAL = 10 min
*/
PendingIntent mPendingIntent = PendingIntent
.getService(
context,
PENDING_INTENT_REQUEST_CODE_FOR_GPS,
new Intent(context, GpsDataService.class),
PendingIntent.FLAG_IMMUTABLE);
AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
ALARM_MANAGER_GPS_TIME_INTERVAL,
mPendingIntent);
会有(可能每秒一次)额外的来电,但它不会影响显示,而显示会保持黑色。< / p>