VideoView无效,黑屏

时间:2018-06-19 10:53:48

标签: java android android-videoview

我想在我的started_activity背景下运行视频视图。但它无法加载。

屏幕是黑色的,如果我导航或将我的页面推送到另一个页面2次,它将显示应用程序没有响应警报。

我关注此视频教程:https://www.youtube.com/watch?v=tPeDn18FrGY&t=210s

started_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StartedActivity">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <VideoView
            android:id="@+id/videoView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true" />

        <Button
            android:id="@+id/btnToLoginPage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="52dp"
            android:text="Button"/>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

ActiviyStarted.java

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class StartedActivity extends AppCompatActivity {

    private VideoView videoview;

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

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

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

        Button btn = (Button) findViewById(R.id.btnToLoginPage);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(StartedActivity.this, LoginActivity.class);
                StartedActivity.this.startActivity(i);
            }
        });
    }
}

带视频观看的代码

enter image description here

没有视频观看的代码

enter image description here

视频路径和错误消息 enter image description here

1 个答案:

答案 0 :(得分:1)

Try this way:

videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        videoview.stopPlayback();
        videoview.setVideoURI(uri);
        videoview.start();
    }
});
videoview.setVideoURI(uri);
videoview.start();

You can also set setOnErrorListener to check for the error:

videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        //check error here
        return false;
    }
});