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

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值