我正在尝试在详细信息活动中加载YouTube视频。 我正在使用体系结构组件设计,首先必须从Web服务器获取视频密钥,然后才能加载它。
我的问题是: onInitializationSuccess在onCreate方法之前起作用,因此将在获取视频密钥之前加载视频。
我需要什么: 最好的情况下,实现YoutubePlayer的最佳方法。
我的课程
public class MovieDetailsActivity extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener{
private final String Tag = this.getClass().getSimpleName();
private String m_movieId;
private ImageView m_moviePoster;
private MovieViewModel m_movieViewModel;
private Context m_context;
private CollapsingToolbarLayout m_collapsingToolBarLayout;
private YouTubePlayerFragment m_youtubePlayerFragment;
private LiveData<Movie> m_movie;
private String m_videoPath;
private boolean m_isPlayerInitialized = false;
private YouTubePlayer.OnInitializedListener m_playerOnInitializedListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_context = getBaseContext();
setContentView(R.layout.movie_details);
if(getIntent().hasExtra("movieId")){
m_movieId = getIntent().getStringExtra("movieId");
setupView();
}
else{
Log.d(Tag, "Invalid data from adapter.");
}
}
private void setupView(){
m_youtubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
m_youtubePlayerFragment.initialize(Settings.Google_API_KEY, this);
m_collapsingToolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
m_movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
m_moviePoster = (ImageView) findViewById(R.id.imageView);
m_movieViewModel.getMovie(m_movieId).observe(this, new Observer<Movie>() {
@Override
public void onChanged(@Nullable Movie movie) {
Picasso.with(m_context).load("http://image.tmdb.org/t/p/w500" + movie.getPosterPath())
.placeholder(R.drawable.ic_launcher_background)
.into(m_moviePoster);
m_collapsingToolBarLayout.setTitle(movie.getTitle());
m_videoPath = "http://www.youtube.com/watch?v=" + movie.getVideos().getResults().get(0).getKey();
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if(!b){
m_isPlayerInitialized = true;
youTubePlayer.cueVideo(m_videoPath);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if(youTubeInitializationResult.isUserRecoverableError()){
Log.d(Tag ,"Loading Video Error" );
}
else{
//;
}
}
}