我正在尝试在我的应用中播放Youtube视频但在屏幕上显示错误“初始化YouTube播放器时出错”
我已经使用SHA-1密钥生成API密钥,我认为这已经成功,并且不确定是否会因此出现错误,请帮我解决。
活动代码 - :
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "my API KEY";
public static final String VIDEO_ID = "1M2gC86Q_9o";
// public static final String VIDEO_ID = "https://www.youtube.com/watch?v=1M2gC86Q_9o";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
/** Initializing YouTube player view **/
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new 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() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setSupportActionBar(Toolbar supportActionBar) {
//this.supportActionBar = supportActionBar;
}
}
错误标题是 - :
com.google.android.youtube.player.internal.w$a: Unable to find dynamic class com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer
at com.google.android.youtube.player.internal.w.a(Unknown Source)....
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer" on path: DexPathList[[zip file "/system/app/YouTube_l1_64/YouTube_l1_64.apk"],nativeLibraryDirectories=[/system/app/YouTube_l1_64/lib/arm, /vendor/lib64, /system/lib64]]
Suppressed: java.io.IOException: Zip archive '/system/app/YouTube_l1_64/YouTube_l1_64.apk' doesn't contain classes.dex (error msg: Entry not found)
Buil.Gradle(模块:应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.administrator.youtube"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile files('libs/YouTubeAndroidPlayerApi.jar')
}
答案 0 :(得分:1)
你的gradle uri google-api-services-youtube
错了,替换你的gradle
依赖:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.apis:google-api-services-youtube:v3-rev157-1.21.0'
}
我找到了here。然后我换了最新版本v3-rev157-1.21.0
它会起作用: