Home > Back-end >  TrackPlayer.destroy is not a function
TrackPlayer.destroy is not a function

Time:08-19

I got TrackPlayer.destroy is not a function. (In 'TrackPlayer.destroy()', 'TrackPlayer.destroy' is undefined error when I called TrackPlayer destroy method in my expo react native app

this is my code
the service.ts

import TrackPlayer, {Event} from 'react-native-track-player';

module.exports = async function(): Promise<void> {
// This service needs to be registered for the module to work
// but it will be used later in the "Receiving Events" section
TrackPlayer.addEventListener('remote-play' as Event, () => TrackPlayer.play());

TrackPlayer.addEventListener('remote-pause' as Event, () => TrackPlayer.pause());

TrackPlayer.addEventListener('remote-stop' as Event, () => TrackPlayer.destroy());
}

and I imported it in my index.js

import TrackPlayer from 'react-native-track-player'
...
...
TrackPlayer.registerPlaybackService(() => require('./service'));

and this is the react-native-track-player configuration in my App.tsx

import TrackPlayer, {Capability} from 'react-native-track-player'
...

const initializeTrackPlayer = React.useCallback(async () => {
await TrackPlayer.setupPlayer({
  waitForBuffer: true,
})

await TrackPlayer.updateOptions({
  stoppingAppPausesPlayback: true,
  capabilities: [
      Capability.Play,
      Capability.Pause,
      Capability.SkipToNext,
      Capability.SkipToPrevious,
      Capability.Stop,
      Capability.SeekTo,
  ],
  compactCapabilities: [
    Capability.Play, 
    Capability.Pause,
    Capability.SkipToNext,
    Capability.SkipToPrevious,
    Capability.SeekTo,
    Capability.Stop,
  ],
  icon: require('./assets/icon.png')
})
}, [])
React.useLayoutEffect(() => {
  initializeTrackPlayer()
}, [])

so I am listening for remote events like from the notification bar on Android, it works fine if I click on play/pause, but when I click on stop icon which will trigger the TrackPlayer.destroy() method but it gives the above error TrackPlayer.destroy() is not a function

It also gives the same error if I call the TrackPlayer.destroy() method within the app

please what could be the cause of the error?

thanks in advance

CodePudding user response:

I have found out what went wrong, the destroy function was removed from react-native-track-player few minutes before I installed it and that's like 15 - 20 hours now

this is the link to the PR https://github.com/doublesymmetry/react-native-track-player/pull/1645/files#diff-67806635aecad4c3ddf7d36728b6510a7904dcab82efb6b775bff19f77d6b7b1

and more details on it https://github.com/doublesymmetry/react-native-track-player/releases/tag/v3.0

from the PR
On Android, the audio service can't be manually stopped by the app anymore. The OS itself decides when to stop it.

CodePudding user response:

What's your react-native-track-player version? There's some bugs on versions 2.2.0-rc3 and 2.2.0-rc4, maybe it's your case. If it is, you have to upgrade to the newest version.

Check this issue.

  • Related