gpt4 book ai didi

java - VideoView 不工作并且黑屏

转载 作者:行者123 更新时间:2023-12-02 11:06:24 25 4
gpt4 key购买 nike

我想在我的started_activity背景下运行视频 View 。但加载失败。

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

我遵循这个视频教程: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);
}
});
}
}

带有视频 View 的代码

enter image description here

没有视频 View 的代码

enter image description here

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

最佳答案

试试这个方法:

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

您还可以设置setOnErrorListener来检查错误:

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

关于java - VideoView 不工作并且黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com