반응형
YouTube Android Player API 사용하여 MainActivity 내 Fragment 삽입
- 메인액티비티(MainActivity.java)
public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); setContentView(R.layout.activity_main); youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubeView); youTubePlayerFragment.initialize(getString(R.string.youtube_api_key), this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION); youTubePlayer.setPlaylistEventListener(playlistEventListener); youTubePlayer.setPlaybackEventListener(playbackEventListener); youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener); if (!wasRestored) { youTubePlayer.loadVideo(currentVideoId); } } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) { if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); } else { String errorMessage = String.format(getString(R.string.error_player), errorReason.toString()); Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RECOVERY_DIALOG_REQUEST) { // Retry initialization if user performed a recovery action getYouTubePlayerProvider().initialize(getString(R.string.youtube_api_key), this); } } protected YouTubePlayer.Provider getYouTubePlayerProvider() { return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubeView); } }
- 레이아웃(activity_main.xml)
Player 에 전체화면 보기를 클릭하면 전체화면으로 변하다가 다시 돌아가기를 반복하면서 오류 발생
- 오류내용
MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@f52b39c that was originally bound here
- 해결방법
AndroidManifest.xml 파일 내 activity 에 설정 값 추가
android:configChanges="keyboardHidden|orientation|screenSize"
반응형
'Android' 카테고리의 다른 글
[Android] Notification 에 RemoteViews 사용 시 이미지 삽입 (0) | 2019.07.23 |
---|---|
[android] retrofit2 JSONP 데이터 파싱 (0) | 2019.07.09 |
[Android Fcm]FirebaseApp name [DEFAULT] already exists! (0) | 2019.05.27 |
안드로이드 전화번호부에 저장된 이름 가져오기 (0) | 2014.03.13 |
activity 없이 service 만 실행하기(앱 화면 없이 실행하기) (1) | 2014.03.13 |