VideoView 不工作并且黑屏
VideoView is not working and black screen
我想在我的 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);
}
});
}
}
带有视频视图的代码
没有视频视图的代码
视频路径和错误信息
试试这个方法:
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;
}
});
我想在我的 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);
}
});
}
}
带有视频视图的代码
没有视频视图的代码
视频路径和错误信息
试试这个方法:
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;
}
});