Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Play 2 sounds?  (Read 4897 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Play 2 sounds?
« on: April 03, 2012, 11:59:35 am »
So i got SFML working...

How do I load a new sound in the buffer?

Code: [Select]
sf::SoundBuffer Buffer;
if (!Buffer.LoadFromFile("miss.ogg"))
return EXIT_FAILURE;

// Display sound informations
std::cout << "sound.wav :" << std::endl;
std::cout << " " << Buffer.GetDuration()      << " sec"           << std::endl;
std::cout << " " << Buffer.GetSampleRate()    << " samples / sec" << std::endl;
std::cout << " " << Buffer.GetChannelsCount() << " channels"      << std::endl;

// Create a sound instance and play it
sf::Sound Sound(Buffer);
Sound.Play();

system("Pause");

if (!Buffer.LoadFromFile("hit.ogg"))
return EXIT_FAILURE;

// Display sound informations
std::cout << "sound.wav :" << std::endl;
std::cout << " " << Buffer.GetDuration()      << " sec"           << std::endl;
std::cout << " " << Buffer.GetSampleRate()    << " samples / sec" << std::endl;
std::cout << " " << Buffer.GetChannelsCount() << " channels"      << std::endl;

// Create a sound instance and play it
sf::Sound Sound(Buffer);
Sound.Play();

// Wait until the user presses 'enter' key
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');

return EXIT_SUCCESS;

So how do i play other sounds.. do I make a new buffer each time? Or do I just load new sounds into the buffer and then play them with sound.play?
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Play 2 sounds?
« Reply #1 on: April 03, 2012, 12:22:15 pm »
You can reuse the same buffer, like you did, if you want to play sounds sequentially. You'll need two buffers only if you want to play multiple sounds together, or if you want to pre-load sounds.
Laurent Gomila - SFML developer

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Play 2 sounds?
« Reply #2 on: April 03, 2012, 09:22:49 pm »
yeah i could get it working using a different buffer for each sound.. but the above code will not change the sound... how do I load a new sound into the same buffer ? I suppose I can load a new sound into each buffer... kind of pre-load all the sounds I need... but then i might run out of stuff.. how do you unload a memory form the buffer.. liek clear it.. to either load a new sound into it.. or?
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Play 2 sounds?
« Reply #3 on: April 03, 2012, 11:21:33 pm »
Calling LoadFromFile again on the same sf::SoundBuffer replaces its contents with the new audio file.
Laurent Gomila - SFML developer

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Play 2 sounds?
« Reply #4 on: April 11, 2012, 12:55:55 am »
you speaks as if the example I posted would work? It doesn't seam to ?

How do I destroy a "buffer" once it is created? Like i can only seam to make a global sound loaded for entire project. I wanted to have a new set of sounds loaded depending on what level I am on.. but I can not work out how to load a new sound into the same buffer.. it always plays the same file form what was loaded first time.
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Play 2 sounds?
« Reply #5 on: April 11, 2012, 08:13:22 am »
Yes, the above code would work, but it is obviously not your actual code since it won't compile (you declare two variables named "Sound" in the same scope).

There's really nothing complicated with sounds and buffers. After calling LoadFromFile, the buffer contains the new audio file, that's it. I suspect that you're doing something really strange in your code, can you show it?
Laurent Gomila - SFML developer

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Play 2 sounds?
« Reply #6 on: April 14, 2012, 03:14:46 am »
ok, thanks for your help, I know I am noobish. I have only started learning programming... I appreciate your patience with such a low level user.
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

 

anything