回答

2

我终于找到了如何在对话框中播放嵌入的youtube视频。

只需为您的对话框创建自定义布局,并在扩展DialogsFragment的类中对其进行膨胀。

在此自定义布局中,您添加了一个framelayout,它将成为youtubefragment容器。 不要忘记让你的Dialog实现YouTubePlayer界面,并且几乎完成。

最后的窍门使用getChildFragmentManager而不是supportFragmentManager作为在您的容器中添加youtube播放器片段的事务。

下面是一些代码,为您的对话框类:

public class AddMarkerFragment extends DialogFragment implements YouTubePlayer.OnInitializedListener{ 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    Log.d("ADDMARKERLIFECYCLE","ONCREATE"); 
    final View view = inflater.inflate(R.layout.fragment_add_marker, container, false); 

     prepareYoutubePlayer(view); 
} 

private void prepareYoutubePlayer(View view) { 
    youtubePlayerFragment = (YouTubePlayerSupportFragment) 
      getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment); 
    if (youtubePlayerFragment == null) { 
     youtubePlayerFragment = YouTubePlayerSupportFragment.newInstance(); 
     getChildFragmentManager().beginTransaction().add(R.id.youtube_fragment, youtubePlayerFragment).commit(); 
    } 
    youtubePlayerFragment.initialize(YoutubeConnector.KEY, this); 
} 
@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { 

    if (!wasRestored) { 
     youTubePlayer.loadVideo(footage.getYoutubeID()); 
     this.youtubePlayer=youTubePlayer; 
     /* youTubePlayer.setShowFullscreenButton(false); 
     youTubePlayer.*/ 
     youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL); 
    } 

} 

@Override 
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 
    Log.e(CreateExperienceActivity.class.getSimpleName(), "Ruh Roh!"); 
} 

为YouTube播放器参考可以使用完全实现自己的控制研究(播放/暂停等等等等)

希望它会帮助一些身体。

Regards,

1

至少不是通过延伸YouTubeBaseActivity,但通常根本没有必要。推荐的方法是让您的活动延伸AppCompatActivity并在您的活动中使用YouTubePlayerSupportFragment

直从docs

包含YouTubePlayerView片段。使用这个片段是 是播放YouTube视频的首选方式,因为您的活动确实不需要扩展该库提供的活动,如直接使用YouTubePlayerView那样, 就是这种情况。

在对话框中播放YouTube视频可能会很棘手或根本不可能。最好的方法是使用带有对话框主题/透明背景的Activity来模仿对话框的外观(同时还是一项活动)。