无拉伸视频全屏videoview [英] Full screen videoview without stretching the video

查看:1483
本文介绍了无拉伸视频全屏videoview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如果我能得到一个方法,让视频运行通过videoview全屏?

我搜索了很多,试了很多办法,如:

  1. 应用主题清单:

     安卓主题=@安卓风格/ Theme.NoTitleBar.Fullscreen
     

    但是,这并不强制视频是在全屏显示。

  2. 应用在活动本身:

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

    也不会强制视频是在全屏显示。

只有这样,力视频全屏是:

 < VideoView机器人:ID =@ + ID / myvideoview
    机器人:layout_width =FILL_PARENT
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_alignParentBottom =真
    机器人:layout_height =FILL_PARENT>
< / VideoView>
 

此方式,导致全屏幕视频,但它的绵延视频本身(拉长视频),

我不将这种不正当的解决方案,我videoview,那么有没有什么办法可以做到这一点不拉伸视频?

视频类:

 公共类视频延伸活动{
    私人VideoView myvid;

    @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);
        的setContentView(R.layout.main);
        myvid =(VideoView)findViewById(R.id.myvideoview);
        myvid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
            +/+ R.raw.video_1));
        myvid.setMediaController(新的MediaController(本));
        myvid.requestFocus();
        myvid.start();
    }
}
 

main.xml中:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    < VideoView
        机器人:ID =@ + ID / myvideoview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>

< / LinearLayout中>
 

解决方案

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

使用一个SurfaceView(给你的看法更多的控制),将其设置为FILL_PARENT匹配整个屏幕

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:方向=垂直
              机器人:layout_width =match_parent
              机器人:layout_height =FILL_PARENT>

    < SurfaceView
        机器人:ID =@ + ID / surfaceViewFrame
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_gravity =中心>
    < / SurfaceView>
< / LinearLayout中>
 

然后在你的java code得到表面观和媒体播放器添加到它

  surfaceViewFrame =(SurfaceView)findViewById(R.id.surfaceViewFrame);
球员=新的MediaPlayer();
player.setDisplay(保持器);
 

设置在媒体播放器上的preparedListener和手动计算视频所需的大小,以填满屏幕所需的比例,避免拉伸视频!

  player.setOn preparedListener(新在preparedListener(){

        @覆盖
        公共无效于prepared(MediaPlayer的MP){
                    //调整视频的大小
    //所以它在屏幕上适合
    INT videoWidth = player.getVideoWidth();
    INT videoHeight = player.getVideoHeight();
    浮动videoProportion =(浮点)videoWidth /(浮点)videoHeight;
    INT屏幕宽度= getWindowManager()getDefaultDisplay()的getWidth()。
    。INT screenHeight = getWindowManager()getDefaultDisplay()的getHeight();
    浮动screenProportion =(浮点)屏幕宽度/(浮点)screenHeight;
    android.view.ViewGroup.LayoutParams LP = surfaceViewFrame.getLayoutParams();

    如果(videoProportion> screenProportion){
        lp.width =屏幕宽度;
        lp.height =(INT)((浮点)屏幕宽度/ videoProportion);
    } 其他 {
        lp.width =(INT)(videoProportion *(浮点)screenHeight);
        lp.height = screenHeight;
    }
    surfaceViewFrame.setLayoutParams(LP);

    如果(!player.isPlaying()){
        player.start();
    }

        }
    });
 

我修改这从一个教程视频流,我跟着前一段时间,找不到它现在引用它,如果有人不请链接添加到答案!

希望它能帮助!

修改

好了,所以,如果你想要的视频占据整个屏幕,你不希望它伸展你将结束与黑色条纹的两侧。在code我发布我们发现了什么越大,视频或手机屏幕,配合它的最佳方式,我们可以。

有你有我的完整活动中,链接的流媒体视频。这是100%的功能。我不能告诉你如何从自己的设备上播放的视频,因为我不知道。我敢肯定,你会发现它在文档这里here

 公共类的VideoPlayer扩展活动实现回调,在preparedListener,OnCompletionListener,
    OnClickListener {

私人SurfaceView surfaceViewFrame;
私有静态最后字符串变量=的VideoPlayer;
私人SurfaceHolder持有人;
私人进度progressBarWait;
私人ImageView的停顿;
私人的MediaPlayer播放器;
私人定时器updateTimer;
字符串video_uri =htt​​p://daily3gp.com/vids/familyguy_has_own_orbit.3gp;


@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.videosample);


    暂停=(ImageView的)findViewById(R.id.imageViewPauseIndicator);
    pause.setVisibility(View.GONE);
    如果(玩家!= NULL){
        如果(!player.isPlaying()){
            pause.setVisibility(View.VISIBLE);
        }
    }


    surfaceViewFrame =(SurfaceView)findViewById(R.id.surfaceViewFrame);
    surfaceViewFrame.setOnClickListener(本);
    surfaceViewFrame.setClickable(假);

    progressBarWait =(进度)findViewById(R.id.progressBarWait);

    支架= surfaceViewFrame.getHolder();
    holder.addCallback(本);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    球员=新的MediaPlayer();
    player.setOn preparedListener(本);
    player.setOnCompletionListener(本);
    player.setScreenOnWhilePlaying(真正的);
    player.setDisplay(保持器);
}

私人无效的playVideo(){
        新主题(新的Runnable(){
            公共无效的run(){
                尝试 {
                    player.setDataSource(video_uri);
                    播放器prepare()。
                }赶上(例外五){//我可以分割例外获得我需要哪些错误。
                    showToast(错误,而播放视频);
                    Log.i(TAG,错误);
                    e.printStackTrace();
                }
            }
        })。开始();
}

私人无效showToast(最后弦乐字符串){
    runOnUiThread(新的Runnable(){
        公共无效的run(){
            Toast.makeText(VideoPlayer.this,串,Toast.LENGTH_LONG).show();
            完();
        }
    });
}


公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){
    // TODO自动生成方法存根

}

公共无效surfaceCreated(SurfaceHolder持有者){
    的playVideo();
}

公共无效surfaceDestroyed(SurfaceHolder持有者){
    // TODO自动生成方法存根

}
// prepare视频
公共无效于prepared(MediaPlayer的MP){
    progressBarWait.setVisibility(View.GONE);

    //调整视频的大小
    //所以它在屏幕上适合
    INT videoWidth = player.getVideoWidth();
    INT videoHeight = player.getVideoHeight();
    浮动videoProportion =(浮点)videoWidth /(浮点)videoHeight;
    INT屏幕宽度= getWindowManager()getDefaultDisplay()的getWidth()。
    。INT screenHeight = getWindowManager()getDefaultDisplay()的getHeight();
    浮动screenProportion =(浮点)屏幕宽度/(浮点)screenHeight;
    android.view.ViewGroup.LayoutParams LP = surfaceViewFrame.getLayoutParams();

    如果(videoProportion> screenProportion){
        lp.width =屏幕宽度;
        lp.height =(INT)((浮点)屏幕宽度/ videoProportion);
    } 其他 {
        lp.width =(INT)(videoProportion *(浮点)screenHeight);
        lp.height = screenHeight;
    }
    surfaceViewFrame.setLayoutParams(LP);

    如果(!player.isPlaying()){
        player.start();
    }
    surfaceViewFrame.setClickable(真正的);
}

//回调在视频结束
公共无效onCompletion(MediaPlayer的MP){
    mp.stop();
    如果(updateTimer!= NULL){
        updateTimer.cancel();
    }
    完();
}

//暂停和恢复
公共无效的onClick(视图v){
    如果(v.getId()== R.id.surfaceViewFrame){
         如果(玩家!= NULL){
            如果(player.isPlaying()){
                player.pause();
                pause.setVisibility(View.VISIBLE);
            } 其他 {
                player.start();
                pause.setVisibility(View.GONE);
            }
        }
    }
}

}
 

I wonder if I can get a way to let video run via videoview in full screen?

I searched a lot and tried many ways such as:

  1. Apply theme in manifest:

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

    but that does not force the video to be in full screen.

  2. Apply in activity itself:

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

    also does not force the video to be in full screen.

The only way force video to full screen is:

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

This way it results in full screen video but it stretches the video itself (elongated video) ,

I'm not applying this improper solution to my videoview, so is there is any way to do it without stretching the video?

Video Class:

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

main.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>

解决方案

Like this you can set the properties of the video by yourself.

Use a SurfaceView (gives you more control on the view), set it to fill_parent to match the whole screen

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

then on your java code get the surface view and add your media player to it

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

set on your media player a onPreparedListener and manually calculate the desired size of the video, to fill the screen in the desired proportion avoiding stretching the video!

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

        }
    });

I modified this from a tutorial for video streaming that I followed some time ago, can't find it right now to reference it, if someone does please add the link to the answer!

Hope it helps!

EDIT

Ok, so, if you want the video to occupy the whole screen and you don't want it to stretch you will end up with black stripes in the sides. In the code I posted we are finding out what is bigger, the video or the phone screen and fitting it the best way we can.

There you have my complete activity, streaming a video from a link. It's 100% functional. I can't tell you how to play a video from your own device because I don't know that. I'm sure you will find it in the documentation here or 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);
            }
        }
    }
}

}

这篇关于无拉伸视频全屏videoview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆