简体   繁体   English

如何将视频文件(mp4)添加到我的android应用程序中?

[英]How do I add a video file (mp4) into my android application?

I want to be able to add a video I created that automatically starts playing when it's brought up on the screen, so far my code looks like this: 我希望能够添加我创建的视频,该视频在屏幕上显示时会自动开始播放,到目前为止,我的代码如下所示:

VideoView videoview = (VideoView) findViewById(R.id.button10);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoview.setVideoURI(uri);
videoview.start();

Currently nothing happens when I click and go on that screen. 当前,当我单击并转到该屏幕时,什么也没有发生。 It plays sound when I used media player but I really want to add this video. 当我使用媒体播放器时,它会播放声音,但我确实要添加此视频。 any help would be much appreciated. 任何帮助将非常感激。

Very important the use of setOnPreparedListener() method, and be sure to have a video called group_project.mp4 inside /raw folder. 使用setOnPreparedListener()方法非常重要,并确保在/raw文件夹中有一个名为group_project.mp4的视频。

  final VideoView videoview = (VideoView) findViewById(R.id.button10);
    Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
    videoview.setVideoURI(uri);
    videoview.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoview.start();    
            }
        });

Read the documentation: 阅读文档:

A MediaPlayer object must first enter the Prepared state before playback can be started. MediaPlayer对象必须首先进入“准备”状态,然后才能开始播放。 There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. 可以通过两种方式(同步或异步)达到Prepared状态:对方法的调用返回后将对象转移到Prepared状态的prepare()调用,或对prepareAsync()的调用(异步),它会在调用返回之后(几乎是正确的方式)首先将对象转移到Preparing状态,而内部播放器引擎将继续进行其余的准备工作,直到准备工作完成为止。 When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener (android.media.MediaPlayer.OnPreparedListener). 如果准备工作完成或当prepare()调用返回时,内部播放器引擎就会调用用户提供的回调方法,即OnPreparedListener接口的onPrepared(),前提是已通过setOnPreparedListener (android.media.MediaPlayer.OnPreparedListener)预先注册了OnPreparedListener。

VideoView videoview = (VideoView) findViewById(R.id.button10);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoview.start();

Try this before doing videoview.start() to ensure is ready. 在执行videoview.start()之前尝试此操作以确保已准备就绪。

videoview.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        videoview.start();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM