我尝试在我的 Android 应用程序中使用 Youtube API,我的应用程序看起来像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
ytpv.initialize("this is my key", this);
et = (EditText) findViewById(R.id.eturl);
et.setOnEditorActionListener(this);
}
@Override
public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
if(arg1 == YouTubeInitializationResult.DEVELOPER_KEY_INVALID)
Toast.makeText(this, "Initialization Fail- key invalid", Toast.LENGTH_LONG).show();
else if(arg1 == YouTubeInitializationResult.SERVICE_INVALID)
Toast.makeText(this, "Initialization Fail- Service invalid", Toast.LENGTH_LONG).show();
else if(arg1 == YouTubeInitializationResult.INVALID_APPLICATION_SIGNATURE)
Toast.makeText(this, "Initialization Fail- invalid application", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasrestored) {
ytp = player;
Toast.makeText(this, "Initialization Success", Toast.LENGTH_LONG).show();
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_GO ){
if(ytp !=null){
ytp.loadVideo(et.getText().toString());
}
}
return false;
}
但是当我在我的真实设备上安装时,我收到一个错误:初始化失败-服务无效我不知道要修复这个错误。让我听听你的想法,谢谢:)