Code Monkey home page Code Monkey logo

Comments (11)

johnsonsu avatar johnsonsu commented on July 30, 2024 1

Hi @jaccotaco, you can try calling getInfo() and play() inside FinishedLoadingFile or FinishedLoadingURL callback block, and use loadUrl or loadSoundFile to play the music. Something like this:

  // Subscribe to event(s) you want when component mounted
  componentDidMount() {
    _onFinishedLoadingFileSubscription = SoundPlayer.addEventListener('FinishedLoadingFile', async ({ success, name, type }) => {
      console.log('finished loading file', success, name, type)
	  const info = await SoundPlayer.getInfo()
	  this.setState(...)
      SoundPlayer.play()
    })
    _onFinishedLoadingURLSubscription = SoundPlayer.addEventListener('FinishedLoadingURL', async ({ success, url }) => {
      console.log('finished loading url', success, url)
	  const info = await SoundPlayer.getInfo()
	  this.setState(...)
      SoundPlayer.play()
    })
  }

  // Remove all the subscriptions when component will unmount
  componentWillUnmount() {
    _onFinishedLoadingURLSubscription.remove()
    _onFinishedLoadingFileSubscription.remove()
  }

from react-native-sound-player.

johnsonsu avatar johnsonsu commented on July 30, 2024

Hi @jaccotaco, how are you calling the getInfo() function? Make sure you have the state in the function scope. If you add console.log('state', this.state) right after the try, do you get the state?

from react-native-sound-player.

jaccotaco avatar jaccotaco commented on July 30, 2024

@johnsonsu Hi! I call it on the "start-button", Finally I want to show a slider depended on the duration of the song. Do you have any ideas how to achive that?

from react-native-sound-player.

jaccotaco avatar jaccotaco commented on July 30, 2024

@johnsonsu But how can I show do duration in a state? When I do GetInfo() I get both currentTime and duration in a object, can I split them in some way to just get the duration?

from react-native-sound-player.

johnsonsu avatar johnsonsu commented on July 30, 2024

Yes, there are many ways to do it, for example a decontruct:

const { duration } = await SoundPlayer.getInfo()

Or you can event store both values in the state by doing:

const info = await SoundPlayer.getInfo()
this.setState({
  ...info,
})

from react-native-sound-player.

jaccotaco avatar jaccotaco commented on July 30, 2024

@johnsonsu I see, Thank you, It works:)

from react-native-sound-player.

Harsh-shah-coder avatar Harsh-shah-coder commented on July 30, 2024

@jaccotaco i m not getting the Current Time i m just Getting the Duration
// this is how i tried
componentDidMount() {
_onFinishedLoadingURLSubscription = SoundPlayer.addEventListener('FinishedLoadingURL', async ({ success, url }) => {
console.log('finished loading url', success, url)
const info = await SoundPlayer.getInfo()
console.log('INFO : ', info)
})
}
componentWillUnmount() {
_onFinishedLoadingURLSubscription.remove()
console.log('Umount is called')
}

Please Help

Thank You in Advance

from react-native-sound-player.

Harsh-shah-coder avatar Harsh-shah-coder commented on July 30, 2024

@johnsonsu i m not getting the Current Time i m just Getting the Duration
// this is how i tried
componentDidMount() {
_onFinishedLoadingURLSubscription = SoundPlayer.addEventListener('FinishedLoadingURL', async ({ success, url }) => {
console.log('finished loading url', success, url)
const info = await SoundPlayer.getInfo()
console.log('INFO : ', info)
})
}
componentWillUnmount() {
_onFinishedLoadingURLSubscription.remove()
console.log('Umount is called')
}

Please Help

Thank You in Advance

from react-native-sound-player.

jaccotaco avatar jaccotaco commented on July 30, 2024

@Harsh-shah-coder Hi!

This is how I store it in a state. And split it up so it looks like 1:20 eg.

async getDuration() {
const { duration } = await SoundPlayer.getInfo();
const minutesnow = Math.floor(duration / 60);
const secondsnow = Math.floor(duration % 60);
try {
this.setState({ maxvalue: duration });
this.setState({ durationsec: secondsnow });
this.setState({ durationmin: minutesnow });
} catch (e) {
console.log("There is no song playing", e);
}
}

from react-native-sound-player.

Harsh-shah-coder avatar Harsh-shah-coder commented on July 30, 2024

Thanks For Reply @jaccotaco But i m not getting the Value of Current Time

// this is how i tried
componentDidMount() {
_onFinishedLoadingURLSubscription = SoundPlayer.addEventListener('FinishedLoadingURL', async ({ success, url }) => {
console.log('finished loading url', success, url)
const info = await SoundPlayer.getInfo()
console.log('INFO : ', info)
})
}
componentWillUnmount() {
_onFinishedLoadingURLSubscription.remove()
console.log('Umount is called')
}

Please Help

Thank You in Advance
Screenshot 2020-01-06 at 3 36 17 PM

from react-native-sound-player.

Harsh-shah-coder avatar Harsh-shah-coder commented on July 30, 2024

@jaccotaco @johnsonsu I m getting Value of Current Time But need to convert it in 00:00 format so how can i do this any suggestion

desire OutPut : 00:00 , 00:01 , 00:02 like wise

// this is how i tried
async getInfo() {

try {
  const info = await SoundPlayer.getInfo();
  console.log('getInfo', info.currentTime)
  const minuteRound = Math.round(info.currentTime );
  const minuteFloor = Math.floor((info.currentTime % 1000) / 10);
  // const minuteFloor = Math.floor(info.currentTime );
  // const minuteFloor = Math.floor(info.currentTime / 60);
  // const secondRound = Math.round(info.currentTime % 60);
  // const secondFloor = Math.floor(info.currentTime % 60);

  console.log('DURATION : ', info.currentTime)
  console.log('minuteRound : ', minuteRound)
  console.log('minuteFloor : ', minuteFloor)
  // {duration: 12.416, currentTime: 7.691}
} catch (e) {
  console.log("There is no song playing", e);
}

}

Please Help
Thank You in Advance

from react-native-sound-player.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.