不幸的是,youtube播放器已停止工作

时间:2015-05-02 10:26:21

标签: android android-youtube-api

我已将api jar文件放在libs文件夹中。当我运行此程序时,我得到很遗憾,youtube播放器已停止工作。

任何人都可以帮助我吗?

我还想知道一件事: 我在java文件中使用了以下语句。它在DEVELOPER_KEY上显示redmark(实际上这是一个存储api密钥的变量),  youTubeView.initialize(Config.DEVELOPER_KEY,this); 这就是我使用以下声明的原因: youTubeView.initialize(Developer_Key开发,这一点);

因为这个有什么问题

这是我的activity.java

    package com.example.XXX.XXX;

    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Toast;

    import com.google.android.youtube.player.YouTubeBaseActivity;
    import com.google.android.youtube.player.YouTubeInitializationResult;
    import com.google.android.youtube.player.YouTubePlayer;
    import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
    import com.google.android.youtube.player.YouTubePlayerView;

    public class YoutubeActivity extends YouTubeBaseActivity implements
            YouTubePlayer.OnInitializedListener {
        public static final String DEVELOPER_KEY = "<Censored>";
        public static final String YOUTUBE_VIDEO_CODE = "uOpVvokKnu0";

        private static final int RECOVERY_DIALOG_REQUEST = 1;

        // 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(DEVELOPER_KEY,this);

        }

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

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

    }

`

这是我的XML代码

`

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            android:src="@drawable/snake_bg" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/rouned_corner_shadow"
                android:gravity="center_horizontal"
                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" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="70dp"
                    android:layout_marginBottom="20dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/nat_geo_logo" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:text="@string/title_logo"
                    android:textColor="@color/title"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginBottom="30dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/wild" />
            </LinearLayout>

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:background="@drawable/rouned_corner_shadow"
                android:text="@string/btn_skip_intro" />
        </LinearLayout>

    </RelativeLayout>

`

Log cat输出:

at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                at android.os.Looper.loop(Looper.java:136)
                                at android.app.ActivityThread.main(ActivityThread.java:5017)
                                at java.lang.reflect.Method.invokeNative(Native Method)
                                at java.lang.reflect.Method.invoke(Method.java:515)
                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                at dalvik.system.NativeStart.main(Native Method)
                         Caused by: java.lang.NullPointerException
                                at com.example.health.weshallovercome.YoutubeActivity.onCreate(YoutubeActivity.java:38)
                                at android.app.Activity.performCreate(Activity.java:5231)
                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                at android.os.Looper.loop(Looper.java:136)
                                at android.app.ActivityThread.main(ActivityThread.java:5017)
                                at java.lang.reflect.Method.invokeNative(Native Method)
                                at java.lang.reflect.Method.invoke(Method.java:515)
                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                at dalvik.system.NativeStart.main(Native Method)

由于以下代码,我在设计视图中遇到了一些错误。我会附上那个。

<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" />

That is error I'm getting on XML file

this is the continuation for this error

请找到logcat:

05-04 06:24:29.670      935-935/XXX D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
                    05-04 06:24:31.580      935-935/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 48K, 5% free 2808K/2940K, paused 248ms, total 333ms
                    05-04 06:24:31.580      935-935/XXX I/dalvikvm-heap﹕ Grow heap (frag case) to 9.002MB for 6480016-byte allocation
                    05-04 06:24:32.030      935-944/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 2% free 9136K/9272K, paused 453ms, total 453ms
                    05-04 06:24:33.410      935-935/XXX D/﹕ HostConnection::get() New Host Connection established 0xb7550c78, tid 935
                    05-04 06:24:33.550      935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
                    05-04 06:24:33.570      935-935/XXX D/OpenGLRenderer﹕ Enabling debug mode 0
                    05-04 06:28:24.979      935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
                    05-04 06:28:27.559      935-935/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 29K, 2% free 11093K/11216K, paused 41ms, total 42ms
                    05-04 06:28:27.769      935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
                    05-04 06:28:30.839      935-935/XXX D/AndroidRuntime﹕ Shutting down VM
                    05-04 06:28:30.839      935-935/XXX W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
                    05-04 06:28:30.869      935-935/XXX E/AndroidRuntime﹕ FATAL EXCEPTION: main
                        Process: XXX, PID: 935
                        java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX/XXX.YoutubeActivity}: java.lang.NullPointerException
                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                at android.os.Looper.loop(Looper.java:136)
                                at android.app.ActivityThread.main(ActivityThread.java:5017)
                                at java.lang.reflect.Method.invokeNative(Native Method)
                                at java.lang.reflect.Method.invoke(Method.java:515)
                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                at dalvik.system.NativeStart.main(Native Method)
                         Caused by: java.lang.NullPointerException
                                at XXX.YoutubeActivity.onCreate(YoutubeActivity.java:38)
                                at android.app.Activity.performCreate(Activity.java:5231)
                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                at android.os.Looper.loop(Looper.java:136)
                                at android.app.ActivityThread.main(ActivityThread.java:5017)
                                at java.lang.reflect.Method.invokeNative(Native Method)
                                at java.lang.reflect.Method.invoke(Method.java:515)
                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                at dalvik.system.NativeStart.main(Native Method)
                    05-04 06:28:35.489      935-935/XXX I/Process﹕ Sending signal. PID: 935 SIG: 9
                    05-04 06:28:36.699    1148-1148/XXX  D/dalvikvm﹕ GC_FOR_ALLOC freed 40K, 5% free 2809K/2936K, paused 39ms, total 40ms
                    05-04 06:28:36.939    1148-1161/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 16K, 3% free 3308K/3408K, paused 24ms, total 24ms
                    05-04 06:28:37.219    1148-1148/XXX D/﹕ HostConnection::get() New Host Connection established 0xb769ae50, tid 1148
                    05-04 06:28:37.289    1148-1148/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
                    05-04 06:28:37.309    1148-1148/XXX D/OpenGLRenderer﹕ Enabling debug mode 0

我已经按照以下链接在Android应用中播放youtube。 http://www.androidhive.info/2014/12/how-to-play-youtube-video-in-android-app/我在自己的应用中所做的更改包括: 我在前面添加了以下行Androidmanifest.xml:我在build.gradle中添加了以下行: 编译文件(&#39; libs / YouTubeAndroidPlayerApi.jar&#39;)

我在libs文件夹中添加了这个jar。 我已经在这个问题中保留了我的xml和java文件的代码。我完全遵循了这个教程。

由于xml:

中的代码如下,您是否遇到任何问题?
<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" />

我在java文件夹中创建了config.java:

config.java的代码为:

public class config {
                // Google Console APIs developer key
                // Replace this key with your's
                public static final String DEVELOPER_KEY = "";

                // YouTube video id
                public static final String YOUTUBE_VIDEO_CODE = "uOpVvokKnu0";  }

Screenshot of where I have added jar file

由于我没有20个声誉,我无法聊天。我还有一个帐户,它有20多个声誉。但我无法在那里找到你。

我们差不多了,请帮助我。

你能现在开始聊天吗?我有20多个声誉。我可以聊聊。

还请找到现在的logcat:

05-06 02:27:38.110 991-991/com.example.health.weshallovercome D/AndroidRuntime﹕ Shutting down VM 05-06 02:27:38.110 991-991/com.example.health.weshallovercome W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a3fba8) 05-06 02:27:38.140 991-991/com.example.health.weshallovercome E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.health.weshallovercome, PID: 991 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.health.weshallovercome/com.example.health.weshallovercome.YoutubeActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.health.weshallovercome.YoutubeActivity.onCreate(YoutubeActivity.java:19) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             at dalvik.system.NativeStart.main(Native Method) 05-06 02:27:46.480 991-991/com.example.health.weshallovercome I/Process﹕ Sending signal. PID: 991 SIG: 9 05-06 02:27:47.510 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 59K, 5% free 2809K/2940K, paused 52ms, total 53ms 05-06 02:27:47.510 1010-1010/com.example.health.weshallovercome I/dalvikvm-heap﹕ Grow heap (frag case) to 3.258MB for 472516-byte allocation 05-06 02:27:47.560 1010-1019/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 3271K/3404K, paused 39ms, total 39ms 05-06 02:27:47.960 1010-1010/com.example.health.weshallovercome D/﹕ HostConnection::get() New Host Connection established 0xb7c64710, tid 1010 05-06 02:27:48.050 1010-1010/com.example.health.weshallovercome W/EGL_emulation﹕ eglSurfaceAttrib not implemented 05-06 02:27:48.070 1010-1010/com.example.health.weshallovercome D/OpenGLRenderer﹕ Enabling debug mode 0 05-06 02:27:50.180 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 24K, 3% free 3597K/3692K, paused 30ms, total 31ms 05-06 02:27:50.280 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 3% free 3966K/4068K, paused 30ms, total 31ms 05-06 02:27:50.380 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 3% free 4338K/4444K, paused 31ms, total 31ms 05-06 02:27:50.760 1010-1010/com.example.health.weshallovercome W/EGL_emulation﹕ eglSurfaceAttrib not implemented 05-06 02:27:54.760 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 22K, 2% free 4837K/4928K, paused 34ms, total 34ms 05-06 02:27:54.970 1010-1010/com.example.health.weshallovercome I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted 05-06 02:27:54.970 1010-1010/com.example.health.weshallovercome W/dalvikvm﹕ VFY: unable to resolve virtual method 12609: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V

正如你所说,我没有在右键菜单中找到添加为库选项。而且我还没有通过工作室添加jar文件。我已经打开了Androidstudioprojects(存在于C中),我选择了我的项目,因为我打开了lib并复制了jar文件。它在我的项目中显示了libs下的jar文件。我不认为这是添加jar的错误方法,是吗? right click menu

1 个答案:

答案 0 :(得分:0)

我已经测试了你的代码,当问题是互联网许可时,我觉得自己很傻。 将其添加到AndroidManifest.xml:

<uses-permission  android:name="android.permission.INTERNET"/>

确保您拥有有效的API密钥,因为YoutubePlayer API文档说明了这一点。

  

如果未设置开发人员密钥,则示例将抛出java.lang.NullPointerException并在您尝试运行时关闭。

修改

要修复布局渲染问题,请使用工具:context as

<AnyRootLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".YouTubeActivit" <!-- Your activity name here -->
    >
<!-- Your child elements here -->
</AnyRootElement>

将jar文件添加到libs文件夹中并右键单击该jar文件,然后单击“添加为库”,在android studio中添加jar文件的更多信息here

我的Build.gradle(适用于app模块):

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.mypackage.youtubedemo"
    minSdkVersion 9
    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.1.1'
compile files('libs/YouTubeAndroidPlayerApi.jar')
}