You have to extend not only
YouTubeBaseActivity but also Implement YouTubePlayer.OnInitializedListener
and implement the reequired Methods.
Question
Hey Everyone I am developing an application that plays YouTube Video, I extend YoutubeBaseActivity for the activity that should play the video but I keep getting an error when the activity is reached. Below is log cat error.
04-30 09:11:18.367: E/AndroidRuntime(26832): FATAL EXCEPTION: main
04-30 09:11:18.367: E/AndroidRuntime(26832): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.dclmhq.streamingapp/com.dclmhq.disuplytbvid.PlaySelectedVideo}: java.lang.ClassCastException: com.dclmhq.disuplytbvid.PlaySelectedVideo cannot be cast to com.google.android.youtube.player.YouTubePlayer$OnInitializedListener
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.os.Looper.loop(Looper.java:153)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.main(ActivityThread.java:5297)
04-30 09:11:18.367: E/AndroidRuntime(26832): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:11:18.367: E/AndroidRuntime(26832): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-30 09:11:18.367: E/AndroidRuntime(26832): at dalvik.system.NativeStart.main(Native Method)
04-30 09:11:18.367: E/AndroidRuntime(26832): Caused by: java.lang.ClassCastException: com.dclmhq.disuplytbvid.PlaySelectedVideo cannot be cast to com.google.android.youtube.player.YouTubePlayer$OnInitializedListener
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.dclmhq.disuplytbvid.PlaySelectedVideo.onCreate(PlaySelectedVideo.java:33)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.Activity.performCreate(Activity.java:5122)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-30 09:11:18.367: E/AndroidRuntime(26832): ... 11 more
Here is my code
public class PlaySelectedVideo extends YouTubeBaseActivity {
MediaPlayer mPlayer;
Intent receInt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playyoutubevideo);
receInt = getIntent();
TextView titleView = (TextView) findViewById(R.id.playerTitle);
TextView descView = (TextView) findViewById(R.id.playerDesc);
titleView.setText(receInt.getStringExtra("com.xxx.disuplytbvid.TITLE"));
descView.setText(receInt.getStringExtra("com.xxx.disuplytbvid.DESCRIPTION"));
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(API_KEY, (YouTubePlayer.OnInitializedListener)this);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
String id = receInt.getStringExtra("com.XXX.disuplytbvid.vID");
player.cueVideo(id);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
And my Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/playerTitle"
android:textAppearance="@android:style/TextAppearance.Large"
android:textStyle="bold"
android:gravity="center"
android:text="@string/playerview_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/playerDesc"
android:textAppearance="@android:style/TextAppearance.Medium"
android:gravity="center"
android:text="@string/playerview_desc"/>
</LinearLayout>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Solution 2
You have to extend not only
YouTubeBaseActivity but also Implement YouTubePlayer.OnInitializedListener
and implement the reequired Methods.
OTHER TIPS
Try YouTubeFailureRecoveryActivity instead of YouTubeBaseActivity for extending your PlaySelectedVideo activity.
and instead of this
youTubeView.initialize("AIzaSyAYBzv-h4SrKw16L-IXczrkXkVeX21fyU8", (YouTubePlayer.OnInitializedListener)this);
try
youTubeView.initialize("AIzaSyAYBzv-h4SrKw16L-IXczrkXkVeX21fyU8", YouTubeBaseActivity.this);