google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.7k stars 6.02k forks source link

Not stable audio session, at first start. #6975

Closed master255 closed 4 years ago

master255 commented 4 years ago

The first start of the player gives the audio session ID, but once in 5-15 times it does not work. The ID comes through the onAudioSessionId(int id) event {}. Track in flac format. ID comes correctly, but music is not transmitted through it. At the same time mMediaPlayer.getAudioSessionId() returns the same, not working ID. The player plays music correctly but does not pass it to audiosessionId.

Device: Samsung Galaxy s8+ Android 8.0 Version ExoPlayer 2.11.2 I checked the same scenario on other player modules. Classic player and VLC player do not have such a bug.

kim-vde commented 4 years ago

Please provide complete information as requested in the issue template. The issue template can be found here. If you're unable to share bug reports or test content publicly, please send them to dev.exoplayer@gmail.com using a subject in the format "Issue #1234", where "#1234" should be replaced with your issue number. Please also update this issue to indicate you’ve done this.

master255 commented 4 years ago

@kim-vde [REQUIRED] Issue description The first start of the player gives the audio session ID, but once in 5-15 times it does not work. The ID comes through the onAudioSessionId(int id) event {}. Track in flac format. ID comes correctly, but music is not transmitted through it. At the same time mMediaPlayer.getAudioSessionId() returns the same, not working ID. The player plays music correctly but does not pass it to audiosessionId. Expected: audio session always works stable.

[REQUIRED] Reproduction steps To reproduce the problem, start the player 5-20 times and make sure that the audio session works correctly. To do this you need to use it with EQ or Visualizer.

[REQUIRED] Link to test content You must create an example yourself. The problem is reproduced with any Flac file.

[REQUIRED] A full bug report captured from the device Bug report in the attachment. It's taken right after the bug appeared.

[REQUIRED] Version of ExoPlayer being used 2.11.2

[REQUIRED] Device(s) and version(s) of Android being used Samsung Galaxy S8+, Android 8.0. bugreport-2020-02-14-14-52-26.zip

kim-vde commented 4 years ago

Could you please send a minimal piece of code that I could use to reproduce? Also, what is the type of mMediaPlayer?

kim-vde commented 4 years ago

I tried to reproduce the issue but I couldn't. Here is the sample code that I used:

 player.addAudioListener(
          new AudioListener() {
            @Override
            @TargetApi(29)
            public void onAudioSessionId(int audioSessionId) {
              if (equalizer != null) {
                equalizer.release();
              }
              equalizer = new Equalizer(/* priority= */ 0, audioSessionId);
              short minimumBandLevel = equalizer.getBandLevelRange()[0];
              for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
                equalizer.setBandLevel(i, minimumBandLevel);
              }
              equalizer.setEnabled(true);
            }
          });
master255 commented 4 years ago

@kim-vde , I just checked. This problem only happens with Visualizer. And if you turn on the equalizer with Visualizer, the problem goes away. Bug only happens when the EQ is off and Visualizer is on.

I use SimpleExoPlayer. I tried to change all the settings and the bug is reproduced anyway.

master255 commented 4 years ago

@kim-vde I just tried to add Visualizer to the Demo application and got the following errors:

020-02-14 22:03:52.028 6994-6994/com.google.android.exoplayer2.demo E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
2020-02-14 22:03:52.028 6994-6994/com.google.android.exoplayer2.demo E/visualizers-JNI: Visualizer initCheck failed -3
2020-02-14 22:03:52.028 6994-6994/com.google.android.exoplayer2.demo E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at android.media.audiofx.Visualizer.<init>(Unknown Source:112)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at visualizer.VisualizerView.link(VisualizerView.java:85)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.google.android.exoplayer2.demo.PlayerActivity$1.onAudioSessionId(PlayerActivity.java:392)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.google.android.exoplayer2.SimpleExoPlayer$ComponentListener.onAudioSessionId(SimpleExoPlayer.java:1643)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.google.android.exoplayer2.audio.AudioRendererEventListener$EventDispatcher.lambda$audioSessionId$5$AudioRendererEventListener$EventDispatcher(AudioRendererEventListener.java:170)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.google.android.exoplayer2.audio.-$$Lambda$AudioRendererEventListener$EventDispatcher$a1B1YBHhPRCtc1MQAc2fSVEo22I.run(Unknown Source:4)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at android.os.Handler.handleCallback(Unknown Source:2)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at android.os.Handler.dispatchMessage(Unknown Source:4)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at android.os.Looper.loop(Unknown Source:139)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at android.app.ActivityThread.main(Unknown Source:146)
2020-02-14 22:03:52.029 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-02-14 22:03:52.030 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Unknown Source:11)
2020-02-14 22:03:52.030 6994-6994/com.google.android.exoplayer2.demo W/System.err:     at com.android.internal.os.ZygoteInit.main(Unknown Source:198)
master255 commented 4 years ago

@kim-vde This error is due to the fact that I did not add the permission to record the sound. Now I'm testing...

master255 commented 4 years ago

@kim-vde It's hard to reproduce. I found the truth! An audio session doesn't work if it's generated when the application has no audio focus. Only in Visualizer.

master255 commented 4 years ago

@kim-vde No. I get an audio focus, but the session still doesn't work. Testing. Don't close the bug.

master255 commented 4 years ago

@kim-vde Perfect! I found the bug! And I was able to finish the demo application so it could reproduce it, too. The app's in the attachment. Bug calls the line: player.seekTo(2147); When I need to restore the audio position, it happens. And one time out of 5-20 player launches. http://master255.org/res/%d0%9c%d1%83%d0%b7%d1%8b%d0%ba%d0%b0/0-9/3rd%20Force/3rd%20Force%20-%20Here%20Comes%20The%20Night.flac Place the file like this: "name": "Misc", "samples." [ { "name": "Dizzy (MP4)." "uri": "/storage/emulated/0/MediaLibrary/3rd Force - Here Comes The Night. flac" }, Then it must be restarted several times and you will get a non-working audio session. Tadaa!

main.zip

master255 commented 4 years ago

And I've already won the bug with that logic: boolean haveStartPosition = startWindow != C.INDEX_UNSET; if (haveStartPosition) { player.seekTo(startWindow, startPosition); } player.prepare(mediaSource, !haveStartPosition, false);

If you don't think it's a bug, you can close it. But there's a problem with the audio session. It just doesn't interfere with the processes right now.

kim-vde commented 4 years ago

What happens is that the visualiser is configured while the audio track is being released and rebuilt. Sometimes, the audio track does not exist anymore when the visualiser accesses it, which causes the bug.

To fix this, you have 2 possibilities:

We already have plans to avoid recreating audio tracks when possible. Hopefully, this will fix this bug.