0

我是一个新手Android“程序员”,我试图找到这个错误的答案,但没有运气。有一些一半的答案,但没有什么可以建议在我独特的情况下更准确地做什么。

关闭 YoutubePlayer 活动时会出现此错误。看来我应该注销一个接收器,可能在 onPause() 中......但我该怎么做,也许更重要的是,应该注销哪个接收器?

这是我的日志:

06-06 02:17:31.781 26887-26887/? E/ActivityThread: Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver adez@e1c9513 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver adez@e1c9513 that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:923)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:724)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1182)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1162)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1156)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:564)
        at adex.<init>(SourceFile:18)
        at adfe.get(SourceFile:15)
        at aerr.get(SourceFile:46)
        at aoqy.get(SourceFile:11)
        at lad.h(SourceFile:148)
        at addo.get(SourceFile:9)
        at aoqy.get(SourceFile:11)
        at laf.a(SourceFile:7)
        at kyp.<init>(SourceFile:10)
        at com.google.android.apps.youtube.embeddedplayer.service.service.jar.ApiPlayerService.<init>(SourceFile:48)
        at com.google.android.apps.youtube.embeddedplayer.service.service.jar.ApiPlayerFactoryService$1.run(SourceFile:4)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

添加一些代码。试图保持简短。活动:

public class VideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, EasyPermissions.PermissionCallbacks {

这就是我在 onCreate() 中的内容:

youTubeView.initialize("@string/ytkey", this);

...这就是我对 YoutubePlayer 的看法:

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.loadVideo(userVideo.getYtId());
         //player.setFullscreen(true);
    }
}

@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();
    }
}

protected Provider getYouTubePlayerProvider() {
    return youTubeView;
}
4

2 回答 2

0

试试这个代码让它工作:

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

private static final int REQUEST_DIALOG = 101;

// YouTube player view
private YouTubePlayerView youTubeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

    // Initializing video player with developer key
    youTubeView.initialize(Config.DEVELOPER_API_KEY, this);

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
        YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, REQUEST_DIALOG).show();
    } else {
        String errorMessage = String.format(
                getString("Print an error"), errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
        YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {

        // loadVideo() will auto play video
        // Use cueVideo() method, if you don't want to play it automatically
        player.loadVideo(Config.YOUTUBE_VIDEO_CODE);

        // Hiding player controls
        player.setPlayerStyle(PlayerStyle.CHROMELESS);
    }
}

@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(Config.DEVELOPER_API_KEY, this);
    }
}

private YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

}

AndroidManifest.xml

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
于 2018-06-06T06:21:05.380 回答
0

YouTube Player API 通常很难使用,您可能需要考虑基于 WebView 的解决方案。

出于这个原因,我构建了一个开源替代方案。它基于 WebView,不会出现任何此类问题。安卓-YouTube-播放器

这是一个简单的视图,你可以把它放在你想要的任何地方。没有要扩展的活动/片段,也没有泄漏服务。

于 2018-06-06T06:41:05.740 回答