Приложение Android не будет воспроизводить видео YouTube при нажатии кнопки. Не запускается плеер YouTube? - PullRequest
0 голосов
/ 23 октября 2019

У меня есть похожее приложение с очень похожим кодом, который воспроизводит видео YouTube, но этого не будет. Я пытался получить другой ключ API от Google, но это не имело значения. Приложение просто падает, когда я нажимаю кнопку, которая запускает проигрыватель YouTube.

Это класс Java:

package xxxxxxxxxxxxxxxxxxx;

import android.os.Bundle;
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.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class Level_1_Training extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener{

    public static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    public static final String VIDEO_ID = "Bm7WMvpjDec";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationFailure(Provider provider,
                                        YouTubeInitializationResult result) {
        Toast.makeText(getApplicationContext(),
                "onInitializationFailure()",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                        boolean wasRestored) {
        player.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);// it's a simple style that doesn't show the YouTube logo and it prevents users to find the real source of the YouTube video
        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);
        }
    }

}

И манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="creativemind.babyread2">

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

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


    <application

        android:allowBackup="true"
        android:icon="@drawable/babyreadappicon"
        android:label="@string/app_name">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:screenOrientation="portrait"
            android:name="creativemind.babyread2.Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="creativemind.babyread2.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity

            android:screenOrientation="landscape"
            android:name="creativemind.babyread2.Level_1_Training"
            android:label="Level 1"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

            <intent-filter>
                <action android:name="creativemind.babyread2.LEVEL1TRAINING" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity

            android:screenOrientation="landscape"
            android:name="creativemind.babyread2.Level_1_Testing"
            android:label="Level 1 Training"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            >
            <intent-filter>
                <action android:name="creativemind.babyread2.LEVEL1TESTING" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity

            android:screenOrientation="portrait"
            android:name="creativemind.babyread2.Introduction"
            android:label="Level 1"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="creativemind.babyread2.INTRODUCTION" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>



    </application>


</manifest>

Эточто я получаю в журнале приложений

10/22 22:06:04: Launching 'app' on samsung SM-J727V.
$ adb shell am start -n "creativemind.babyread2/creativemind.babyread2.Splash" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 18665 on device 'samsung-sm_j727v-52b1fcea'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
E/Zygote: isWhitelistProcess - Process is Whitelisted
E/libpersona: scanKnoxPersonas
    Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-J727V_8.1.0_0008, [-1 -1 -1 -1 0 1]
I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=creativemind.babyread2 
I/zygote64: Late-enabling -Xcheck:jni
D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
D/ActivityThread: Added TimaKeyStore provider
I/MediaPlayer: Need to enable context aware info
V/MediaPlayer-JNI: native_setup
V/MediaPlayerNative: constructor
    setListener
V/MediaPlayer-JNI: setVolume: left 1.000000  right 1.000000
V/MediaPlayerNative: MediaPlayer::setVolume(1.000000, 1.000000)
V/MediaPlayer-JNI: setAuxEffectSendLevel: level 0.000000
V/MediaPlayerNative: MediaPlayer::setAuxEffectSendLevel(0.000000)
V/MediaPlayer-JNI: setParameter: key 1400
V/MediaPlayerNative: MediaPlayer::setParameter(1400)
V/MediaPlayer-JNI: setAudioStreamType: 3
V/MediaPlayerNative: MediaPlayer::setAudioStreamType
V/MediaPlayer-JNI: set_session_id(): 16993
V/MediaPlayerNative: MediaPlayer::setAudioSessionId(16993)
V/MediaPlayer-JNI: setDataSourceFD: fd 53
V/MediaPlayerNative: setDataSource(53, 2060780, 435840)
V/MediaPlayerNative: setVideoSurfaceTexture
V/MediaPlayerNative: prepare
V/MediaPlayerNative: message received msg=200, ext1=10973, ext2=0
W/MediaPlayerNative: info/warning (10973, 0)
V/MediaPlayerNative: callback application
V/MediaPlayerNative: back from callback
V/MediaPlayerNative: message received msg=1, ext1=0, ext2=0
    MediaPlayer::notify() prepared
    signal application thread
    callback application
V/MediaPlayerNative: prepare complete - status=0
V/MediaPlayerNative: back from callback
V/MediaPlayerNative: invoke 68
W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
    See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
V/MediaPlayer-JNI: setVolume: left 1.000000  right 1.000000
V/MediaPlayerNative: MediaPlayer::setVolume(1.000000, 1.000000)
V/MediaPlayer-JNI: setAuxEffectSendLevel: level 0.000000
V/MediaPlayerNative: MediaPlayer::setAuxEffectSendLevel(0.000000)
V/MediaPlayer-JNI: setAudioStreamType: 3
V/MediaPlayerNative: MediaPlayer::setAudioStreamType
V/MediaPlayer-JNI: setLooping: 1
V/MediaPlayerNative: MediaPlayer::setLooping
V/MediaPlayer-JNI: start
V/MediaPlayerNative: start
D/OpenGLRenderer: HWUI GL Pipeline
D/InputTransport: Input channel constructed: fd=60
D/ViewRootImpl@a60997f[Splash]: setView = DecorView@1203d4c[Splash] TM=true MM=false
V/MediaPlayerNative: invoke 68
D/ViewRootImpl@a60997f[Splash]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@a60997f[Splash]: Relayout returned: old=[0,0][0,0] new=[0,0][720,1280] result=0x7 surface={valid=true 514885689344} changed=true
I/Adreno: QUALCOMM build                   : 160a517, I47625b5b56
    Build Date                       : 06/13/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01_06_07
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
D/ViewRootImpl@a60997f[Splash]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 720, 1280) ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
    MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@def4e9b nm : creativemind.babyread2 ic=null
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=65
I/zygote64: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
D/libGLESv1: STS_GLApi : DTS, ODTC are not allowed for Package : creativemind.babyread2
D/OpenGLRenderer: eglCreateWindowSurface = 0x77e385ca40, 0x77e1942010
D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
V/MediaPlayerNative: message received msg=6, ext1=0, ext2=0
    unrecognized message: (6, 0, 0)
    callback application
    back from callback
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@fb9e238 nm : creativemind.babyread2 ic=null
D/InputMethodManager: startInputInner - Id : 0
V/MediaPlayer-JNI: getCurrentPosition: 1171 (msec)
V/MediaPlayer: resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
    cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer-JNI: release
V/MediaPlayerNative: setListener
    disconnect
V/MediaPlayerNative: destructor
V/MediaPlayerNative: disconnect
D/ViewRootImpl@a60997f[Splash]: MSG_WINDOW_FOCUS_CHANGED 0
D/OpenGLRenderer: eglDestroySurface = 0x77e385ca40, 0x77e1942000
D/ViewRootImpl@a60997f[Splash]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x5 surface={valid=false 0} changed=true
D/InputTransport: Input channel destroyed: fd=65
D/ViewRootImpl@a60997f[Splash]: dispatchDetachedFromWindow
D/InputEventReceiver: channel 'c94978d creativemind.babyread2/creativemind.babyread2.Splash (client)' ~ Disposing input event receiver.
    channel 'c94978d creativemind.babyread2/creativemind.babyread2.Splash (client)' ~NativeInputEventReceiver.
D/InputTransport: Input channel destroyed: fd=60
V/MediaPlayer-JNI: native_finalize
    release
Process 18665 terminated.

1 Ответ

0 голосов
/ 23 октября 2019

Попробуйте!

public class Level_1_Training extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

        public static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
        public static final String VIDEO_ID = "Bm7WMvpjDec";
        private static final int RECOVERY_DIALOG_REQUEST = 1;
        private YouTubePlayerView youTubeView;

        @Override
        protected void attachBaseContext(Context newBase) {
            super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_video);

            youTubeView = findViewById(R.id.youtubeplayerview);

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

        }

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            if (!b) {
                youTubePlayer.loadVideo(VIDEO_ID);
            }
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
        }

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

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

    }
...