使用Android Youtube api我想在我的应用主要活动中播放两个视频而不使用片段我们在一个活动中对这个多个Youtube播放器有同样的问题,但是没有可接受的答案,请帮助我们.
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/video1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/video2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package com.apps.you;
import android.os.Bundle;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends YouTubeBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((YouTubePlayerView) findViewById(R.id.video1)).initialize("AIzaSyBmb9Yqu9vEBXjNOrBmZZ__6s12RH5RSv0", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider provider1, YouTubePlayer player1, boolean restored1) {
if (!restored1) {
player1.cueVideo("Daa38ruXHxE");
}
}
@Override
public void onInitializationFailure(Provider provider1, YouTubeInitializationResult …Run Code Online (Sandbox Code Playgroud) 我想在彼此之上安装两个YouTubePlayerView.他们都出现了,但只有一个似乎提示任何视频.第二个(youTubeView2)只是黑色.没有错误.知道我做错了什么吗?
JSONObject data;
JSONArray array;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview_demo);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
YouTubePlayerView youTubeView2 = (YouTubePlayerView) findViewById(R.id.youtube_view2);
youTubeView2.initialize(DeveloperKey.DEVELOPER_KEY, this);
//JSONObject json = getJSONfromURL("http://gdata.youtube.com/feeds/api/users/rhettandlink2/uploads?v=2&alt=jsonc");
try{
JSONObject json = new RetreiveFeedTask().execute().get();
data = json.getJSONObject("data");
array = data.getJSONArray("items");
//Log.d("num vids", ""+id[1].toString());
for (int i = 0; i < array.length(); i++) {
final JSONObject item = array.getJSONObject(i);
final JSONObject player = item.getJSONObject("player");
final String url = player.getString("default");
// The url is that of the video to …Run Code Online (Sandbox Code Playgroud) 我想在Horizontal ScrollView中显示多个YouTube视频.为此,我正在动态添加YouTubePlayerFragments.这是我的代码片段:
//set videos
int count = 0;
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
for (final Video video : mVideos) {
//create an instance of a video fragment
final VideoFragment fragment = new VideoFragment();// VideoFragment extends YouTubePlayerFragment
Bundle bundle = new Bundle();
if (null != mVideos && 0 < mVideos.size()) {
bundle.putString(VideoFragment.KEY_VIDEO_KEY, video.getKey());
}
fragment.setArguments(bundle);
FrameLayout frameLayout = new FrameLayout(getActivity());
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(900, 500));
frameLayout.setPadding(10, 0, 10, 0);
frameLayout.setId(12345 + ++count);
mLayoutTrailers.addView(frameLayout);//mLayoutTrailers is a LinearLayout inside my Horizontal ScrollView …Run Code Online (Sandbox Code Playgroud) android horizontalscrollview android-fragments android-youtube-api