Comments (8)
Hi @mukesh-urbanpiper! The problem with the code you pasted is that you're trying to think about it in an imperative style. You cannot do Sound.position
since that's not doing what you intend it to do. The only way to change the props of a component is in the render method, by passing new props when the component re-renders.
To play in loop you need to keep the position controlled with local state, and when the song finishes playing, reset it to 0 so that it starts again. To update the position though, you need to use the onPlaying
prop. This is an example that does what you want:
class Notification extends React.Component {
constructor(props) {
super(props);
this.state = { position: 0 };
this.handlePlaying = this.handlePlaying.bind(this);
this.handleFinishedPlaying = this.handleFinishedPlaying.bind(this);
}
handlePlaying(ev) {
this.setState({ position: ev.position });
}
handleFinishedPlaying() {
this.setState({ position: 0 });
}
render() {
return(
<Sound url="./audio/a2.mp3"
playStatus={Sound.status.PLAYING}
position={this.state.position}
onPlaying={this.handlePlaying}
onFinishedPlaying={this.handleFinishedPlaying}
/>
);
}
};
Take a look at how the example handles the position of the sound using local state and re-rendering the component every time the position updates.
Hope this helps!
from react-sound.
It should be fairly easy, you can pass a callback prop to onFinishedPlaying
and reset the position
prop back to 0 anytime that one gets called.
from react-sound.
@leoasis The problem is that I don't want user to sense audio's ending.
The best solution I can think of is to start playing the same audio in a different Sound element before the first one ends. But this one seems too complicated, so if there isn't a better way, I'll try resetting position
something close to 0 just before the audio ends using onPlaying
.
from react-sound.
Ah so maybe I didn't understand the use case. The idea is to restart the sound when the sound wave is small enough that the user doesn't perceive anything anymore?
from react-sound.
The exact use case is, I am working on an application that plays calm sound tracks that will help user to focus. User selects the tracks to be played, and those tracks are played simultaneously. And I need to make a loop of the track, without user noticing track's ending or restarting.
from react-sound.
Have you tried using the loops
property of soundmanager2
(the underlying lib that actually plays the sound)? I believe this will cause the same effect as in the approach I suggested, but if for some reason it makes the restart smoother, it may be worth adding it to the lib somehow. If not, and you still need to restart a bit before the sound finishes, since it's a too specific use case, I'm afraid you'll have to resort to solving it in user-land
from react-sound.
Hope the above answer was good enough for your use case! Closing this issue since it's a little old, but feel free to reopen if you still need help
from react-sound.
Hi @leoasis , I am trying to play the audio in loop. But it seems I'm missing something.
Here is my component.
const Notification = React.createClass({
repeat:function(){
Sound.position = 0;
},
render: function(){
return(
<Sound url="./audio/a2.mp3"
playStatus='PLAYING'
position={0}
onFinishedPlaying={this.repeat}/>
)
}
})
from react-sound.
Related Issues (20)
- onPause does not work on iOS when running in background HOT 1
- Mute feature? HOT 2
- Audio stuck when switching networks
- Please update the yarn and npm packages HOT 1
- No supported file found
- Problem with react-sound with setInterval
- Issue with Status Typing HOT 5
- Issue Sith Dynamic Positioning
- How to handle multiple plays of the same sound
- AbortError: The fetching process for the media resource was aborted by the user agent at the user's request.
- External sounds are prevented in background [Mobile Firefox, Mobile Safari]
- Uncaught Error: The error you provided does not contain a stack trace. HOT 1
- 怎么去获取当前播放时间?
- onLoad event is not called after onLoading
- typescript issue with onPlaying callback
- Creating a new v1.2.1 release with recent bugfix in master
- React-sound soundmanager doesnt work with useHistory hook
- Duration in milliseconds
- Could not resolve dependency issue for newer react@ ^17 HOT 1
- Decrease update interval
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-sound.