react-native-audio-toolkit: [Android] PlayerId 1 not found

Bug

Hello, I have the 2.0.0 version of your library, but on Android very often I can’t start the audio, because I have an error on the player not found!

On iOS all works, on Android, only 20% of installations works, so isn’t the installation of the library the problem.

The canPlay parameters is always false and the error is

PlayerId 1 not found.

This is my player, you can see the id is 1 and not 0. But I’m sure I’ve only one player running.

"player": {
        "_pan": 0,
        "_path": "file:\/\/\/data\/user\/0\/com.rideme\/files\/vale_cavanata.mp3",
        "_speed": 1,
        "_state": -1,
        "_volume": 1,
        "_looping": false,
        "_options": {
            "autoDestroy": true,
            "continuesToPlayInBackground": true
        },
        "_duration": 133355,
        "_lastSync": 1565278206789,
        "_playerId": 1,
        "_position": 0,
        "_wakeLock": true
    },

Environment info

React native info output:

System:
      OS: macOS 10.14.6
      CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
      Memory: 944.90 MB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.4.0 - ~/.nvm/versions/node/v8.4.0/bin/node
      npm: 6.3.0 - ~/.nvm/versions/node/v8.4.0/bin/npm
      Watchman: 4.7.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3
      Android SDK:
        API Levels: 19, 23, 25, 26, 27, 28
        Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.3
        System Images: android-19 | Intel x86 Atom, android-22 | Google APIs Intel x86 Atom, android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-26 | Google APIs Intel x86 Atom, android-26 | Google Play Intel x86 Atom, android-27 | Google APIs Intel x86 Atom
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5692245
      Xcode: 10.3/10G8 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.3 => 16.6.3
      react-native: ^0.58.5 => 0.58.5
    npmGlobalPackages:
      create-react-native-app: 2.0.2
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7

Library version: 2.0.0

Steps To Reproduce

  1. Install the library
  2. Run on several android devices

Describe what you expected to happen:

  1. Play the audio on every android

Please, I’m stuck on this problem for too long. Can you help me? Someone with the same error? Thanks!!

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 7
  • Comments: 20

Most upvoted comments

On javascript side you need to make this setting only for android while initializing player

Just need to set this.player.speed = 0.0 only for android

Screen Shot 2019-12-09 at 11 53 56

I found the solution by setting this.player.speed = 0.0 intially will fix this issue and the reason is during player initialization player.pause is getting called even though it’s not playing yet, so setting this speed to 0 ensure will not call pause method on player first time

here is the method which is causing issue Source: AudioPlayerModule.java `public void set(Integer playerId, ReadableMap options, Callback callback) { …
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (options.hasKey(“speed”) || options.hasKey(“pitch”))) {

        PlaybackParams params = new PlaybackParams();
        boolean needToPauseAfterSet = false;
        if (options.hasKey("speed") && !options.isNull("speed")) {
            // If the player wasn't already playing, then setting the speed value to a non-zero value
            // will start it playing and we don't want that so we need to make sure to pause it straight
            // after setting the speed value
            boolean wasAlreadyPlaying = player.isPlaying();
            float speedValue = (float) options.getDouble("speed");
            needToPauseAfterSet = !wasAlreadyPlaying && speedValue != 0.0f;

            params.setSpeed(speedValue);
        }

        if (options.hasKey("pitch") && !options.isNull("pitch")) {
            params.setPitch((float) options.getDouble("pitch"));
        }

        player.setPlaybackParams(params);

        if (needToPauseAfterSet) {
            player.pause();
        }
    }

… }`

Still an issue for me and @pavm035 's change doesn’t seem to help.

@HieuDevX Sorry, my typo mistake, mine actually has dependency:

useEffect(() => {
    setTimeout(() => { // works
      initPlayer()
    }, 0)
}, [])

😃

same here - this is my error log stacktrace : {“stackTrace”:“dalvik.system.VMStack.getThreadStackTrace(Native Method)\njava.lang.Thread.getStackTrace(Thread.java:580)\ncom.reactnativecommunity.rctaudiotoolkit.AudioPlayerModule.errObj(AudioPlayerModule.java:120)\ncom.reactnativecommunity.rctaudiotoolkit.AudioPlayerModule.errObj(AudioPlayerModule.java:141)\ncom.reactnativecommunity.rctaudiotoolkit.AudioPlayerModule.play(AudioPlayerModule.java:381)\njava.lang.reflect.Method.invoke(Native Method)\ncom.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)\ncom.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:158)\ncom.facebook.react.bridge.queue.NativeRunnable.run(Native Method)\nandroid.os.Handler.handleCallback(Handler.java:739)\nandroid.os.Handler.dispatchMessage(Handler.java:95)\ncom.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)\nandroid.os.Looper.loop(Looper.java:148)\ncom.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:232)\njava.lang.Thread.run(Thread.java:818)\n”,“message”:“playerId 3 not found.”,“err”:“notfound”}

Same issue here. Can someone please look into this?

Hi! My workaround for this is checking for duration saved in player instance. If the audio is not yet loaded from the network the duration will be -1.

My play/Pause function looks something like this:

 _playPause() {
    if (this.player.duration <= 0) {
      return;
    }
    this.player.playPause((err, paused) => {
      if (err) {
        this.setState({
          error: err.message
        });
      }
    });
  }

Also with version 2.2.0 nothing change…