How to catch all errors for ExoPlayer?

Michalsx picture Michalsx · Mar 22, 2017 · Viewed 13.1k times · Source

I implemented ExoPlayer as player for my application. But I can´t find out how to catch all ExoPlayer errors to avoid app crash. I added following listener, but it doesn´t catch all errors.

I have to use DRM and it sometimes crash on some problem with it, but I can´t set listener before, because player is NULL.

    player = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl, drmSessionManager);

    player.addListener(new ExoPlayer.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {

        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
            //Catch here, but app still crash on some errors!
        }

        @Override
        public void onPositionDiscontinuity() {

        }
    });

Error example:

03-22 16:38:31.401 17960-25624/com.mypackage.name E/ExoPlayerImplInternal: Renderer error.  
                                                                     com.google.android.exoplayer2.ExoPlaybackException 
                                                                         at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.shouldWaitForKeys(MediaCodecRenderer.java:709)  
                                                                         at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.feedInputBuffer(MediaCodecRenderer.java:650)    
                                                                         at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:490) 
                                                                         at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:464)  
                                                                         at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:300)   
                                                                         at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                         at android.os.HandlerThread.run(HandlerThread.java:61) 
                                                                         at com.google.android.exoplayer2.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40) 
                                                                      Caused by: com.google.android.exoplayer2.drm.DrmSession$DrmSessionException: com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to https://widevine-dash.ezdrm.com/proxy?pX=blablablabla    
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager.onError(DefaultDrmSessionManager.java:594)   
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager.onKeysError(DefaultDrmSessionManager.java:589)   
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager.onKeyResponse(DefaultDrmSessionManager.java:549) 
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager.access$900(DefaultDrmSessionManager.java:49) 
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager$PostResponseHandler.handleMessage(DefaultDrmSessionManager.java:669) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)    
                                                                         at android.os.Looper.loop(Looper.java:154)     
                                                                         at android.os.HandlerThread.run(HandlerThread.java:61)     
                                                                         at com.google.android.exoplayer2.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40)     
                                                                      Caused by: com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to https://widevine-dash.ezdrm.com/proxy?pX=blablablabla  
                                                                         at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:204)   
                                                                         at com.google.android.exoplayer2.upstream.DataSourceInputStream.checkOpened(DataSourceInputStream.java:101)    
                                                                         at com.google.android.exoplayer2.upstream.DataSourceInputStream.read(DataSourceInputStream.java:81)    
                                                                         at com.google.android.exoplayer2.upstream.DataSourceInputStream.read(DataSourceInputStream.java:75)    
                                                                         at com.google.android.exoplayer2.util.Util.toByteArray(Util.java:118)  
                                                                         at com.google.android.exoplayer2.drm.HttpMediaDrmCallback.executePost(HttpMediaDrmCallback.java:106)   
                                                                         at com.google.android.exoplayer2.drm.HttpMediaDrmCallback.executeKeyRequest(HttpMediaDrmCallback.java:91)  
                                                                         at com.google.android.exoplayer2.drm.DefaultDrmSessionManager$PostRequestHandler.handleMessage(DefaultDrmSessionManager.java:692)  
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)    
                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                         at android.os.HandlerThread.run(HandlerThread.java:61) 
                                                                      Caused by: java.net.SocketTimeoutException: timeout   
                                                                         at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)   
                                                                         at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:250)    
                                                                         at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:217)  
                                                                         at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306) 
                                                                         at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300) 
                                                                         at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)  
                                                                         at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)   
                                                                         at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)   
                                                                         at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)    
                                                                         at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782)   
                                                                         at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)   
03-22 16:38:31.401 17960-25624/com.mypackage.name E/ExoPlayerImplInternal:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405) 
                                                                         at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)   
                                                                         at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 
                                                                         at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java) 
                                                                         at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:201)   
                                                                            ... 10 more
03-22 16:38:31.402 17960-17960/com.mypackage.name E/PlayerActivity: onPlayerError: com.google.android.exoplayer2.drm.DrmSession$DrmSessionException: com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to https://widevine-dash.ezdrm.com/proxy?pX=blablablabla  
03-22 16:38:31.404 17960-17960/com.mypackage.name D/AndroidRuntime: Shutting down VM    
03-22 16:38:31.407 17960-17960/com.mypackage.name E/UncaughtException: java.lang.IllegalStateException  
                                                                     at com.google.android.exoplayer2.util.Assertions.checkState(Assertions.java:79)    
                                                                     at com.google.android.exoplayer2.ExoPlaybackException.getSourceException(ExoPlaybackException.java:111)    
                                                                     at com.mypackage.name.ui.activities.PlayerActivity$1.onPlayerError(PlayerActivity.java:260)    
                                                                     at com.google.android.exoplayer2.ExoPlayerImpl.handleEvent(ExoPlayerImpl.java:382) 
                                                                     at com.google.android.exoplayer2.ExoPlayerImpl$1.handleMessage(ExoPlayerImpl.java:93)  
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)    
                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6121)   
                                                                     at java.lang.reflect.Method.invoke(Native Method)  
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)    
03-22 16:38:31.417 17960-25623/com.mypackage.name D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=PlayerActivity_, _si=-5050973009208192093, timestamp=1490197111407, fatal=1}]    
03-22 16:38:31.437 17960-25684/com.mypackage.name D/SurfaceUtils: set up nativeWindow 0x791072a810 for 1x1, color 0x2, rotation 0, usage 0x930  
03-22 16:38:31.454 17960-25623/com.mypackage.name V/FA: Using measurement service   
03-22 16:38:31.455 17960-25623/com.mypackage.name V/FA: Connecting to remote service    
03-22 16:38:31.707 17960-17960/com.mypackage.name E/AndroidRuntime: FATAL EXCEPTION: main   
                                                              Process: com.mypackage.name, PID: 17960   
                                                              java.lang.IllegalStateException   
                                                                  at com.google.android.exoplayer2.util.Assertions.checkState(Assertions.java:79)   
                                                                  at com.google.android.exoplayer2.ExoPlaybackException.getSourceException(ExoPlaybackException.java:111)   
                                                                  at com.mypackage.name.ui.activities.PlayerActivity$1.onPlayerError(PlayerActivity.java:260)   
                                                                  at com.google.android.exoplayer2.ExoPlayerImpl.handleEvent(ExoPlayerImpl.java:382)    
                                                                  at com.google.android.exoplayer2.ExoPlayerImpl$1.handleMessage(ExoPlayerImpl.java:93) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)   
                                                                  at android.os.Looper.loop(Looper.java:154)    
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6121)  
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)    
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)   

Answer

Michalsx picture Michalsx · Mar 24, 2017

Thanks to feedback from ExoPlayer support, this should cover all problems.

    @Override
    public void onPlayerError(ExoPlaybackException error) {
        switch (error.type) {
            case ExoPlaybackException.TYPE_SOURCE:
                Log.e(TAG, "TYPE_SOURCE: " + error.getSourceException().getMessage());
                break;

            case ExoPlaybackException.TYPE_RENDERER:
                Log.e(TAG, "TYPE_RENDERER: " + error.getRendererException().getMessage());
                break;

            case ExoPlaybackException.TYPE_UNEXPECTED:
                Log.e(TAG, "TYPE_UNEXPECTED: " + error.getUnexpectedException().getMessage());
                break;
        }
    }