Android 全屏播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1574654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:18:11  来源:igfitidea点击:

playback video full screen

androidvideoplayback

提问by Javanator

I am trying to play a video in my app. It has to be embedded.

我正在尝试在我的应用中播放视频。它必须被嵌入。

I went through the "Play Video Files in Android"thread.

我浏览了“在 Android 中播放视频文件”线程。

I am able to play my video using VideoView as mentioned in this discussion. But there are a few problems.

我可以使用本讨论中提到的 VideoView 播放我的视频。但是有几个问题。

  1. I need full screen video, how do I stretch VideoView to full screen? Will that stretch the video too?

  2. I don't need the default play/forward/stop buttons at all. Basically I need continuous loop playing of the video.

  1. 我需要全屏视频,如何将 VideoView 拉伸到全屏?这也会拉伸视频吗?

  2. 我根本不需要默认的播放/前进/停止按钮。基本上我需要连续循环播放视频。

I tried the MediaPlayer class as in here, but it never worked. What should the string format look like if I have my video file in res/raw directory? I really don't want the video file played from sdcard. How can it be bundled along with the app?

我在这里尝试了 MediaPlayer 类,但它从未奏效。如果我的视频文件在 res/raw 目录中,字符串格式应该是什么样的?我真的不想从 sdcard 播放视频文件。它如何与应用程序捆绑在一起?

If any of these two approaches work, I will be better off.

如果这两种方法中的任何一种有效,我都会过得更好。

This is my code:

这是我的代码:

videoHolder = new VideoView(this); 
// videoHolder = (VideoView)findViewById(R.id.videoview); 
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
videoHolder.setLayoutParams(params); 
videoHolder.setMediaController(new MediaController(this)); 
setContentView(videoHolder); 
// 
// 
//// I tested and found that it works fine for .wmv, .3gp and .mp4 
//// videoHolder.setVideoURI(Uri.parse("file:///sdcard/video.3gp")); 
videoHolder.setVideoURI(Uri.parse("res/raw/demo.3gp")); 
videoHolder.requestFocus(); 
videoHolder.start(); 

Strangely the commented url works (the one with sdcard). The other one does not work. I have tried many combinations from "file:// res/raw/demo.3gp" to only "demo".

奇怪的是,注释的 url 有效(带有 sdcard 的那个)。另一个不起作用。我尝试了很多组合,从“file:// res/raw/demo.3gp”到“demo”。

What would be the correct string to access the file?

访问文件的正确字符串是什么?

回答by Javanator

No need to do any coding for playing video to full screen mode

无需进行任何编码即可将视频播放到全屏模式

Apply the following layout format over the xml containing the videoview it will for sure will play the video in full screen mode. as it is running mine. :) Hope it helps

在包含视频视图的 xml 上应用以下布局格式,它肯定会以全屏模式播放视频。因为它正在运行我的。:) 希望能帮助到你

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" >
   <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>
 </RelativeLayout>

Plus adding the mediacontroller will also give you the controller to use.

此外,添加 mediacontroller 还将为您提供要使用的控制器。

回答by Someone

I have solved the problem:

我已经解决了这个问题:

1. To remove the continue/pause buttons remove the media controller. For the looping issue put OnCompletionListenerso that when the video reaches the end, it starts again:

1. 要移除继续/暂停按钮,请移除媒体控制器。对于循环问题OnCompletionListener,当视频结束时,它会再次开始:

videoView.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            videoView.start();
        }
        });

2. To resize the video, override method onMeasure()in VideoViewlike this:

2.要调整视频大小onMeasure()VideoView像这样覆盖方法:

public class MyVideoView extends VideoView {

        public MyVideoView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {
             setMeasuredDimension(480,800);
        }

    }

回答by Brian

I know this thread is a few weeks old, but you don't have to copy the video file to the SD card just to play it. Use the following but insert your package name instead of "com.yourcompany.yourproject":

我知道这个线程已经有几个星期了,但您不必为了播放它而将视频文件复制到 SD 卡。使用以下但插入您的包名称而不是“com.yourcompany.yourproject”:

videoView.setVideoURI(Uri.parse("android.resource://com.yourcompay.yourproject/" + R.raw.yourvideoresource));

videoView.setVideoURI(Uri.parse("android.resource://com.yourcompay.yourproject/" + R.raw.yourvideoresource));

回答by Pir Fahim Shah

If you want to play video in full screen, then just add these line of code in your Androidmanifest.xml file, in the activity where this video is going to play. Just add these two lines in android manifest file.

如果你想全屏播放视频,那么只需在你的 Androidmanifest.xml 文件中添加这些代码行,在这个视频将要播放的活动中。只需在android清单文件中添加这两行即可。

 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
 android:configChanges="orientation|screenSize"

Like in my androidmanifest.xml file i have added it

就像在我的 androidmanifest.xml 文件中一样,我已经添加了它

  <activity
        android:name="com.appliconic.straminglivevideo.MainActivity"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

回答by genko stain champion

Modified @Javanator's answer a bit, this won't stretch the video:

稍微修改了@Javanator 的回答,这不会拉伸视频:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" >
   <VideoView android:id="@+id/myvideoview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerVertical="true"
              android:layout_centerHorizontal="true">
    </VideoView>
 </RelativeLayout>

Hope this helps someone :)

希望这对某人有所帮助:)