在我的Fragment
班级中,PlaybackEventListener
和PlayerStateChangeListener
不起作用。它不会显示Toast。
这是我的代码:
进口:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log; import android.view.LayoutInflater;
import android.view.View; import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
脚本:
private static final String API_KEY = "-------";
private static String VIDEO_ID = "------";
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.you_tube_api, container, false);
final YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider provider, final YouTubePlayer player, boolean wasRestored) {
/*Listener*/
final YouTubePlayer.PlaybackEventListener mPlaybackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
final YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
Toast.makeText(getActivity(),"END",Toast.LENGTH_LONG).show();
}
@Override
public void onVideoStarted() {
}
};
player.setPlayerStateChangeListener(mPlayerStateChangeListener);
player.setPlaybackEventListener(mPlaybackEventListener);
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(VIDEO_ID);
player.play();
}
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
return rootView;
}
有人能帮助我吗?
答案 0 :(得分:1)
您可以尝试以下方式:
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider provider, final YouTubePlayer player, boolean wasRestored) {
/*Listener*/
player.PlaybackEventListener mPlaybackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
player.PlayerStateChangeListener mPlayerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
Toast.makeText(getActivity(),"END",Toast.LENGTH_LONG).show();
}
@Override
public void onVideoStarted() {
}
};
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(VIDEO_ID);
player.play();
}
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
我希望这会对你有所帮助。
答案 1 :(得分:0)
以下内容对我有用...
InitializationSuccess内部:
player.setPlaybackEventListener(
new YouTubePlayer.PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
}
);
player.setPlayerStateChangeListener(
new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
}
);