我想通过意图将YouTubePlayer对象发送到我的服务。这是我如何将YouTubePlayer对象发送到我的服务的代码,但它给出了错误。我该如何解决?请帮忙。
java.lang.ClassCastException:com.pierfrancescosoffritti.androidyoutubeplayer.player.a无法转换为android.os.Parcelable
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYouTubePlayerView = findViewById(R.id.youtube_player_view);
mYouTubePlayerView.initialize(new YouTubePlayerInitListener() {
@Override
public void onInitSuccess(@NonNull final YouTubePlayer youTubePlayer) {
youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
startService(youTubePlayer);
}
});
}
},true);
}
private void startService(YouTubePlayer player)
{
Intent intent = new Intent(MainActivity.this, ExampleService.class);
intent.putExtra("player", (Parcelable) player);
startService(intent);
}
服务等级
public class ExampleService extends Service {
YouTubePlayer mYouTubePlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mYouTubePlayer = intent.getParcelableExtra("player");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mYouTubePlayer.loadVideo("mHM5zyRZqpw",0);
Intent main_intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,main_intent,0);
Notification notification = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.setContentTitle("Example service")
.setContentText("Content text")
.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(R.drawable.ic_launcher_background, "Stop service", pi)
.setContentIntent(pi)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}