我想做的是在开始播放视频之前在 SurfaceView 上显示背景图像。
- 我试图只绘制一个 jpeg 图像作为 SurfaceView 的背景。有效。
- 我还尝试在 SurfaceView 上播放视频。它也奏效了。
但是,当我尝试在 surfaceCreated 函数中将 jpeg 图像绘制为 Surface 的背景然后播放视频时。我收到错误“[SurfaceView] 连接:已连接”。
有任何想法吗?谢谢。
这是部分代码
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
float scale = (float) background.getHeight()/(float)mPreview.getHeight();
int newWidth = Math.round(background.getWidth()/scale);
int newHeight = Math.round(background.getHeight()/scale);
mScaledBitmap = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
Canvas canvas = mHolder.lockCanvas();
canvas.drawBitmap(mScaledBitmap, 0, 0, null);
mHolder.unlockCanvasAndPost(canvas);
} catch (Exception e)
{}
// initialize mediaplayer
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource("/sdcard/Movies/2.mp4");
mMediaPlayer.setDisplay(mHolder);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "error: " + e.getMessage(), e);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "error: " + e.getMessage(), e);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
Log.i(TAG, "surfaceChanged(w="+ width + ", h=" + height);
mSurfaceWidth = width;
mSurfaceHeight = height;
mVideoWidth = mMediaPlayer.getVideoWidth();
mVideoHeight = mMediaPlayer.getVideoHeight();
Log.i(TAG, "video size w=" + mVideoWidth + ", h=" + mVideoHeight +")");
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.i(TAG, "onPrepared");
mMediaPlayer.start();
}