Я использую Firebase
, чтобы позволить пользователям сохранять URL для воспроизведения Youtube
видео.Но если видео загружено не полностью, нажмите кнопку, и при следующем действии произойдет сбой.Если видео уже загружено, независимо от того, является ли URL правильным или нет, нажатие кнопки на следующей странице будет работать.
Я поместил видео процесса в youtube
.
Когда загружается видео с YouTube, нажатие кнопки приводит к сбою
Вот мой код:
public class PersonalPageActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,YouTubePlayer.PlaybackEventListener,YouTubePlayer.PlayerStateChangeListener {
private FirebaseAuth mAuth;
ImageView ImageView;
TextView NameTextview,Emailtextview,text;
private String name;
ImageButton videobtn;
YouTubePlayerView playerview;
String API_KEY="AIzaSyAJbnIeJT7HuLHtXHq6eT43SSYialtfElM";
String VIDEO_ID="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personal_page);
ImageView=findViewById(R.id.Imageview);
NameTextview=findViewById(R.id.NameTextview);
Emailtextview=findViewById(R.id.Emailtextview);
text=findViewById(R.id.text);
playerview=findViewById(R.id.playerview);
playerview.initialize(API_KEY,this);
videobtn=findViewById(R.id.videobtn);
mAuth = FirebaseAuth.getInstance();
final FirebaseUser currentUser = mAuth.getCurrentUser();
NameTextview.setText(currentUser.getDisplayName());
Emailtextview.setText(currentUser.getEmail());
Profile profile = Profile.getCurrentProfile();
Uri userPhoto = profile.getProfilePictureUri(350, 350);
Glide.with(PersonalPageActivity.this)
.load(userPhoto.toString())
.into(ImageView);
videobtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it = new Intent();
it.setClass(PersonalPageActivity.this, VideoPageActivity.class);
startActivity(it);
finish();
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.setPlayerStateChangeListener(this);
youTubePlayer.setPlaybackEventListener(this);
if(!b){
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myVideo = database.getReference("Youtube");
mAuth = FirebaseAuth.getInstance();
final FirebaseUser currentUser = mAuth.getCurrentUser();
Query q = myVideo.orderByChild("mail").equalTo(currentUser.getEmail());
q.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
VIDEO_ID=ds.child("url").getValue().toString();
youTubePlayer.cueVideo(VIDEO_ID);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onPlaying() {
}
@Override
public void onLoaded(String s) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
@Override
public void onAdStarted() {
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onStopped() {
}
@Override
public void onPaused() {
}
@Override
public void onSeekTo(int i) {
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
public class Video {
public String mail;
public String url;
public Video(){}
public Video(String mail, String url){
this.mail = mail;
this.url = url;
}
}
}
public class VideoPageActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
EditText editText;
Button btn;
ImageButton Imgbtn;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myVideo = database.getReference("Youtube");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_page);
editText=findViewById(R.id.editText);
btn=findViewById(R.id.btn);
Imgbtn=findViewById(R.id.Imgbtn);
Imgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(VideoPageActivity.this ,R.style.AlertDialogStyle);
alert.setIcon(R.drawable.youtube);
alert.setTitle("如何新增個人寵物Youtube Video");
alert.setMessage(" 請先到想上傳的Youtube影片頁面上\n\n"+"以此網站為例\n\n"
+ "https://www.youtube.com/watch?v=d7VIW1vYQRA\n\n" +
"僅需複製 = 後面全部即可\n\n"+"即為d7VIW1vYQRA\n\n"+"再張貼至空格處\n\n"+"點選確定即可新增\n\n"+"若無法新增,請檢查步驟是否有誤");
alert.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(editText.getText().toString().matches("")) {
Toast toast = Toast.makeText(VideoPageActivity.this, "您並未輸入網址哦!", Toast.LENGTH_SHORT);
toast.show();
}
else{
AlertDialog.Builder alert = new AlertDialog.Builder(VideoPageActivity.this ,R.style.AlertDialogStyle);
alert.setIcon(R.drawable.doghouse);
alert.setTitle("Take Me Home");
alert.setMessage("已新增網址");
alert.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mAuth = FirebaseAuth.getInstance();
final FirebaseUser currentUser = mAuth.getCurrentUser();
myVideo.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
if ((ds.child("mail").getValue().equals(currentUser.getEmail())) ){
String key = ds.getKey();
Video s5 = new Video();
s5.url = editText.getText().toString();
s5.mail=currentUser.getEmail();
myVideo.child(key).setValue(s5);
}
else{
Video s1 = new Video();
s1.url = editText.getText().toString();
s1.mail=currentUser.getEmail();
String k = myVideo.push().getKey();
myVideo.child(k).setValue(s1);
}
return;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Intent it= new Intent();
it.setClass(VideoPageActivity.this,PersonalPageActivity.class);
startActivity(it);
finish();
}
});
alert.show();
}
}
});
}
public static class Video {
public String mail;
public String url;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 攔截返回鍵
Intent it= new Intent();
it.setClass(VideoPageActivity.this,PersonalPageActivity.class);
startActivity(it);
finish();
}
return true;
}
}
Журнал сбоев
--------- beginning of crash
12-19 14:47:40.214 3331-3331/com.example.jakehuang.takemehome E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jakehuang.takemehome, PID: 3331
java.lang.IllegalStateException: This YouTubePlayer has been released
at hue.a(SourceFile:98)
at hue.a(SourceFile:209)
at acye.onTransact(SourceFile:16)
at android.os.Binder.transact(Binder.java:604)
at com.google.android.youtube.player.internal.d$a$a.a(Unknown Source:23)
at com.google.android.youtube.player.internal.s.cueVideo(Unknown Source:2)
at com.google.android.youtube.player.internal.s.cueVideo(Unknown Source:1)
at com.example.jakehuang.takemehome.PersonalPageActivity$2.onDataChange(PersonalPageActivity.java:128)
at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
12-19 14:47:42.512 3331-3581/com.example.jakehuang.takemehome E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
12-19 14:47:42.515 3331-3581/com.example.jakehuang.takemehome I/chatty: uid=10089(u0_a89) Binder:3331_5 identical 3 lines
12-19 14:47:42.515 3331-3581/com.example.jakehuang.takemehome E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
12-19 14:47:55.085 3331-3336/com.example.jakehuang.takemehome I/zygote: Do partial code cache collection, code=245KB, data=133KB
12-19 14:47:55.086 3331-3336/com.example.jakehuang.takemehome I/zygote: After code cache collection, code=242KB, data=132KB
Increasing code cache capacity to 1024KB
12-19 14:48:08.960 3331-3351/com.example.jakehuang.takemehome W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms
12-19 14:48:11.418 3331-3581/com.example.jakehuang.takemehome D/FirebaseAuth: Notifying id token listeners about user ( mDhIgMq88SModK2CLEGoS18X52c2 ).