Last active
May 29, 2023 15:48
-
-
Save takeshiyako2/e776bbaf2966c6501c4f to your computer and use it in GitHub Desktop.
Android Sample YouTube API on the Fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:id="@+id/main" | |
tools:context=".MainActivity"> | |
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="xxxx.xxxx.xxxx.fragmentdeyoutube" > | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 22 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "xxxx.xxxx.xxxx.fragmentdeyoutube" | |
minSdkVersion 15 | |
targetSdkVersion 22 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:22.2.0' | |
compile files('libs/YouTubeAndroidPlayerApi.jar') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxxx.xxxx.xxxx.fragmentdeyoutube; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// フラグメント起動 (v4の作法で) | |
YoutubeFragment fragment = new YoutubeFragment(); | |
FragmentManager manager = getSupportFragmentManager(); | |
manager.beginTransaction() | |
.replace(R.id.main, fragment) | |
.addToBackStack(null) | |
.commit(); | |
} | |
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<FrameLayout | |
android:id="@+id/youtube_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:visibility="visible" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxxx.xxxx.xxxx.fragmentdeyoutube; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentTransaction; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Toast; | |
import com.google.android.youtube.player.YouTubeInitializationResult; | |
import com.google.android.youtube.player.YouTubePlayer; | |
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener; | |
import com.google.android.youtube.player.YouTubePlayer.Provider; | |
import com.google.android.youtube.player.YouTubePlayerSupportFragment; | |
public class YoutubeFragment extends Fragment { | |
// API キー | |
private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
// YouTubeのビデオID | |
private static String VIDEO_ID = "EGy39OMyHzw"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.you_tube_api, container, false); | |
// YouTubeフラグメントインスタンスを取得 | |
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance(); | |
// レイアウトにYouTubeフラグメントを追加 | |
FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); | |
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit(); | |
// YouTubeフラグメントのプレーヤーを初期化する | |
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() { | |
// YouTubeプレーヤーの初期化成功 | |
@Override | |
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) { | |
if (!wasRestored) { | |
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); | |
player.loadVideo(VIDEO_ID); | |
player.play(); | |
} | |
} | |
// YouTubeプレーヤーの初期化失敗 | |
@Override | |
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) { | |
// YouTube error | |
String errorMessage = error.toString(); | |
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show(); | |
Log.d("errorMessage:", errorMessage); | |
} | |
}); | |
return rootView; | |
} | |
} |
YoutubeFragment.java is useful thx
This is very helpful.Thank you.
Thank you! This was incredibly helpful, and the only information I could find on using the YouTube API in a fragment.
thank you :)
thank you so much, I have no words but thank you !
`YouTubePlayerSupportFragment youTubePlayFragment=YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayFragment).commit(); \\ My Error here
youTubePlayFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b)
{
System.out.println("In The success");
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.cueVideo(VIDEO_ID);
YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()){
System.out.println("In The Failure");
errorReason.getErrorDialog(getActivity(),1).show();
}
else {
String errorMessage=String.format("Ther was wrror initioslksd the ",errorReason.toString());
System.out.println(errorMessage);
}
}
});
`
I have an error that when I wrote ' transaction.add(R.id.youtube_fragment, youTubePlayFragment).commit();' Red underline apear in this ststment
and said
yo te amo culero, gracias por el aporte
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i wrote this just to tell you, thank you so much, you are the best.
the only code that helps me out,
the rest of them was using activity and they call it fragment, the only one use a real fragment was you,
you rock, keep going