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

Author Topic: I need urgent help with a project's music player  (Read 15283 times)

0 Members and 1 Guest are viewing this topic.

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
I need urgent help with a project's music player
« on: December 18, 2013, 04:03:51 am »
By December 27, 2013 I will need to have created a music player for a personal project.  My schedule is extremely strict for my project to be successful.  I've been fine with playing one music file.  What I need now is to know how to get different music files to play like an itunes playlist.  What I'm thinking is having music files in a folder and having a stream read that folder.  BUT I have no idea how to do this.  I know that I have to run the music player in a separate thread than the main menu, but have the main menu change variables for playlists to play.  What I'm missing now is just, how do you make a playlist? Can you make one sf::Music function have more than one file assigned?  If not, how can I achieve something similar?  Any help will be appreciated.  Thank you very much.



For those of you who are curious, my project is a smart helmet.  I think I'm calling it Smart FML Helmet in honor of the library.  I chose to use SFML because although the library is made for games, it seems to have everything I need for my project.  My software will be a main menu that will have access to a music menu and a stats menu.  The music meny will allow you to chose from different playlists/genres.  The stats menu will tell you basic stats about the helmet, like time it's been used or battery left.  The menu will be easily accessible from a joypad.  My idea is to rebuild my PC in my helmet with along with an audio system and an HUD (with a small screen)  A joypad will be connected via bluetooth (I'm also having trouble with that if anyone wants to help).  The joypad will have easy music access controls.  Changing a playlist will be as easy as changing a radio station in Grand Theft Auto V (I could also use help with playlist changing).  A strap for the joypad will be built for it to be able to strap onto a motorcycle or ATV handle bar.  Thank you for all support in this project.  All help will be appreciated.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: I need urgent help with a project's music player
« Reply #1 on: December 18, 2013, 09:36:11 am »
I know that I have to run the music player in a separate thread than the main menu
You don't. sf::Music internally already starts a thread to play the music, so you can handle it asynchronously from your main thread.

What I'm missing now is just, how do you make a playlist?
Are you familiar with STL containers? Then it can be something as simple as std::vector<std::string> with the list of filenames. In your main loop, you check whether the current music is still playing, and if it is not, you advance to the next file, open and play it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #2 on: December 26, 2013, 06:17:30 pm »
Are you familiar with STL containers?
I'm sorry.  I'm really new to this.  I'm trying to make the best out of the little stuff I know.  I'm not familiar at all with STL containers.  What I am doing is having a string be my filename.  What I'm having trouble with now is checking the music status.

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <iostream>

int main()
{

//Playlist track number
std::string testMusicFilename;
int testMusic = 1;
//defining filename strings

    switch (testMusic) {
    case 0:
        testMusic = 4;
    case 1:
        testMusicFilename = "CheckYoSelf.wav";
        break;
    case 2:
        testMusicFilename = "Helicopter.wav";
        break;
    case 3:
        testMusicFilename = "BittersweetSymphony.wav";
    case 4:
        testMusic = 1;
        break;
    default:
        testMusicFilename = "LevelsGoodFeeling.wav";
        break;
}

//Window, sprites, and textures
    sf::RenderWindow window(sf::VideoMode(1350, 700), "Smart Fast Multimedia Library Helmet");
    sf::Texture mainMenuTexture;
    if (!mainMenuTexture.loadFromFile("titleScreen.png")) {
        return -1;
    }
    mainMenuTexture.setSmooth(true);
    sf::Sprite mainMenuSprite;
    mainMenuSprite.setTexture(mainMenuTexture);
//Opening the music file
    sf::Music testingPlaylist;
    if (!testingPlaylist.openFromFile(testMusicFilename)) {
        return - 1;
    }

//Main Window Loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))  //Making sure that the Window can close
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
//Playing Music
        testingPlaylist.play();
        enum musicStatus = testingPlaylist(sf::SoundSource::getStatus()));
        if (musicStatus == Stopped) {
            testMusic++;
            testingPlaylist.play();
        }

        window.clear();
        window.draw(mainMenuSprite);
        window.display();
    }

    return 0;
}

Can you tell what's wrong?  In that "Playing music" section, how can I get that if statement to work?  How can I say "if the music is stopped, run this code"?  Thanks.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: I need urgent help with a project's music player
« Reply #3 on: December 27, 2013, 01:16:43 am »
I'm sorry.  I'm really new to this.  I'm trying to make the best out of the little stuff I know.  I'm not familiar at all with STL containers.
You had a week time, and the STL container basics would have been learned in a few hours (or less, if you know C++ well). I think that might have been less time than caused by the trouble you have without them ;)

enum musicStatus = testingPlaylist(sf::SoundSource::getStatus()));
Why the enum keyword, and what kind of function call is that? You have to use the correct type (a member of sf::Music, maybe Status, but look at the docs).
sf::Music::Status status = testingPlaylist.getStatus();

And your variable is badly named; testingPlaylist is a single music file, not a playlist. As stated, you need STL containers for lists...
« Last Edit: December 27, 2013, 01:19:30 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #4 on: December 27, 2013, 04:48:35 pm »
I'm sorry.  I'm really new to this.  I'm trying to make the best out of the little stuff I know.  I'm not familiar at all with STL containers.
You had a week time, and the STL container basics would have been learned in a few hours (or less, if you know C++ well). I think that might have been less time than caused by the trouble you have without them ;)

enum musicStatus = testingPlaylist(sf::SoundSource::getStatus()));
Why the enum keyword, and what kind of function call is that? You have to use the correct type (a member of sf::Music, maybe Status, but look at the docs).
sf::Music::Status status = testingPlaylist.getStatus();

And your variable is badly named; testingPlaylist is a single music file, not a playlist. As stated, you need STL containers for lists...


Sorry about not learning to use STL containers. I barely learned the basics before engaging in my projects, and I spent the week learning SFML concepts.  I thought STL containers had something to do with SFML.  My bad!  I'll try to research them right now.  May I ask you for one last favor?  How can I use the music status in an if statement?  Like, how can I make sure that the song will change if the music is stopped?  SFML has different syntax than other C++ libraries for if statements, so I'm not sure that I'll be doing it right.  Thanks for all your help and suggestions!  You really have helped me a lot.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: I need urgent help with a project's music player
« Reply #5 on: December 27, 2013, 08:33:44 pm »
How can I use the music status in an if statement?  Like, how can I make sure that the song will change if the music is stopped?
Dude, if statements are really most basic C++ stuff, you should have learned them at the very beginning. But since it's Christmas, I'll show you nevertheless ;)
if (music.getStatus() == sf::Music::Stopped)
    ... // change music (I still don't know how you want to do this, maybe reload?)

SFML has different syntax than other C++ libraries for if statements
No, totally not. if statements are a language feature, their syntax is the same everywhere.

You only have to combine the different parts (like the music status) meaningfully to get a boolean condition.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #6 on: January 06, 2014, 04:44:01 am »
Nexus, thank you for everything.  You've been extremely nice and helpful.  I was just hoping you could help me with one las thing.  Why won't the song change once the previous one is done?
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>


int main()
{

//Music Status Text
            sf::Text playingText;
            sf::Font base;
            if (!base.loadFromFile("Base 02.ttf")) {
                return -1;
            }
            playingText.setFont(base);
            playingText.setString("Music is Playing");
            playingText.setCharacterSize(24);
            playingText.setColor(sf::Color::Green);
//Playlist track number
int trackNumber = 2;
if (trackNumber == -1) {
    trackNumber = 3;
}
else if (trackNumber == 4) {
    trackNumber = 0;
}
else {trackNumber = trackNumber;}
//Vector Container
std::vector <std::string> testingFiles;
testingFiles.push_back("BitterSweetSymphony.wav");
testingFiles.push_back("Helicopter.wav");
testingFiles.push_back("CheckYoSelf.wav");
testingFiles.push_back("LevelsGoodFeeling.wav");
//Window, sprites, and textures
    sf::RenderWindow window(sf::VideoMode(1200, 700), "Smart Fast Multimedia Library Helmet");
    sf::Texture mainMenuTexture;
    mainMenuTexture.loadFromFile("titleScreen.png");
    sf::Sprite mainMenuSprite;
    mainMenuSprite.setTexture(mainMenuTexture);
//Opening the music file
    sf::Music testingPlaylist;
    testingPlaylist.openFromFile(testingFiles[trackNumber]);
//Main Window Loop
    while (window.isOpen())
    {
        sf::Event windowIsClosed;
        while (window.pollEvent(windowIsClosed))  //Making sure that the Window can close
        {
            if (windowIsClosed.type == sf::Event::Closed)
                window.close();
        }
        //Playing Music
        testingPlaylist.play();
        window.clear();
        window.draw(mainMenuSprite);
        window.display();
        if (testingPlaylist.getStatus() == sf::Music::Stopped) {
            trackNumber++;
            testingPlaylist.play();
        }
                while (testingPlaylist.getStatus() == sf::Music::Playing) {
          window.clear();
            window.draw(mainMenuSprite);
            window.draw(playingText);
            window.display();
        }
        //Drawing the window
        window.clear();
        window.draw(mainMenuSprite);
        window.display();
        }  /*Window Loop Ending*/

    return 0;
}  /*Main Function ending*/

 
I think that will be the last I ask for, although I am not sure.  I finally learned about STL and Vector containers .  Thanks for all the suggestions you have given me.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: I need urgent help with a project's music player
« Reply #7 on: January 06, 2014, 08:45:08 pm »
You don't change the sf::Music object, you only call its play() method.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #8 on: January 06, 2014, 10:52:47 pm »
So, should I use it like:
play(testingPlaylist)
 
this?  How exacly do you call play for only one specific music object without changing it?

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #9 on: January 07, 2014, 01:33:25 am »
Nevermind.  Sorry, I realize the stupidity in my question.  I thought you were saying I didn't have to use testingplaylist.play() again.  My bad! :-[

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #10 on: January 08, 2014, 02:50:12 am »
My program is freezing and functioning terribly.  I'm not sure what's wrong or what's causing this.  I finally successfully got the music to move on, but I still have that one major issue.  Trying to click on the window will allow the program to keep on playing the music, but the window will freeze and stop working.  Might there be something in my code that causes this?  I've already tried to make the window smaller than the desktop so I doubt that it has to do with that.  I also tried adding a testing interaction (pausing the music when the space key is pressed) to see if that made  What do you think is causing my program to do this?
My current code:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>


int main()
{
//Rendering the Window
            sf::RenderWindow window(sf::VideoMode(1200, 400), "Smart Fast Multimedia Library Helmet");
            window.setVerticalSyncEnabled(true);
            window.setFramerateLimit(30);
//Music Status Text
            sf::Text playingText;
            sf::Font base;
            if (!base.loadFromFile("Base 02.ttf")) {
                return -1;
            }
            playingText.setFont(base);
            playingText.setString("Music is Playing");
            playingText.setCharacterSize(24);
            playingText.setColor(sf::Color::Green);
            sf::Text pausedText;
            pausedText.setString("Music is Paused");
            pausedText.setCharacterSize(24);
            pausedText.setColor(sf::Color::Green);
//Playlist track number
int trackNumber = 3;
while (trackNumber == 4) {
    trackNumber = 0;
}
while (trackNumber == -1) {
    trackNumber == 3;
}
//Vector Container
std::vector <std::string> testingFiles;
testingFiles.push_back("BitterSweetSymphony.wav");
testingFiles.push_back("Helicopter.wav");
testingFiles.push_back("CheckYoSelf.wav");
testingFiles.push_back("LevelsGoodFeeling.wav");
//Window, sprites, and textures
    while (window.isOpen())
{
    sf::Texture mainMenuTexture;
    mainMenuTexture.loadFromFile("titleScreen.png");
    sf::Sprite mainMenuSprite;
    mainMenuSprite.setTexture(mainMenuTexture);
//Opening the music file
    sf::Music testingPlaylist;
    testingPlaylist.openFromFile(testingFiles.at(trackNumber));
//Main Window Loop
        sf::Event windowIsClosed;
        while (window.pollEvent(windowIsClosed))  //Making sure that the Window can close
        {
            if (windowIsClosed.type == sf::Event::Closed)
                window.close();
        }
        //Playing Music
        testingPlaylist.play();
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
            testingPlaylist.pause();
        }
        //Making sure the track moves on
                while (testingPlaylist.getStatus() == sf::Music::Playing) {
          window.clear();
            window.draw(mainMenuSprite);
            window.draw(playingText);
            window.display();
        }
                while (testingPlaylist.getStatus() == sf::Music::Paused) {
          window.clear();
            window.draw(mainMenuSprite);
            window.draw(pausedText);
            window.display();
                }
         while (testingPlaylist.getStatus() == sf::Music::Stopped) {
            trackNumber++;
            testingPlaylist.play();
        }
        //Drawing the window
        window.clear();
        window.draw(mainMenuSprite);
        window.display();
        }  /*Window Loop Ending*/

    return 0;
}  /*Main Function ending*/

 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: I need urgent help with a project's music player
« Reply #11 on: January 08, 2014, 03:12:54 am »
You have multiple lets call it "mini drawing loops" where you aren't handling events. Remove other loops and you should be good.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #12 on: January 12, 2014, 02:43:29 am »
I tried deleting the while (testingPlaylist.getStatus() == sf::Music::Stopped) loop but it wouldn't really work after that.  The music simply wouldn't start

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: I need urgent help with a project's music player
« Reply #13 on: January 12, 2014, 03:08:19 am »
I tried deleting the while (testingPlaylist.getStatus() == sf::Music::Stopped) loop but it wouldn't really work after that.  The music simply wouldn't start

Ever tried if?
« Last Edit: January 12, 2014, 03:15:20 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Saadin Dassum

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: I need urgent help with a project's music player
« Reply #14 on: January 13, 2014, 12:12:48 am »
Quote
Ever tried if?
Yup.  I tried.  The music simply won't start.  Isn't there something that I could modify within those loops so that I could still get paused text and playing text?