gpt4 book ai didi

android - Nexus 6 和 MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:03:55 26 4
gpt4 key购买 nike

您好,我正在尝试实现一个背景由视频文件制作的 Activity 。下面发布的解决方案适用于大多数设备,nexus 6 Android 5.1 除外。视频正常播放,已缩放但未裁剪,因此未保留纵横比。

使用 Galaxy Note、HTC One m7、Xperia Z3、Nexus 5 进行测试。

代码如下:

public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback, OnClickListener
{
private static final int VIDEO_START_HACK_DELAY = 0;

private static final String BG_VIDEO_PATH = "/raw/construction";
private SurfaceView mSurface;

private Button mTour, mLogin;

private MediaPlayer mMediaPlayer;

private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_main);
setupUI();
}

private void setupUI()
{
mMediaPlayer = new MediaPlayer();
mSurface = (SurfaceView) findViewById(R.id.surface);
SurfaceHolder holder = mSurface.getHolder();
holder.addCallback(this);

mTour = (Button) findViewById(R.id.tour);
mLogin = (Button) findViewById(R.id.login);

mTour.setOnClickListener(this);
mLogin.setOnClickListener(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder)
{
startVideo(holder);
}

@SuppressLint("NewApi")
public void startVideo(SurfaceHolder holder)
{
String path = "android.resource://" + getPackageName() + BG_VIDEO_PATH;
Uri uri = Uri.parse(path);

try
{
android.view.ViewGroup.LayoutParams lp = mSurface.getLayoutParams();
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setDataSource(this, uri);
mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
mMediaPlayer.setLooping(true);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener()
{

@Override
public void onPrepared(MediaPlayer mp)
{
mHandler.postDelayed(new Runnable()
{

@Override
public void run()
{
mMediaPlayer.start();
}
}, VIDEO_START_HACK_DELAY);

}
});
mMediaPlayer.prepareAsync();
}
catch (Exception e)
{
Log.e("VID", "Problem starting video bg", e);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
// TODO Auto-generated method stub
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub

}

@Override
public void onBackPressed()
{
mSurface.setVisibility(View.INVISIBLE);
mMediaPlayer.stop();
super.onBackPressed();
}

@Override
protected void onDestroy()
{
mMediaPlayer.stop();
mMediaPlayer.release();
super.onDestroy();
}
}

这是布局文件:

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

<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center"
android:orientation="vertical" >

<Button
android:id="@+id/tour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:padding="10dp"
android:text="@string/ma_tour"/>

<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:text="@string/ma_log_in"/>

</LinearLayout>

</RelativeLayout>

目标 sdk 设置为 22。您是否经历过这种行为并可以提供任何建议?

最佳答案

这是我在我的一个应用程序中的做法:

  1. 在 MediaPlayer 对象上设置 OnPreparedListener。

    mediaPlayer.setOnPreparedListener(this);

  2. public void onPrepared(MediaPlayer mp) 被调用时,调整 SurfaceView 的大小,使其产生裁剪视频的错觉。 Log.v(LOG_TAG, "onPrepared 调用 MediaPlayer"); //调整视频大小使其适合屏幕 int videoWidth = mInitialVideoPlayer.getVideoWidth(); int videoHeight = mInitialVideoPlayer.getVideoHeight(); float videoProportion = (float) videoWidth/(float) videoHeight;

    ViewGroup.LayoutParams lp = mSplashVideoSurfaceView.getLayoutParams();

    // We always want to fill entire view port. So, we keep the smallest dimension and proportionately
    // scale the other dimension.
    if (videoWidth < videoHeight) {
    lp.width = UiUtils.sScreenWidth;
    lp.height = (int) ((float) UiUtils.sScreenWidth / videoProportion);
    } else {
    lp.width = (int) (videoProportion * (float) UiUtils.sScreenHeight);If you are targetting API level 14 and above, <code>TextureView</code> allows you to play cropped video
    lp.height = UiUtils.sScreenHeight;
    }
    Log.v(LOG_TAG, "old video size: " + videoWidth + ", " + videoHeight);
    Log.v(LOG_TAG, "screen size: " + UiUtils.sScreenWidth + ", " + UiUtils.sScreenHeight);
    Log.v(LOG_TAG, "new video size: " + lp.width + ", " + lp.height);
    mSplashVideoSurfaceView.setLayoutParams(lp);

关于android - Nexus 6 和 MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com