Giter VIP home page Giter VIP logo

Comments (8)

leoasis avatar leoasis commented on June 29, 2024 5

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.

leoasis avatar leoasis commented on June 29, 2024

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.

th0th avatar th0th commented on June 29, 2024

@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.

leoasis avatar leoasis commented on June 29, 2024

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.

th0th avatar th0th commented on June 29, 2024

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.

leoasis avatar leoasis commented on June 29, 2024

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.

leoasis avatar leoasis commented on June 29, 2024

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.

muke1908 avatar muke1908 commented on June 29, 2024

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)

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.