Closed shahzore-qureshi closed 6 months ago
the android example app on android 14 should only give a cycling dependency warning and another image warning, likely could be your setup
I forgot to update this issue - I was able to resolve this issue by changing how I initialized the player. Here is my index.js
file:
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import { playbackService } from './musicPlayerServices';
import TrackPlayer from 'react-native-track-player';
AppRegistry.registerComponent(appName, () => App);
TrackPlayer.registerPlaybackService(() => playbackService);
Here is the playbackService
function that I imported and passed to the TrackPlayer registration:
export function playbackService() {
TrackPlayer.addEventListener(Event.RemotePause, () => {
TrackPlayer.pause()
.then(() => {})
.catch(() => {});
});
TrackPlayer.addEventListener(Event.RemotePlay, () => {
TrackPlayer.play()
.then(() => {})
.catch(() => {});
});
TrackPlayer.addEventListener(Event.PlaybackProgressUpdated, () => {});
}
If you look closely enough, the function is not a Promise. That is why I am getting the error Cannot read property 'then' of undefined
. It is because TrackPlayer is expecting a Promise.
To fix this issue, I added "async" in front of the function definition like so:
export async function playbackService() {
TrackPlayer.addEventListener(Event.RemotePause, () => {
TrackPlayer.pause()
.then(() => {})
.catch(() => {});
});
TrackPlayer.addEventListener(Event.RemotePlay, () => {
TrackPlayer.play()
.then(() => {})
.catch(() => {});
});
TrackPlayer.addEventListener(Event.PlaybackProgressUpdated, () => {});
}
I hope this helps someone out there!
I hope this helps someone out there!
Yes, it definitely helped someone (me)
Describe the Bug When calling the function below:
the error below occurs:
After this error is closed, everything seems to work fine.
Here is a screenshot:
Steps To Reproduce Run
TrackPlayer.setupPlayer()
on Android 14 (API Level 34) in a Pixel 3a emulator provided by Android Studio. This error occurred on a 2023 MacBook Air M2.Code To Reproduce
Replicable on Example App? Can you replicate this bug in the React Native Track Player Example App?
I have not tried. I will go ahead, try right away and then update this ticket.
Environment Info: Paste the results of
npx react-native info
:Paste the exact
react-native-track-player
version you are using:4.0.1-nightly-a0fdd21f77afb4c11cb7062c6665dfd337cef117
Real device? Or simulator?
Simulator (Android 14 (API Level 34) in a Pixel 3a emulator provided by Android Studio)
What OS are you running?
macOS Sonoma 14.1
How I can Help What can you do to help resolve this?
I am willing to try any steps provided by the community to help resolve this. For now, I'll go ahead and ignore the error since it does not seem to break any functionality.
Have you investigated the underlying JS or Swift/Android code causing this bug?
I have not. However, I can certainly do that.
Can you create a Pull Request with a fix?
Sure!