gpt4 book ai didi

java - 全屏视频查看而不拉伸(stretch)视频

转载 作者:IT老高 更新时间:2023-10-28 21:11:56 26 4
gpt4 key购买 nike

我想知道是否可以让视频通过 videoview 全屏运行?

我搜索了很多,尝试了很多方法,例如:

  1. 在 list 中应用主题:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

    但这不会强制视频全屏显示。

  2. 在 Activity 本身中应用:

    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    也不强制视频全屏显示。

强制视频全屏的唯一方法是:

<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>

这样会产生全屏视频,但会拉伸(stretch)视频本身(拉长视频),

我没有将这个不正确的解决方案应用于我的视频 View ,那么有什么方法可以在不拉伸(stretch)视频的情况下做到这一点?

视频类:

public class Video extends Activity {
private VideoView myvid;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myvid = (VideoView) findViewById(R.id.myvideoview);
myvid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+"/"+R.raw.video_1));
myvid.setMediaController(new MediaController(this));
myvid.requestFocus();
myvid.start();
}
}

ma​​in.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<VideoView
android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>

最佳答案

这样你可以自己设置视频的属性。

使用 SurfaceView(让您对 View 有更多控制权),将其设置为 fill_parent 以匹配整个屏幕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">

<SurfaceView
android:id="@+id/surfaceViewFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
</SurfaceView>
</Linearlayout>

然后在您的 java 代码中获取表面 View 并将您的媒体播放器添加到其中

surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
player = new MediaPlayer();
player.setDisplay(holder);

在您的媒体播放器上设置一个 onPreparedListener 并手动计算所需的视频大小,以所需的比例填充屏幕,避免拉伸(stretch)视频!

player.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
// Adjust the size of the video
// so it fits on the screen
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();

if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
surfaceViewFrame.setLayoutParams(lp);

if (!player.isPlaying()) {
player.start();
}

}
});

我根据前段时间关注的视频流教程修改了这个,现在找不到它来引用它,如果有人这样做,请将链接添加到答案中!

希望对你有帮助!

编辑

好的,因此,如果您希望视频占据整个屏幕并且不希望它拉伸(stretch),那么最终会在两侧出现黑色条纹。在我发布的代码中,我们正在找出更大的视频或手机屏幕,并以最佳方式对其进行调整。

你有我的完整 Activity ,从链接流式传输视频。它是 100% 功能性的。我不能告诉你如何从你自己的设备上播放视频,因为我不知道。我相信您会在文档 here 中找到它。或 here .

public class VideoPlayer extends Activity implements Callback, OnPreparedListener, OnCompletionListener, 
OnClickListener {

private SurfaceView surfaceViewFrame;
private static final String TAG = "VideoPlayer";
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private ImageView pause;
private MediaPlayer player;
private Timer updateTimer;
String video_uri = "http://daily3gp.com/vids/familyguy_has_own_orbit.3gp";


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


pause = (ImageView) findViewById(R.id.imageViewPauseIndicator);
pause.setVisibility(View.GONE);
if (player != null) {
if (!player.isPlaying()) {
pause.setVisibility(View.VISIBLE);
}
}


surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
surfaceViewFrame.setOnClickListener(this);
surfaceViewFrame.setClickable(false);

progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);

holder = surfaceViewFrame.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

player = new MediaPlayer();
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setScreenOnWhilePlaying(true);
player.setDisplay(holder);
}

private void playVideo() {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(video_uri);
player.prepare();
} catch (Exception e) { // I can split the exceptions to get which error i need.
showToast("Error while playing video");
Log.i(TAG, "Error");
e.printStackTrace();
}
}
}).start();
}

private void showToast(final String string) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(VideoPlayer.this, string, Toast.LENGTH_LONG).show();
finish();
}
});
}


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

}

public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}

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

}
//prepare the video
public void onPrepared(MediaPlayer mp) {
progressBarWait.setVisibility(View.GONE);

// Adjust the size of the video
// so it fits on the screen
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();

if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
surfaceViewFrame.setLayoutParams(lp);

if (!player.isPlaying()) {
player.start();
}
surfaceViewFrame.setClickable(true);
}

// callback when the video is over
public void onCompletion(MediaPlayer mp) {
mp.stop();
if (updateTimer != null) {
updateTimer.cancel();
}
finish();
}

//pause and resume
public void onClick(View v) {
if (v.getId() == R.id.surfaceViewFrame) {
if (player != null) {
if (player.isPlaying()) {
player.pause();
pause.setVisibility(View.VISIBLE);
} else {
player.start();
pause.setVisibility(View.GONE);
}
}
}
}

}

关于java - 全屏视频查看而不拉伸(stretch)视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12211837/

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