React-native-track-player: SeekTo on iOS does not work unless the track has started playing

Created on 9 Jan 2019  路  13Comments  路  Source: react-native-kit/react-native-track-player

For example, the code below does not seek on ios unless I play the song first (i am unsure of the behavior on android). Is this the expected behavior? And if so, is there any workaround that would allow me to seek a track that is not playing?

await TrackPlayer.setupPlayer();
await TrackPlayer.add([
    {
      url: 'fakeUrl',
      title: 'fakeTitle',
      artist: 'fakeArtist',
      id: 'fakeId',
    },
  ]);
await TrackPlayer.seekTo(5)

I would expect the code above to start playing the song at the 5 second mark, but instead it always starts from the beginning. But after it starts, then the seekTo works appropriately.

Specs:
"react": "16.6.3",
"react-native": "0.57.8",
"react-native-track-player": "^1.0.2"

iOS

Most helpful comment

@brenwell @robertblackwell Would either of you two be able to put a PR based on your fork? It's hard to tell what is a needed change and what is not based on the fork being out of date so if you could rebase then put up a PR it would help a ton.

The work around of play -> seekTo -> pause for this bug/behavior work but isn't great because on slower devices the user can actually here the audio.

All 13 comments

Hello,

Actually, I have the same problem on android and iOS.
I wanted to make an issue but you did before me ! :)

The only solution is, as you said, play the audio and seek to the time.

Is it a bug or the function has not been developed yet ? @Guichaguri

It's my understanding that this is a problem on iOS side, as the library RNTrackplayer is using to wrap AVPlayer doesn't have this capability.

The best way to get this functionality is to feature request the guys/gals behind AudioPlayer

Small update, indeed the problem is only on iOS.
What is our chance to get this functionality ? Because it's an annoying issue for podcasts.

I looked into this and had to modify the https://github.com/delannoyk/AudioPlayer library to get it working

This fork was made by my father and though it is quite behind on the Android side it has the functionality added for iOS I believe https://github.com/robertblackwell/react-native-track-player

A fork is too risky for me :(
No chance to have that on this repo ?

Any other way to fix that ?

@brenwell @robertblackwell Would either of you two be able to put a PR based on your fork? It's hard to tell what is a needed change and what is not based on the fork being out of date so if you could rebase then put up a PR it would help a ton.

The work around of play -> seekTo -> pause for this bug/behavior work but isn't great because on slower devices the user can actually here the audio.

I'm using the dev branch (specifically react-native-track-player#8ae5808340c435b450c71dbe4da5e75101c7b720) which has this issue as well.

The following doesn't work:

await TrackPlayer.add(track);
await TrackPlayer.skip(track.id);
await TrackPlayer.play();
TrackPlayer.seekTo(15000);

The only workaround I've found for this is to wait a bit after play:

await TrackPlayer.add(track);
await TrackPlayer.skip(track.id);
await TrackPlayer.play();

setTimeout(() => {
    TrackPlayer.seekTo(15000);
}, 1500)

I've also tried adding an event listener for ready:

  useTrackPlayerEvents([
    TrackPlayer.TrackPlayerEvents.PLAYBACK_STATE,
  ], async event => {
    if (event.type === TrackPlayer.TrackPlayerEvents.PLAYBACK_STATE && event.state === TrackPlayer.STATE_READY) {
      TrackPlayer.seekTo(5000);
    }
  });

However, that fails with the following error:

Possible Unhandled Promise Rejection (id: 0):
Error: Given track ID was not found in queue
...
Possible Unhandled Promise Rejection (id: 1):
Error: Given track ID was not found in queue

Any insights here would be helpful. I'd also happily create an MR to fix this if someone would push me off in the right direction.

One hack around this problem is to apply this ios only patch https://gist.github.com/gilbertl/82e3043fa5632ef653b274a2b86f59f2

then pass in iosInitialTime in your Track object to skip immediately to that spot.

There's no equivalent in Android because the interface would be different: https://github.com/react-native-kit/react-native-track-player/pull/713

I tried the patch from @gilbertl and it seems to work well for iOS - thank you 馃檹
Hope we can have an iOS/Android option in the package soon.

I tried the patch from @gilbertl and it seems to work well for iOS - thank you 馃檹
Hope we can have an iOS/Android option in the package soon.

@davidsalib how to add this path file provided by @gilbertl

hi @rahutwr33 have you fixed this problem yet?

@tnghia944 yes. the songs url should be https.track player is working fine

~The patch is not working for me. Does the patch alone make it work or I have to add something else?~

Nevermind, I just understood what this quote meant

then pass in iosInitialTime in your Track object to skip immediately to that spot.

It works for me too! It would be nice if it was an update. Also, could/should it be a setupPlayer or updatePlayer parameter?

Was this page helpful?
0 / 5 - 0 ratings