问题
I have an android app, which allows to play youtube video. I am using the latest YouTube API (1.2.1). I try it on a several devices within android version 4.0.4, 4.3, 4.4.4 or 5.0 and it works perfect (on every device is YouTube app version 10.-. But on one device, where is android 4.0.4 and YouTube app version 4.4.11 it does not work and get an error "An error occurred while initializing YouTube player". In documentation is written, that the minimal required version of YouTube app is 4.2.16. So I don't know, where is the problem.
Does somebody have an idea, what is wrong or how can I fix it?
Thanks a lot...
回答1:
Update your Android youtube app to the latest version and it will work for Sure!!
回答2:
I would never have thought to look in that location for the settings. It worked just as you said.
Here are the instructions for that less knowledge of the settings.
First, you need to go Settings -> battery -> app launch -> YouTube.
Now make disable the option manage automatically.
After disabling the above option, a popup will be shown.
Now make enable the option Secondary Launch (can be launched by other apps)
回答3:
Please try this. First you should try to download the Youtube player library for Android from the link below:
Youtube Android Player
You should first install it like this: Project -> menu: File > Structure > Dependencies Tab > Add -> library dependency
if it doesn't work, please try one of these two:
Add dependency of the library inside dependency inside build.gradle file of the library u r using, and paste ur library in External Libraries.
OR
Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there Now the trick here is that now go inside settings.gradle file now add this line include ':app:libs' after include ':app' It will definitely work.
Then, you should have a layout like this:
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And you can have a player activity like this:
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
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.YouTubePlayerView;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import java.io.IOException;
public class YoutubeActivity extends YouTubeBaseActivity{
private YouTubePlayerView playerView;
private YouTube youtube;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_youtube);
youtube = new YouTube.Builder(new NetHttpTransport(),
new JacksonFactory(), new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest hr) throws IOException {}
}).setApplicationName(this.getString(R.string.app_name)).build();
playerView = (YouTubePlayerView)findViewById(R.id.player_view);
playerView.initialize("Your API Key", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if(!b){
String videoId = getIntent().getExtras().getString("videoID");
youTubePlayer.cueVideo(videoId);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(getApplicationContext(), getString(R.string.failed), Toast.LENGTH_LONG).show();
}
});
}
}
来源:https://stackoverflow.com/questions/29133874/android-youtube-api-an-error-occurred-while-initializing-youtube-player