【问题标题】:Unable to play youtube video in my android app无法在我的 Android 应用中播放 youtube 视频
【发布时间】:2016-02-21 18:01:51
【问题描述】:

大家好,
我正在尝试通过集成 youtube sdk 并从谷歌控制台生成 api 密钥在我的 android 应用程序中播放 youtube 视频,当我尝试在运行应用程序时运行我的应用程序时,我的控制台中出现此错误,任何人都可以帮我解决这个问题。

11-19 19:41:53.264 1173-1173/? E/ActivityThread: Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver lwz@427e4cf0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-19 19:41:53.264 1173-1173/? E/ActivityThread: android.app.IntentReceiverLeaked: Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver lwz@427e4cf0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:805)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:606)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1720)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1700)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1694)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:453)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at lwz.a(SourceFile:1238)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at lwv.a(SourceFile:671)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at aha.a(SourceFile:267)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at aha.b(SourceFile:287)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at aps.run(SourceFile:209)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.os.Handler.handleCallback(Handler.java:730)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.os.Looper.loop(Looper.java:176)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at android.app.ActivityThread.main(ActivityThread.java:5419)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at java.lang.reflect.Method.invoke(Method.java:525)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-19 19:41:53.264 1173-1173/? E/ActivityThread:     at dalvik.system.NativeStart.main(Native Method)


Thanks in advance 

【问题讨论】:

  • 您错过了 unregisterReceiver。所有这些我们都可以错误地建议您

标签: android youtube-api android-youtube-api android-video-player


【解决方案1】:

当它出现时,您应该取消注册您的接收器

@Override
protected void onStop()
{
    unregisterReceiver(yourReceiver);
    super.onStop();
}

【讨论】:

    【解决方案2】:

    虽然你的答案已经给出了。

    如果您的代码仍有问题,您可以使用此完整代码(java 和 xml)。

    YoutubeActivity.java

        public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    
        private static final int RECOVERY_REQUEST = 1;
        private YouTubePlayerView youTubeView;
        private YouTubePlayer youTubePlayer;
        public static final String YOUTUBE_API_KEY = "YOUR-API-KEY";
        private String youtubeid;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_youtube);
    
            youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player);
            youTubeView.initialize(YOUTUBE_API_KEY, this);
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                youtubeid = extras.getString("youtubeid");
            }
    
        }
    
        @Override
        public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    
            if (!wasRestored) {
                YouTubePlayer.PlayerStyle style = YouTubePlayer.PlayerStyle.DEFAULT;
    
    
                player.setPlayerStyle(style);
                player.setFullscreen(true);
                player.setShowFullscreenButton(false);
                //player.cueVideo("fhWaJi1Hsfo"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
                player.cueVideo(youtubeid);
                //player.loadVideo(youtubeid);
    
            }
        }
    
        @Override
        public void onInitializationFailure(Provider provider, YouTubeInitializationResult errorReason) {
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
            } else {
                String error = String.format(getString(R.string.player_error), errorReason.toString());
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RECOVERY_REQUEST) {
                // Retry initialization if user performed a recovery action
                getYouTubePlayerProvider().initialize(YOUTUBE_API_KEY, this);
            }
        }
    
        protected Provider getYouTubePlayerProvider() {
            return youTubeView;
        }
    
        @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);
        }
    
    }
    

    activity_youtube.xml //你的布局 xml 文件

        <LinearLayout 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:orientation="vertical"
        android:background="#000000">
    
        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_player"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    【讨论】:

    • 这里缺少注销方法,如何才能完整或正确?
    猜你喜欢
    • 2017-02-11
    • 2012-07-18
    • 2012-10-17
    • 2014-03-11
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多