天天看點

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

}

}

}

}

}