SurfaceView 高度 +宽度被忽略

发布于 2024-12-18 06:20:48 字数 3981 浏览 1 评论 0原文

下面的脚本工作完美,但是在将表面视图从纵向更改为横向后,我的视频播放器的大小将会变大。尝试了几个选项,例如 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();
}
}

The following script works flawless, but the size of my videoplayer will get f'd after changing my surfaceview from portrait to landscape. Tried several options like setFixedSize(), setSizeFromLayout() and removing everything which involves my width + height from my surfaceview. Does anyone knows whats wrong with the code below?
Or did someone have the same problem in the past?

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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2024-12-25 06:20:48

同样的问题:

我相信问题出在 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。

Same problem here:

I believe the problem is the Vitamio library as I have experienced a similar problem when dealing with the Vitamio SDK. Resizing didn't work for me, when using a SurfaceView and invoking setLayoutParams(ViewGroup.LayoutParams params). The below code works fine using the standard Android media-framework, but importing the vitamio packages breaks it.

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);

My recommendation would be to try using the io.vov.vitamio.widget.VideoView instead of a android.view.SurfaceView.

堇色安年 2024-12-25 06:20:48

使用此方法并在 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);
}

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

use this method and call it inside onConfigurationChanged method :

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);
}

Now on change of orientation your videoview will be resized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文