2011-11-28 110 views
4

以下脚本工作完美无瑕,但在将我的表面视图从纵向更改为横向后,我的视频播放器的大小将会变为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(); 
} 
} 
+0

我需要回答:'( – Jordy

回答

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的尝试。

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

现在对方向的改变,你的videoview将被调整。