SurfaceView高度+宽度被忽略

时间:2011-11-28 09:37:23

标签: android video surfaceview

以下脚本完美无瑕,但在将我的表面视图从纵向更改为横向后,我的视频播放器的大小将变为f'd。尝试了几个选项,例如setFixedSize()setSizeFromLayout(),并从我的surfaceview中删除涉及我的宽度+高度的所有内容。有谁知道下面的代码有什么问题? 或者过去有人遇到同样的问题吗?

package com.list;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.MediaPlayer.OnVideoSizeChangedListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class mediaPlayer extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;


private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.mediaplayer);
    mPreview = (SurfaceView) findViewById(R.id.surface);
    holder = mPreview.getHolder();
    holder.addCallback(this);
}

private void playVideo() {
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer(this);
        Intent myIntent = this.getIntent();
        String url = myIntent.getStringExtra("url");
        mMediaPlayer.setDataSource(url);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
    }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
    Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
    Log.d(TAG, "onCompletion called");
    mMediaPlayer.release();
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.v(TAG, "onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
        return;
    }
    Log.v("afmeting", "->" +width+"px bij "+height+"px");
    mIsVideoSizeKnown = true;

    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called" + i + "  " + j + "   " + k);
}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called");
    playVideo();
}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaPlayer();
    doCleanUp();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
}

private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setSizeFromLayout();
    mMediaPlayer.start();
}
}

2 个答案:

答案 0 :(得分:1)

此处存在相同问题:

我认为问题是Vitamio库,因为我在处理Vitamio SDK时遇到了类似的问题。使用SurfaceView并调用setLayoutParams(ViewGroup.LayoutParams params)时,调整大小对我不起作用。下面的代码使用标准的Android媒体框架可以正常工作,但是导入vitamio包可以打破它。

RelativeLayout.LayoutParams video_VIEWLAYOUT = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
video_VIEWLAYOUT.width = screenWidth;
video_VIEWLAYOUT.height = (int) (((float)videoHeight / (float)videoWidth) *(float)screenWidth);
videoView.setLayoutParams(video_VIEWLAYOUT);

我的建议是尝试使用io.vov.vitamio.widget.VideoView而不是android.view.SurfaceView

答案 1 :(得分:0)

使用此方法并在 onConfigurationChanged 方法中调用它:

private void setVideoSize() {

    // // Get the dimensions of the video

            // mVideoView is your video view object.

    int videoWidth = mVideoView.getVideoWidth();
    int videoHeight = mVideoView.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;

    // Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;

    // Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    // Commit the layout parameters
    mVideoView.setLayoutParams(lp);
}

现在,在更改方向时,您的视频视图将会调整大小。