2013-05-28 63 views
6

請耐心等待,因爲我對Android非常陌生。我正嘗試在片段類中初始化Youtube播放器。谷歌已經提供了方法,其中去活動來初始化如下:初始化片段中的YouTube播放器

package com.examples.youtubeapidemo; 

import com.google.android.youtube.player.YouTubeBaseActivity; 
import com.google.android.youtube.player.YouTubePlayer; 
import com.google.android.youtube.player.YouTubePlayerView; 

import android.os.Bundle; 


public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity { 

    @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); 
    } 

    @Override 
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, 
     boolean wasRestored) { 
    if (!wasRestored) { 
     player.cueVideo("wKJ9KzGQq0w"); 
    } 
    } 

    @Override 
    protected YouTubePlayer.Provider getYouTubePlayerProvider() { 
    return (YouTubePlayerView) findViewById(R.id.youtube_view); 
    } 

} 

我嘗試使用此代碼,但它不工作:

public class TabFragment5 extends YouTubePlayerFragment implements YouTubePlayer.OnInitializedListener { 
    /** (non-Javadoc) 
    * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 
    */ 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
      YouTubePlayerFragment yf = newInstance(); //new YouTubePlayerFragment(); 
     yf.initialize("dsds", this); 
     return (LinearLayout)inflater.inflate(R.layout.tab_frag4_layout, container, false); 
    } 

    @Override 
    public void onInitializationFailure(Provider arg0, 
      YouTubeInitializationResult arg1) { 
     // TODO Auto-generated method stub 

    } 

     @Override 
     public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, 
      boolean wasRestored) { 
     if (!wasRestored) { 
      player.cueVideo("nCgQDjiotG0"); 
     } 
     } 
} 

我將不勝感激,如果有人能提供相當於API分段。

感謝

回答

4

嘗試使用下面的例子:

fragment.xml之

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".VideoFragment" > 
    <fragment 
     android:name="com.google.android.youtube.player.YouTubePlayerFragment" 
     android:id="@+id/youtubeplayerfragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 



片斷創建

VideoFragment videoFragment = VideoFragment.newInstance(VIDEO_ID); 


VideoFragment

public class VideoFragment extends YouTubePlayerSupportFragment { 
    public static final String DEVELOPER_ANDROID_KEY = "KEY"; 

    public static VideoFragment newInstance(String url) { 
     VideoFragment videoFragment = new VideoFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putString("url", url); 
     videoFragment.setArguments(bundle); 
     videoFragment.init(0); 
     return videoFragment; 
    } 

    private void init(final int time) { 
     initialize(DEVELOPER_ANDROID_KEY, new OnInitializedListener() { 

      @Override 
      public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { 
      } 

      @Override 
      public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer 
                  player, boolean wasRestored) { 
       mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); 
       id = getArguments().getString("url"); 
       if (!wasRestored) { 
        mPlayer.loadVideo(id, time); 
       } 
      } 
     }); 
    } 
}  
+0

在該片段被破壞,並得到恢復的情況下(例如使用不保留活動設置),我的mPlayer不再有效。初始化沒有達到onInitilizationSuccess了......因爲Youtube可以恢復它自己的狀態。我怎樣才能恢復mPlayer? – Elye

+1

@Elye:你找到了解決方案嗎? –

+0

@Elye它看起來像youtube爲兩個方向使用相同的佈局,所以onConfigurationChanged中沒有更改 – user5599807

相關問題