1

I'm using mciSendString("play path repeat",0,0,0) to play music in my project and I'm using it specially for playing multiple sounds in the same time.

The problem is that I want to put the sounds in the executable path so I used a function to get the exe path

string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

but mciSendString() takes LPCSTR so I tried the following

string music_cmd="play "+ExePath()+"\\war1.mp3 repeat";
mciSendString(music_cmd.c_str(),0,0,0);

The program runs without Errors but it doesn't play the sound. How can I fix this problem ?

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Ali Essam
  • 502
  • 1
  • 7
  • 17
  • Have you tried to use debugger? – DuXeN0N Dec 19 '12 at 10:15
  • You may look at [this example](http://www.codeproject.com/Articles/17279/Using-mciSendString-to-play-media-files). Also you possibly should use `mciSendcommand` instead of `mciSendString` and use [the following example from Microsoft](http://msdn.microsoft.com/ru-ru/library/windows/desktop/dd743675(v=vs.85).aspx). – Stan Dec 19 '12 at 10:15
  • @DuXeN0N yes,and the string holds the exact path of the mp3 file – Ali Essam Dec 19 '12 at 10:33
  • @Stan The first example is in c# and the second one also uses LPSTR,the only problem i'm facing is that i can't merge the ExePath with the file name in a LPCSTR correctly,but it works fine if i specified the file name directly ,for ex mciSendString("play c:\\war1.mp3",0,0,0); – Ali Essam Dec 19 '12 at 10:40
  • @AliEssam It doesn't matter which language to use for the same API. The main point is that you need to `open` file first and use quotes around its "path/name", and second you can `play` the alias mentioned in the `open` string. Microsoft's example does actually the same. Also it is not much differentce between `LPCSTR` or `LPSTR`, it's just a const modifier behind this. – Stan Dec 19 '12 at 11:09
  • @Stan the problem was in converting from string (returned from the function) to LPCSTR,and it works fine without invloving strings. – Ali Essam Dec 19 '12 at 13:04
  • I figured out that i can use, mciSendString("play \\war1.mp3",0,0,0); ,this plays the sound directly from the Exe path – Ali Essam Dec 19 '12 at 13:12
  • @AliEssam LPCSTR is nothing more than `const char *`, you can get it from `string` by `c_str()`. Unfortunately you disregard official and unoffical instructions. – Stan Dec 19 '12 at 13:31
  • @AliEssam I think the function returns the file name + a backslash. Try: `return string( buffer ).substr( 0, pos-1);` – Mehmed Aug 15 '15 at 14:16

1 Answers1

2

the path should have no spaces, if you use a path like as follows: C:\music\music 2.mp3 it will not work. to make it work delete the space or create a new path without spaces like as follows: C:\music\music2.mp3

Other Observations: the path should have less than 255 characters, relative path will not work (it works when you compile, but when you run the program on another computer, it will not work), cannot have spaces, otherwise it will fail.

there is a workaround you can do which is simple, you will be able to play with dots and spaces on the path

mine is as follows:

path = Application.StartuPath & `\whateverMusic.mp3`
path = Chr(34) & path & Chr(34)
mciSendString("Open " & path & " alias " & oName, Nothing, 0, 0)
mciSendString("Play " & oName, Nothing, 0, 0)

Public Property Name As String
    Set(value as String)
         oName = value
    End Set
    Get
         Return oName
    End Get
End Property

From Here: https://www.youtube.com/watch?v=UWLTegpOuB0

rafael
  • 17
  • 4