Like Tree1Likes
  • 1 Post By Elkvis

Playing a Short Music File Problem

This is a discussion on Playing a Short Music File Problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to write a code that plays out a MIDI file in C++, which my program can do. ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    14

    Playing a Short Music File Problem

    Hello,

    I'm trying to write a code that plays out a MIDI file in C++, which my program can do. However, I want the user to be able to play a chosen file by typing in the address. When I type the variable name into the place where the file address would go, I get an error saying that the variable type is incorrect, or that it expected a '('.

    I'm using Visual Studio 2010 Express.

    I'm trying to use either mciSendString or midiOutOpen, but I can't seem to get either to actually work.

    My (relevant part of the) code:

    Code:
    #pragma comment( lib, "winmm" )
    
    #include <iostream>
    #include <Windows.h>
    #include <MMSystem.h>
    #include <string>
    #include <conio.h>
    
    
    using namespace std;
    
    //void Play(wchar_t* MIDIFile);
    
    int Sound(wchar_t* MIDIFile, int argc, char** argv){
    
        int playing;          
        int onoff = 0;
        int MIDINote;      
        int flag;          
        HMIDIOUT device;    
        
    
    /*This part works*/
        wchar_t* MIDIopen=L"open C:\\Users\\User\\Downloads\\EspanjaPrelude.mid type mpegvideo alias MediaFile";
        wchar_t* MIDIplay=L"play MediaFile";
        mciSendString(MIDIopen, NULL, 0, NULL);
        mciSendString(MIDIplay,NULL, 0, NULL); 
    
        //mciSendString(L"play C:\\Users\\User\\Downloads\\EspanjaPrelude.mid",NULL,0,NULL);
    
    /*This does not*/
        string path = "play C:\\Users\\User\\Downloads\\EspanjaPrelude.mid";
    
        //midiOutOpen(&device, C:\Users\User\Downloads\EspanjaPrelude.mid,0,0,CALLBACK_NULL);
    
        //const char *c=path.c_str();
        //mciSendString(c,NULL,0,NULL);
    
    midiOutOpen(&device, MIDINote, 0, 0, CALLBACK_NULL);
    What I would like to do is something like:

    wchar_t* str1=L"open " + MIDIFile + " type mpegvideo alias MediaFile";

    or

    midiOutOpen(&device, MIDIFile, 0, 0, CALLBACK_NULL);

    so that the user can choose the file path, but these have not been working. Is there some code that I am missing to make this possible? Or do I have to change some other variables?

    midiOutOpen and mciSendString:
    midiOutOpen

    &

    mciSendString function (Windows)
    Last edited by aileentas; 12-14-2012 at 07:05 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Try it with a wide string.
    wstring (Standard C++ Library)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    I just tested that but I got an error saying:
    no suitable conversion function from wstring to wchar_t exists

    tried using it differently:
    no suitable conversion function from wstring to LPCWSTR exists
    Last edited by aileentas; 12-14-2012 at 07:15 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,302
    std::string and std::wstring have a .c_str() function that converts to a const char* or const wchar_t*.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Okay.

    I've written it like this:

    string s1 ("open " + MIDIFile + " type mpegvideo alias MediaFile"));
    const wchar_t* s2 (s1.c_str());

    But now it says that type const char cannot be used to initialize type const wchar_t*

    Is there a way to 'add' the bits together inside the mciSendString? I googled something like that earlier, but it wasn't for C++, but I was wondering if there was a C++ version for doing so.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,302
    s1 should be a wstring.
    aileentas likes this.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Oh, that works for changing it so that s2 can be converted to a wchar_t*, but then MIDIFile can't be added to the string.

    edit: must have enum or integral type

    Edit: I accidentally added an extra * where I should not have, it worked after all!!!

    Thank you so much!!!
    Last edited by aileentas; 12-14-2012 at 09:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help in Playing Music..!!
    By Rehman khan in forum Windows Programming
    Replies: 5
    Last Post: 10-15-2012, 03:29 AM
  2. Playing music
    By adr in forum Windows Programming
    Replies: 13
    Last Post: 12-31-2005, 02:23 PM
  3. Playing music
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 06-25-2003, 09:09 AM
  4. Playing Music in C#
    By Smoose777 in forum C# Programming
    Replies: 9
    Last Post: 06-06-2002, 04:11 PM
  5. Playing Music
    By Quantrizi in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2002, 07:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21