我已经学会了在https://www.sitepoint.com/using-the-youtube-api-to-embed-video-in-an-android-app/
上使用youtube-API制作播放器当练习跳到某一秒
时出现错误java.lang.NullPointerException:尝试调用接口方法' void com.google.android.youtube.player.YouTubePlayer.seekToMillis(int)'在空对象引用上
这是我的编码
public class SitepointSample extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youtubeView;
private MyPlayerStateChangeListener playerStateChangeListener;
private MyPlaybackEventListener playbackEventListener;
private YouTubePlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sitepoint_sample);
final EditText seekToText = (EditText) findViewById(R.id.seek_to_text);
Button seekToButton = (Button) findViewById(R.id.seek_to_button);
seekToButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int skipToSecs = Integer.valueOf(seekToText.getText().toString());
player.seekToMillis(skipToSecs * 1);
}
});
playbackEventListener = new MyPlaybackEventListener();
playerStateChangeListener = new MyPlayerStateChangeListener();
youtubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youtubeView.initialize(Config.YOUTUBE_API_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer,
boolean wasRestored) {
this.player = player;
if (!wasRestored) {
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
youTubePlayer
.cueVideo("fhWaJi1Hsfo"); //plays video_id == https://www.youtube.com/watch?v=fhWaJi1Hsfo
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
youtubeView.initialize(Config.YOUTUBE_API_KEY, this);
}
}
protected YouTubePlayerView getYouTubePlayerProvider() {
return youtubeView;
}
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
任何人都可以处理吗?
答案 0 :(得分:0)
请尝试使用此代码:
PlayFragment(start, stop){
player.seekTo(40);
while(player.getCurrentTime() !> 60)
{
sleep(1000);
}
player.stop();
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}