私はYoutube APIが必要なAndroid Studioでアプリを開発しています。問題は、ユーザーが「Reproducir」(「再生」)をクリックするたびに、ビデオが再生されるようにすることです。 だから私はビデオをロードする方法を研究し、誰もが関数を使うように言います。 は、onInitializationSuccessを呼び出してそこでプレイする初期化子です。しかし、新しい動画を読み込み、URLを解析して新しい動画を再生したい場合はできません。ユーザーがReproducirをクリックするたびに初期化を呼び出そうとしましたが、その場合はigです。AndroidでYoutube Apiで新しい動画を再生する
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
String url = urlTxtView.getText().toString();
Log.d("1",url);
youTubePlayer.loadVideo("a4NT5iBFuZs");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:"+result.toString(), Toast.LENGTH_LONG).show();
}
XMLレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/action_add_video"
android:textStyle="normal|bold"
android:textSize="30sp"
android:gravity="center_horizontal"
android:layout_marginBottom="20dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
<EditText
android:id="@+id/tituloVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Titulo"
android:inputType="textPersonName" />
<EditText
android:id="@+id/urlYoutube"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL o buscar con @youtube"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonCancelar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancelar" />
<Button
android:id="@+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reproducir" />
<Button
android:id="@+id/buttonGuardar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guardar" />
</LinearLayout>
</LinearLayout>
ログイン: W/YouTubeAndroidPlayerAPI:YouTubeのビデオ再生によるプレーヤーの上に不正なオーバーレイに停止しました。 YouTubePlayerViewはandroid.view.View {a21cb6f V.ED .... ........ 0,0-768,50#102002f android:id/statusBarBackground}によって隠されています。下端はYouTubePlayerViewの上端から36ピクセル下にあります。 。
ここではアイデアのイメージがあります:
エラーまたはクラッシュログ? apiキーが適切に与えられ、apiコンソールから有効になっていますか? – user1140237
クラッシュログはありません。キーは正常です。一度初期化すると動作しますので、複数回初期化することはできません。だから、別の方法でプレイヤーを取得し、再び初期化せずにビデオをロードするような解決策があるかもしれません。 –
残念ですが、ログがあります:W/YouTubeAndroidPlayerAPI:プレーヤーの上に不正なオーバーレイが原因でYouTubeビデオ再生が停止しました。 YouTubePlayerViewはandroid.view.View {a21cb6f V.ED ....によって隠されています。0,0-768,50#102002fアンドロイド:id/statusBarBackground}。下端はYouTubePlayerViewの上端から36ピクセル下にあります。 。 –