日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA不能满屏_java – 全屏幕视频,不拉伸视频

發布時間:2025/3/8 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA不能满屏_java – 全屏幕视频,不拉伸视频 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

像這樣,你可以自己設置視頻的屬性。

使用SurfaceView(給你更多的視圖控制),將其設置為fill_parent以匹配整個屏幕

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="fill_parent">

android:id="@+id/surfaceViewFrame"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center" >

然后在您的java代碼獲取表面視圖并添加您的媒體播放器

surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);

player = new MediaPlayer();

player.setDisplay(holder);

在您的媒體播放器上設置一個onPreparedListener,并手動計算視頻的所需大小,以期望的比例填充屏幕,避免視頻播放!

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

}

}

});

我從一段時間以前跟蹤的視頻流教程修改了這個,現在找不到它來引用它,如果有人請添加鏈接到答案!

希望有幫助!

編輯

好的,所以,如果你想讓視頻占據整個屏幕,你不希望它伸展,最終會出現黑色的條紋。在我發布的代碼中,我們發現更大,視頻或手機屏幕是最好的方式。

在那里,您有完整的活動,從鏈接流式傳輸視頻。它的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不能满屏_java – 全屏幕视频,不拉伸视频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。