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

Author Topic: Unable to play music  (Read 2708 times)

0 Members and 1 Guest are viewing this topic.

rtm5

  • Newbie
  • *
  • Posts: 4
    • View Profile
Unable to play music
« on: October 07, 2015, 03:36:38 pm »
Hi all,

I'm struggling with trying to play music from my console, and I am not sure what the problem is. This is the current code:

#include <iostream>
#include <string>
#include <SFML/Audio/Music.hpp>

int main(int argc, char* argv[])
{
  sf::Music music;

  if (!music.openFromFile("recit24bit.flac"))
  {
    std::cout << "Error" << std::endl;
    return -1;
  }

  music.play();
 
  return 0;
}

I compile the file like so: g++ main.cpp -o music-app -lsfml-audio -lsfml-system

The flac file plays on my media player, so I'm not sure what the problem is, and it's located in the same directory as the executable.

I'm using g++ 4.8.4, ubuntu 14.04 LTS, and I downloaded the SMFL package through the debian package handler.

Thanks

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Unable to play music
« Reply #1 on: October 07, 2015, 03:40:52 pm »
music.play() is a non blocking call. This means your program will fall through and exit before the music is finished playing (or starts for that matter). Try to sleep your main thread (or just do a busy wait) until the music is finished playing.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

rtm5

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Unable to play music
« Reply #2 on: October 07, 2015, 05:49:54 pm »
Thanks - Can you point me to any resources that can teach me this specific technique? I'm doing a bit of research on my end, and I am noticing that some use a while loop afterwards to keep the music playing (not sure if this is technically correct).

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Unable to play music
« Reply #3 on: October 07, 2015, 06:06:55 pm »
Usually you'll have everything happening inside your game loop anyway, so there's usually no need to do anything special to keep the music/sound playing. Except keeping the objects alive of course.