当我们点击视频控制面板中的视频图标时,我想要的是My videos
正常加载,然后播放视频。
我被困住了。我可以加载一个视频但我无法在一个活动序列中加载多个视频。
我想加载下一个:
这样我就可以在一个活动中加载很多视频。
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.soleyman.happybirthdayurj.VideoActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/unlisted"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Info"
android:onClick="unlistClick"
/>
<fragment
android:id="@+id/youtube_player_fragment1"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="280dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"/>
<fragment
android:id="@+id/youtube_player_fragment2"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="280dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
package com.example.soleyman.happybirthdayurj;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
public class VideoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayer sPlayer;
YouTubePlayerFragment playerFragment;
private String[] ytNames = {"R.id.youtube_player_fragment1", "R.id.youtube_player_fragment2"};
private String[] vdoID = {"PiKxS3P5o2Y", "PwC-fFIGjZM"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show Actionbar
ActionBar actionBar = getSupportActionBar();
actionBar.show();
this.setTitle("My Favorite Panda Videos");
setContentView(R.layout.activity_video);
playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(ytNames[]); // currently it's showing error
playerFragment.initialize(PlayerConfig.API_KEY, this);
}
private void playVideo(String videoId) {
if ( sPlayer != null ) {
sPlayer.loadVideo(videoId);
sPlayer.cueVideo(videoId);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
sPlayer = youTubePlayer;
playVideo(vdoID[]); // currently it's showing error
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
sPlayer = null;
}
// UnlistedActivity Activity
public void unlistClick(View view) {
Intent intent = new Intent(VideoActivity.this, UnlistedActivity.class);
startActivity(intent);
}
}
非常感谢任何帮助。如果您有任何建议或不同的方法/方法来做同样的事情,请向我建议。
P.S。:已经花了1天时间来解决它
答案 0 :(得分:0)
ytNames[]
和vdoID[]
都是数组。
您尝试使用它们的所有地方都需要提供索引。就像第一个ytNames[0]
一样。
这是非常基本的编程,你可能想要了解数组:
https://code.tutsplus.com/tutorials/learn-java-for-android-development-working-with-arrays--mobile-2894