Youtube API “An error occurred while initializing the YouTube player.”

删除回忆录丶 提交于 2021-02-08 12:37:11

问题


I'm using Youtube API to have simple playerView on my application. I did what the API said to do but I keep getting "application is stopped" message. I don't really know the exact english phrase since I'm using a Korean Phone. Hope that's the right phrase in English.

I put Youtubeandroidplayerapi.jar in libs and built the path and have the unnecessary components in library.

Here is Menu.java.

package com.hobak.sci;

import com.hobak.sci.DeveloperKey;
import com.hobak.sci.R;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import android.os.Bundle;

public class Menu extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
        YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo("2LHv1FPd1Ec");
    }
}

protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult arg1) {
    // TODO Auto-generated method stub

}

}

and here is my xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFACD"
    android:orientation="vertical" >


  <com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />



</LinearLayout>

and here is my logcat

03-17 15:55:51.260: E/AndroidRuntime(5277): FATAL EXCEPTION: main
03-17 15:55:51.260: E/AndroidRuntime(5277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hobak.sci/com.hobak.sci.Menu}: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.google.android.youtube.api.service.START }
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1961)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1152)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.os.Looper.loop(Looper.java:137)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread.main(ActivityThread.java:4453)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at java.lang.reflect.Method.invoke(Method.java:511)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at dalvik.system.NativeStart.main(Native Method)
03-17 15:55:51.260: E/AndroidRuntime(5277): Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.google.android.youtube.api.service.START }
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ContextImpl.bindService(ContextImpl.java:1164)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.content.ContextWrapper.bindService(ContextWrapper.java:370)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.google.android.youtube.player.internal.r.e(Unknown Source)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.google.android.youtube.player.YouTubeBaseActivity$a.a(Unknown Source)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.google.android.youtube.player.YouTubePlayerView.initialize(Unknown Source)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at com.hobak.sci.Menu.onCreate(Menu.java:21)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.Activity.performCreate(Activity.java:4467)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-17 15:55:51.260: E/AndroidRuntime(5277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)
03-17 15:55:51.260: E/AndroidRuntime(5277):     ... 11 more

回答1:


I was having this problem. Then I just added

<uses-permission android:name="android.permission.INTERNET"/> into the AndroidManifest.xml in the <manifest> tag. The app worked fine after that.




回答2:


I've fixed this problem by doing steps below:

  • Clean project then Rebuild Project
  • Run the app in a device which has an Youtube app with a version higher than 4.2.16.



回答3:


maybe, it seems to developer key problem. sample DEVELOPER_KEY key that located in DeveloperKey.java will be not work. first, register a new developer key. (https://code.google.com/apis/console) then, replace this with a valid key.




回答4:


Just update your youtube app to the latest version in your phone and it will work perfect




回答5:


right click on project go to properties->java built in path->libraries->Add library and add your jar file there and then it's working

if you want to use youtube video in your application your device must have to contain an application of youtube otherwise it will not work




回答6:


In addition to adding <uses-permission android:name="android.permission.INTERNET" /> to the AndroidManifest.xml, you have to have the youtube app on the device/emulator. If you want to get it on the emulator, sideload this:

https://play.google.com/store/apps/details?id=com.google.android.youtube&hl=en
(You can get the apk with http://apps.evozi.com/apk-downloader/?id=com.google.android.youtube)




回答7:


If the Player Fails on Google Glass

When the youtube player fails, the YouTubeInitializationResult arg1 paramter passed to the failure method contains information about why the player failed. I simply toasted it, as you can see in the above code. I got a SERVIVE_MISSING print-out in the toast. I found the list of possible return values on google's developer website, and the cause of the SERVICE_MISSING error is that the YouTube API service is missing on the device. This service is part of the standard youtube app that google distributes with android, but apparently has not added it to glass yet. To fix the problem, you can side-load the app to get the services. There is no way to launch the YouTube app and watch videos from it, unfortunately, since it was written for Android telephones and has no voice trigger/timeline card.

It can be downloaded from here, or I have it loaded here: com.google.android.youtube-5.2.27.apk

Simply sideload this with:

adb install com.google.android.youtube-5.2.27.apk

This installs the youtube app on the glass. Now the video should work!

taken from: http://www.eg.bucknell.edu/~jpk017/doku.php?id=wiki:yt




回答8:


I had this issue on my release build but worked after adding this to proguard

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
       public *;
   }



回答9:


I had the same problem, after searching and googling so much , i got the answer that:-

           for youtube player you must have youtube app enabled and still if not work then update youtube to the latest version.

it would work then.



来源:https://stackoverflow.com/questions/15458351/youtube-api-an-error-occurred-while-initializing-the-youtube-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!