Android:Mediaplayer:如何使用SurfaceView或mediaplayer播放正确大小的视频

JRC*_*JRC 19 android media-player

我正在使用MediaPlayer和SurfaceView播放本地视频文件.SurfaceView是活动中唯一的控件,而我的视频文件是QVGA或其他.问题是视频正在变得拉长,我如何以原始大小播放视频,例如qvga,其余区域为黑色.

从迭代开始,

当我在XML中强制设置Surfaceview的layout_height/width时,视频显示正常. surface_holder.setFixedSize(w,h)没有效果,也没有mp.setdisplay().

请指导一下.

UPDATE

XML flie

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+id/home_container"  
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">

<SurfaceView 
        android:id="@+id/surface" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="10dip" />
</framelayout>
Run Code Online (Sandbox Code Playgroud)

MediaPlayer用法如下所示

http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/

提前致谢.

Err*_*454 79

将SurfaceView布局设置为wrap_content 不会调整视频大小以适当的宽高比播放.

  • SurfaceView是优化的绘图表面
  • 视频被绘制到SurfaceView中,而不包含在其中

wrap_content与SurfaceView的fill_parent同义.

您要做的是从MediaPlayer对象获取视频的尺寸.然后,您可以设置SurfaceView的宽高比以匹配视频.

一些基本初始化

public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
    private MediaPlayer mp = null;
    //...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mp = new MediaPlayer();
        mSurfaceView = (SurfaceView) findViewById(R.id.surface);
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是好东西.我在这里省略了错误检查以减少代码,MediaPlayer调用应该包含在try {}中.

@Override
public void surfaceCreated(SurfaceHolder holder) {

    mp.setDataSource("/sdcard/someVideo.mp4");
    mp.prepare();

    //Get the dimensions of the video
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();

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

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    //Commit the layout parameters
    mSurfaceView.setLayoutParams(lp);        

    //Start video
    mp.start();
}
Run Code Online (Sandbox Code Playgroud)

请注意,此代码会对视频的尺寸做出一些假设.原样,它最大化宽度并假设高度不大于屏幕的高度.

您可能希望适合高度而不是宽度,也可以检查尺寸计算并确保它不大于屏幕或屏幕 - other_layout_elements.

  • 嗨,这个样本我只有一个带有声音的黑屏...我该怎么办才能解决它? (2认同)

she*_*pya 5

这是我当前在项目中使用的代码:

private MediaPlayer mMediaPlayer;
private SurfaceView mSurfaceView;
private SurfaceHolder holder;
private int mPos = 0;
Run Code Online (Sandbox Code Playgroud)

...

int width = mSurfaceView.getWidth();
int height = mSurfaceView.getHeight();
float boxWidth = width;
float boxHeight = height;

float videoWidth = mMediaPlayer.getVideoWidth();
float videoHeight = mMediaPlayer.getVideoHeight();

Log.i(TAG, String.format("startVideoPlayback @ %d - video %dx%d - box %dx%d", mPos, (int) videoWidth, (int) videoHeight, width, height));

float wr = boxWidth / videoWidth;
float hr = boxHeight / videoHeight;
float ar = videoWidth / videoHeight;

if (wr > hr)
    width = (int) (boxHeight * ar);
else
    height = (int) (boxWidth / ar);

Log.i(TAG, String.format("Scaled to %dx%d", width, height));

holder.setFixedSize(width, height);
mMediaPlayer.seekTo(mPos);
mMediaPlayer.start();
Run Code Online (Sandbox Code Playgroud)

我正在使用的布局(您可以忽略进度条)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >
</SurfaceView>
Run Code Online (Sandbox Code Playgroud)


Joe*_*oey -5

在 XML 中的 SurfaceView 上,您是否使用 wrap_content ?如果没有的话,这应该可以解决您的问题。如果这不能解决您的问题,您可能需要粘贴更多代码以进行进一步调查。

将表面视图宽度也更改为wrap_content。