资源中的视频未显示

时间:2018-01-22 05:28:17

标签: android video android-resources android-videoview

我正在尝试从VideoView内的应用资源中显示mp4视频,引用this answerthis tutorial我使用了以下方式:

// make the videoView visible
storyVideo.setVisibility(View.VISIBLE);
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setBackgroundColor(Color.BLACK);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// Start The Video
storyVideo.start();

其中story_media4存储在资源中的原始文件中:

enter image description here

3 个答案:

答案 0 :(得分:1)

您应该删除storyVideo.setBackgroundColor(Color.BLACK);。 我测试您的代码并删除setBackgroundColor(Color.BLACK)并解决此问题。 请测试以下代码OnPreparedListener()

 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("LOG","Video test");
        }
    });

请移除videoView.start()中的onPrepared并使用Log()进行测试。

答案 1 :(得分:1)

实际上视频正在显示但没有显示,在this answer的帮助下,我可以在视频URI设置和视频准备的侦听器方法上设置video_view.setZOrderOnTop(true);后显示视频:

// set Video Preferences
storyVideo.requestFocus();
storyVideo.setZOrderOnTop(true);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// when the video is ready for display
storyVideo.setOnPreparedListener(onVideoPrepared);

private MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        storyVideo.setZOrderOnTop(true);
        // to start the video
        storyVideo.start();
    }
};

更新: 对于上述方法,视频将在所有视图的顶部进行,如果 - 原因 - 您希望视频被一个或多个视图(如贴纸或描述文本)覆盖,最好的方法是设置背景视频到TRANSPARENT:

storyVideo.setBackgroundColor(Color.TRANSPARENT);

答案 2 :(得分:0)

public class Videoplay extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videoplay);


        VideoView vidView = (VideoView)findViewById(R.id.myVideo);

        MediaController vidControl = new MediaController(this);

        String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
        Uri vidUri = Uri.parse(vidAddress);

        Toast.makeText(Videoplay.this, "Loading Teaser",Toast.LENGTH_LONG).show();

        vidView.setVideoURI(vidUri);

        vidView.start();


        vidControl.setAnchorView(vidView);

        vidView.setMediaController(vidControl);


    }
相关问题