Comments (11)
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.
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.
@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.
@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.
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.
@johnsonsu I see, Thank you, It works:)
from react-native-sound-player.
@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.
@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.
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.
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
from react-native-sound-player.
@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)
- NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference HOT 2
- Improvements
- On Creation of apk HOT 5
- [BUG] setNumberOfLoops not a function
- Cannot hear sounds in ios version 13 and can hear sounds in ios version 12 HOT 3
- [BUG] Sound keeps playing if the app is killed manually HOT 1
- Add new event listener which returns the current duration as the audio progresses. HOT 3
- Audio not playing[BUG]
- [BUG]: TypeError: Cannot read property 'playSoundFile' of null HOT 1
- [ERROR] Build failed with ios HOT 1
- Invariant Violation with IoS
- Is this repo actively maintained? HOT 1
- [BUG] - Android - SoundPlayer.loadUrl crash HOT 2
- [Question] I have a question!! HOT 2
- No sound received when App is in background/kill mode HOT 1
- [BUG] play multiple sounds HOT 2
- [BUG] iOS silence mode doesn't play on .playUrl HOT 1
- [BUG] when playing sounds, it crashes with saying attempt to invoke virtual method 'void.android.media.MediaPlayer.setOnCompletionListener HOT 1
- [iOS] Problem listening FinishedLoading & FinishedPlaying HOT 5
- [BUG] accumulation of addEventListener callbacks when using fast-reload HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-sound-player.