Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackPlayer.getDuration() always give 0 #229

Closed
imran1992 opened this issue Jul 12, 2018 · 4 comments

Comments

@imran1992
Copy link

commented Jul 12, 2018

"react": "16.3.1",
"react-native": "0.55.4",
"react-native-track-player": "^0.2.4",
"Platform": "Android"
"OS":"7.0"
I think I am doing wrong. I am actually playing online audio by LINK.
In componentWillMount First I just add the track detail then set the remote capabilities and then I am trying to get the duration of audio which i just setup

componentWillMount() {
    TrackPlayer.setupPlayer().then(
      async () =>
        await TrackPlayer.add({
          id: "123",
          url: this.state.Url,
          title: "Track Title",
          artist: "Track Artist",
          artwork:
            "https://socialecologies.files.wordpress.com/2015/10/a_monk.jpg"
        }).then(() =>{
          TrackPlayer.updateOptions({
            capabilities: [
              TrackPlayer.CAPABILITY_PLAY,
              TrackPlayer.CAPABILITY_PAUSE,
              TrackPlayer.CAPABILITY_STOP
            ],
            compactCapabilities: [
              TrackPlayer.CAPABILITY_PLAY,
              TrackPlayer.CAPABILITY_PAUSE,
              TrackPlayer.CAPABILITY_STOP
            ]
          });
          this._GetTrackDuration()  //<-------Here I Called It
        }
        )
    )
  }

And Here is my getting duration of audio function:

_GetTrackDuration = async () => {
  let GetDuration = await TrackPlayer.getDuration();
   alert(GetDuration);
   this.setState({
   duration: GetDuration
   });
   };

Update: When I run it in Debug Mode It show the Trackplayer.getDuration() exactly what I expected but this time the Audio play 2 times. And without Debug Mode Audio play 1 time. My Code for Playing too:

componentDidMount() {
    this.play();
  }

My Play Function:

play = () => {
    this.setState({ playState: "playing" });
    TrackPlayer.play();
  };
@brenwell

This comment has been minimized.

Copy link

commented Aug 4, 2018

Duration is 0 until it plays. This maybe related to the issue where on track changed returns null for track on first play.

@dcvz

This comment has been minimized.

Copy link
Member

commented Oct 21, 2018

Is this still an issue in v1.0.0?

@Guichaguri

This comment has been minimized.

Copy link
Member

commented Oct 23, 2018

The duration will only be available after the module loads and analyses the track. It might be available before that if you feed it manually (the track object accepts a duration property). You also have to make sure you're only playing after the setupPlayer/add promises resolves.

@imran1992

This comment has been minimized.

Copy link
Author

commented Nov 6, 2018

OK. Will test it in other future project. For Now I am closing it.

@imran1992 imran1992 closed this Nov 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.