Android 尝试从原始文件夹(VideoView)播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3609219/
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 10:57:57  来源:igfitidea点击:

Trying to play video from raw folder (VideoView)

android

提问by taraloca

I can play a video from the Internet by inserting the URL like below:

我可以通过插入如下 URL 来播放来自 Internet 的视频:

mPath   = Uri.parse("http://commonsware.com/misc/test2.3gp");
mVid.setVideoURI(mPath);
mVid.requestFocus();
mVid.start();

But now I have a video in my raw folder so the path is res/raw/testing.3gp. The code below doesn't work, and I've tried some other ways too to no avail.

但是现在我的原始文件夹中有一个视频,所以路径是 res/raw/testing.3gp。下面的代码不起作用,我也尝试了其他一些方法无济于事。

mPath   = Uri.parse("../../res/raw/testing.3gp");

Any suggestions?

有什么建议?

回答by Alex Bush

I had the same problem. This worked for me:

我有同样的问题。这对我有用:

Uri video = Uri.parse("android.resource://com.pac.myapp/raw/master");

So as you see you have 3 parts of the uri: 1) "android.resource://" 2) "com.pac.myapp" 3) "/raw/master"

因此,如您所见,您有 uri 的 3 个部分:1)“android.resource://” 2)“com.pac.myapp” 3)“/raw/master”

"master" is the name of your video

“master”是您视频的名称

回答by 3akat

this works for me

这对我有用

 String videoName = nameWithoutFileExtention;

 int id = getResources().getIdentifier(videoName, "raw", getActivity().getPackageName());

 final String path = "android.resource://" + getActivity().getPackageName() + "/" + id;

 vvBgVideo.setVideoURI(Uri.parse(path));

回答by Tenzin_Programmer

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button)this.findViewById(R.id.playvideo);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VideoView vid=(VideoView)findViewById(R.id.video);
                vid.setMediaController(new MediaController(MainActivity.this));
                Uri video = Uri.parse("android.resource://com.example.tenzinthinley.video/raw/ed");
                vid.setVideoURI(video);
                vid.requestFocus();
                vid.start();
            }
        });
    }
}

Change my name if this doesn't works. 'ed' is the video file name.

如果这不起作用,请更改我的名字。'ed' 是视频文件名。

回答by DeepakPanwar

You just need to locate song in raw folder under resource folder. if it is link then

您只需要在资源文件夹下的原始文件夹中找到歌曲。如果是链接,那么

 private String urlVideo ="http://www.pocketjourney.com/downloads/pj/video/famous.3gp";

    //Make uri from song located in raw folder
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.shakebooty);
        player.setUpVideoFrom(uri.toString());

        public void setUpVideoFrom(String source) throws IllegalArgumentException,
                IllegalStateException, IOException {

            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

//Only to check if you want to play song from url
            if (source.contains("http"))
            {
            mPlayer.setDataSource(source);
            }
//If want to play song from uri you created from song in raw folder
        else {
             mPlayer.setDataSource(ctx, source);
             }

        }

Enjoy playing video in surface view

享受在表面视图中播放视频的乐趣