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

Author Topic: can't play music files  (Read 6876 times)

0 Members and 1 Guest are viewing this topic.

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
can't play music files
« on: December 17, 2011, 05:23:37 pm »
hi,

I'm trying to load & play ogg and flac files. I wrote a class that uses sf::Music to load & play the files.

Although it seems to load them & even play them, I can't hear anything... Yes I have the volume turned up, and the device turned on :)

I have two different audio devices: my video card's hdmi output, and the internal sound card, which I use. I can play sounds with sf::Sound & sf::SoundBuffer, but not music. I'm on kUbuntu 11.10 64 bit, and using the latest SFML snapshot.

here's the code:
Code: [Select]

#ifndef sound_stream_h
#define sound_stream_h

#include "common.h"

class sound_stream
{
private:
    sf::Music samples;
    bool loaded;
protected:

public:

    void load(std::string filename);
    void play();
    void pause();
    void stop();
    int get_duration();
    void set_loop(bool loop);
    void set_pitch(float pitch);
    void set_volume(float volume);

    sound_stream() : loaded(false) {}
};

#endif


Code: [Select]

#include "sound_stream.h"
#include "objs.h"

void sound_stream::load(std::string filename)
{
    std::cout << "-Loading: " << filename << "\n";
    timer sound_timer;
    sound_timer.set_timerbegin();
    std::string full_path = objs::get()->conf.app_path + filename;
    if (objs::get()->file_exists(full_path))
    {
        if (samples.OpenFromFile(full_path))
        {
            loaded = true;
            std::cout << "  Loaded in: " << sound_timer.get_time_passed() / 1000.0f << " seconds\n";

        }
        else
        {
            loaded = false;
            std::cerr << "Error loading sound stream file: " << filename << "\n";
        }
    }
    else
    {
        loaded = false;
        std::cerr << "Error loading sound stream file: " << filename << "\n";
    }
}

void sound_stream::play()
{
    if (!loaded)
    {
        return;
    }

    samples.Play();
}

void sound_stream::pause()
{
    if (!loaded)
    {
        return;
    }

    samples.Pause();
}

void sound_stream::stop()
{
    if (!loaded)
    {
        return;
    }

    samples.Stop();
}

int sound_stream::get_duration()
{
    if (!loaded)
    {
        return -1;
    }

    return samples.GetDuration();
}

void sound_stream::set_loop(bool loop)
{
    if (!loaded)
    {
        return;
    }

    samples.SetLoop(loop);
}


void sound_stream::set_pitch(float pitch)
{
    if (!loaded)
    {
        return;
    }

    samples.SetPitch(pitch);
}

void sound_stream::set_volume(float volume)
{
    if (!loaded)
    {
        return;
    }

    if (volume >= 0.0f && volume <= 100.0f)
    {
        samples.SetVolume(volume);
    }
}


and I use it like this:
Code: [Select]

sound_stream test;

    test.load("resources/test.flac");

    test.play();


any idea what might be going wrong?

Best regards,
Yours3!f

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
can't play music files
« Reply #1 on: December 17, 2011, 05:30:27 pm »
Have you tried the "Sound" example?
Laurent Gomila - SFML developer

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
can't play music files
« Reply #2 on: December 17, 2011, 07:18:45 pm »
no I didn't but now I did and it works, it could play both the wav and the ogg files.

EDIT:
I took a look at the example's source, and the only difference was the while loop after you called music.play(). But shouldn't playing music create a separate thread?

anyways I changed my code to this:

Code: [Select]

sound_stream asound;

    asound.load("resources/test.flac");

    asound.play();
   
    while (asound.get_status() == sf::Music::Playing)
    {
        // Leave some CPU time for other processes
        sf::Sleep(100);

        // Display the playing position
        std::cout << "\rPlaying... ";
    }


and now it plays the song, but while it does I can't render the scene...
any ways to get around this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
can't play music files
« Reply #3 on: December 17, 2011, 08:47:20 pm »
Maybe your sound_stream was local to a function, and thus destroyed immediately after being played?
Laurent Gomila - SFML developer

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
can't play music files
« Reply #4 on: December 17, 2011, 08:57:45 pm »
yup it was local :) now it's working, thanks Laurent :)

 

anything