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

Author Topic: How to play another music after a music finish playing?  (Read 7642 times)

0 Members and 1 Guest are viewing this topic.

Kroga

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to play another music after a music finish playing?
« on: December 09, 2014, 10:17:10 am »
Hello,

What I mean is, for example, I want my program to play music1.ogg and after it finish playing, I want it to continue playing music2.ogg. This sounds like a simple question, but I just cannot figure out how to do it.

Thank you for helps provided.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to play another music after a music finish playing?
« Reply #1 on: December 09, 2014, 10:29:05 am »
You simply check in each frame iteration if the music is still playing and once it isn't anymore, you open the next music file and start playing that.
If there's a too big delay, you could use two music objects and switch back and forth between them.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kroga

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to play another music after a music finish playing?
« Reply #2 on: December 09, 2014, 10:49:16 am »
What is the exact code to do that? Sorry, I'm beginner.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to play another music after a music finish playing?
« Reply #3 on: December 09, 2014, 10:50:47 am »
You want to program, so you should either know or learn it. Nobody will go and write your code.

It's simple, look at the tutorial and documentation.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kroga

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to play another music after a music finish playing?
« Reply #4 on: December 09, 2014, 11:44:30 am »
I understand, and I had read the tutorial and documentation about Music and had tried many ways to let it check if the music has stopped, and if the music stopped, play the next music. Nothing seems to work. I guess the problem is that I only let it check once, but I could not figure out the way to let it check in each frame, even after I read through the documentation about Music and SoundSource. That's why I need at least a hint, like what function or anything else should I use to let it check in every frame.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to play another music after a music finish playing?
« Reply #5 on: December 09, 2014, 11:52:05 am »
Do you have a main loop, with a window, event handling, and drawing? Or all that your main() does is to play these musics sequentially? Can you show us the structure of your program?
Laurent Gomila - SFML developer

Kroga

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to play another music after a music finish playing?
« Reply #6 on: December 09, 2014, 12:06:48 pm »
The only SFML stuff I use is audio. Other parts of program are just combination of beginner stuffs such as cin&cout, if-else statements, and some loops which doesn't affect the musics.

It looks something like:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <SFML/Audio.hpp>

... //Some functions prototype

using namespace std;
using namespace sf;

int main()
{
cout<<"..."<<endl;

Music music;
if (!music.openFromFile("music1.ogg"))
        return -1;
music.play();

if (music.Stopped==1) // I tried a lot of other things to check if music1 has stopped or\ not too
{
        Music music;
        if (!music.openFromFile("music2.ogg"))
            return -1;
        music.play();
        music.setLoop(true);
}

... // Long code of cin, cout, if-else, loops, switch-case, etc.

cin.get();
return 0;

}

... // other functions
« Last Edit: December 09, 2014, 08:01:30 pm by eXpl0it3r »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to play another music after a music finish playing?
« Reply #7 on: December 09, 2014, 12:34:56 pm »
The problem here is that your program will continue until its end, regardless of the state of the musics. Whether the first music is finished or not, it performs the following steps/checks. What you must do is to block the execution until the first music is finished, before continuing with the second etc.

I won't show you how to do it, because it is shown in the official SFML examples ;)

Your code for testing the music status is also wrong, but this can easily be fixed if you look at the doc/tutorial/examples.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: How to play another music after a music finish playing?
« Reply #8 on: December 09, 2014, 06:47:15 pm »
And you should google "event loop" and "game loop", because starting with a convoluted mess of control structures where input and output code is needlessly repeated is a typical beginner mistake.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to play another music after a music finish playing?
« Reply #9 on: December 09, 2014, 07:56:37 pm »
Quote
And you should google "event loop" and "game loop"
There's no window so no event loop, and there's no game so obviously no game loop ;)

So far he's just trying to play two songs one after the other, don't make it more complicated than necessary.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to play another music after a music finish playing?
« Reply #10 on: December 09, 2014, 08:19:49 pm »
If you want a simple example, then check out my Jukebox example on the wiki.

 

anything