Youtube API在N版本以下可以完美运行,但是在Oreo 8.1上会出现此错误
10-30 10:36:17.033 12166-12166/com.oceanleaguewebcranks.app.oceanleague E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.oceanleaguewebcranks.app.oceanleague, PID: 12166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oceanleaguewebcranks.app.oceanleague/com.oceanleaguewebcranks.app.oceanleague.Service_Info_Activity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.youtube.api.service.START }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.youtube.api.service.START }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1464)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1609)
at android.app.ContextImpl.bindService(ContextImpl.java:1557)
at android.content.ContextWrapper.bindService(ContextWrapper.java:684)
at com.google.android.youtube.player.internal.r.e(Unknown Source:69)
at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source:67)
at com.google.android.youtube.player.YouTubeBaseActivity$a.a(Unknown Source:12)
at com.google.android.youtube.player.YouTubePlayerView.initialize(Unknown Source:7)
at com.oceanleaguewebcranks.app.oceanleague.Service_Info_Activity.initView(Service_Info_Activity.java:92)
at com.oceanleaguewebcranks.app.oceanleague.Service_Info_Activity.onCreate(Service_Info_Activity.java:57)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Activity.performCreate(Activity.java:7014)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我正在使用lettest Youtube API,但仍然会出现此错误,这是我用于初始化youtube播放器的代码。
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
mYoutubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
mYoutubeView.initialize("AIzaSyCJTK0EL5EM7esPnxf8iFzW-__cvq5asf8", this);
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(Youtube_Link); // Plays 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_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(WebConstants.DEVELOPER_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return mYoutubeView;
}
只有oreo 8.1的问题,我正在使用youtube应用程序上的最新版本,如何解决此错误。
[1]: https://developers.google.com/youtube/android/player/downloads/
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oceanleaguewebcranks.app.oceanleague"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission
android:name="android.permission.CALL_PHONE"
android:required="false" />
<uses-permission
android:name="android.permission.WAKE_LOCK"
android:required="false" />
<uses-permission
android:name="android.permission.READ_CONTACTS"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission
android:name="com.name.name.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.name.name.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Service_Info_Activity"/>
</application>
</manifest>
Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.oceanleaguewebcranks.app.oceanleague"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
useLibrary 'org.apache.http.legacy'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/mipmap/logo'] } }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.volley:volley:1.0.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.github.bumptech.glide:glide:3.6.1'
implementation 'com.squareup.picasso:picasso:2.5.0'
implementation 'com.github.chrisbanes:PhotoView:2.1.3'
implementation 'org.apache.commons:commons-math3:3.6.1'
implementation 'org.apache.commons:commons-lang3:3.6'
// implementation files('libs/YouTubeAndroidPlayerApi.jar')
implementation files('libs/YouTubeAndroidPlayerApi-1.2.2.zip')
}
答案 0 :(得分:0)
使用新的YouTubeAndroidPlayerApi.jar
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubePlayerView.initialize(WebConstants.DEVELOPER_KEY, this);
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failed to initialize.", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if(null== player) return;
// Start buffering
if (!wasRestored) {
player.loadVideo(Youtube_Link);
}
// Add listeners to YouTubePlayer instance
player.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
@Override public void onAdStarted() { }
@Override public void onError(YouTubePlayer.ErrorReason arg0) { }
@Override public void onLoaded(String arg0) { }
@Override public void onLoading() { }
@Override public void onVideoEnded() { }
@Override public void onVideoStarted() { }
});
player.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
@Override public void onBuffering(boolean arg0) { }
@Override public void onPaused() { }
@Override public void onPlaying() { }
@Override public void onSeekTo(int arg0) { }
@Override public void onStopped() { }
});
}