Playing an .WAV file with (or without) Direct X

Weegee

Go Weegee!
Sup TH, I've been recently learning how to program in WINDOWS API and i've been having fun making menus, dialog boxes, etc. Recently, I've tried to put these skills to the test and make an application that opens up a song, plays it, then when it finishes, it allows the user to open up another song. Currently, I'm having troubles getting to the "playing" part as I cannot seem to find any documentation on how to do this >_>. I'm still a noob so don't expect me to be perfect in my coding ;D

Here's my main.cpp:
Code:
#include <Windows.h>
#include "resource.h"
 
const char g_szClassName[] = "myWindowClass";
char szFileName[MAX_PATH] = "";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Creating OPEN FILE Dialog
    OPENFILENAME ofn;
 
    ZeroMemory(&ofn, sizeof(ofn));
 
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "WAVE Files (*.wav)\0*.wav\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "wav";
 
    switch(msg)
    {
    case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;
 
            hMenu = CreateMenu();
 
            hSubMenu = CreatePopupMenu();
            AppendMenu (hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
 
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_LOAD_MUSIC, "&Load");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Music");
 
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&Help");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&About");
 
            SetMenu(hwnd, hMenu);
        }
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case ID_FILE_EXIT:
                PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
            case ID_LOAD_MUSIC:
                if (GetOpenFileName(&ofn))
                {
                     //
                    // Want to play the song here
                   //
                }
                break;
        }
        case WM_LBUTTONDOWN:
            {
            }
 
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
 
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style        = 0;
    wc.lpfnWndProc  = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance    = hInstance;
    wc.hIcon        = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm        = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
 
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Happy Fun Times",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);
 
    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
The code where I open the file has comments below saying "Want to play song here". It's closer to the top. Anywyas, hopefully I provided enough information. I know this is kind of vauge but I can't find any tutorials online so my explanation of what I want to do is to be expected to be pretty vaugue :p. +Rep inbound :D!
 

Weegee

Go Weegee!
BUMP :O

Are bumps even allowed on these forums anymore xD?

Does anyone know how to play the song with the location to the file that is obtained D:?

Edit: It seems playsound is useless when playing .wav files

Or any streamed audio in that case. Any suggestions TH.net? I Figured out how to find the correct path to the file but playsound doesn't do anything with it..
 

s3rius

Linux is only free if your time is worthless.
I don't know much about the Win API to play any sound files, but this one works fine for me:

Code:
PlaySound(TEXT("filename.wav"), NULL, SND_FILENAME);
People say it might have problems playing larger wav files (1+ MB).
 

Weegee

Go Weegee!
I don't know much about the Win API to play any sound files, but this one works fine for me:

Code:
PlaySound(TEXT("filename.wav"), NULL, SND_FILENAME);
People say it might have problems playing larger wav files (1+ MB).
Thank you, the 1 mb rule wasn't being followed in my past attempts but when i used a smaller wave file it worked perfectly.

Now the question is, what are my options for playing a larger WAVE file? (+rep btw)

Edit: R.I.P. +Rep QQ
 

s3rius

Linux is only free if your time is worthless.
Well, you'd be best off using an audio library. Working with sound files yourself can be quite nasty.
A quick googling turns up http://www.portaudio.com/ but there are a lot of free audio libraries. I don't know which ones are easy to use, or best to use. Haven't done much with sounds yet.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Try obtaining DirectMusic, from Microsoft. They still have it up there for April 2007.

You need to send a message containing a byte buffer read from a WAV file. I think it sort of works with MIDI. I have a few source codes I can dig up, but they use MFC (using C++) instead of pure WinAPI in C.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    Plus I'm confident it's only one of my bosses, the other owner is pretty cool. But I don't think he's the one that has as much control over executive decisions
  • Varine Varine:
    They aren't cutting my responsibilities or interjecting in my management abilities or anything, it's just stupid shit that comes up. And they don't exactly have people lining up for my job, there's not that many people applying for the positions I hire
  • Varine Varine:
    Eh, whatever. Thanks for listening guys
  • jonas jonas:
    Sure :) Let us know how it ends
  • Varine Varine:
    All of these things will end happily, they're just stressful. And I still lack many good friends that I can go to, and the ones I can are preoccupied with similar things. Thus general chit chat, cuz for some reason TH and Ghan and Tom all actively keep it up.
  • Varine Varine:
    Just gotta keep Miss Mazie up through the week until her shock wears off and she realizes that she still has family all around her, and bossman will do whatever he's going to do and I'll respond appropriately when it happens. Thank you all for the support, I do very much appreciate everyone being here for me through the years
    +3
  • vypur85 vypur85:
    Best of luck Varine!
  • vypur85 vypur85:
    I just gotten myself an offer to work in China. The pay quadruples my current one. Damn.... Not really ready to start a new life there in China.
  • The Helper The Helper:
    I have heard that they pay pretty good to English teachers in China - you would be an expat
  • jonas jonas:
    Cool, what kind of job?
  • Accname Accname:
    I would be careful with jobs in China. They can be hit and miss depending on where in China you go. Places like hong kong / Shengzen / Beijing can be neat. Other places not so much.
  • Accname Accname:
    I would recommend searching for some first person experiences for the city you got the offer in. Especially now when the political situation in China is deteriorating.
  • jonas jonas:
    Accname, long time no see
  • jonas jonas:
    What have you been up to
  • tom_mai78101 tom_mai78101:
    Hey Accname, welcome back.
  • Accname Accname:
    Not much. Working in the Renewable Energy Sector as an IT Consultant. Its okay, but I think I preferred working at the university. It was more relaxed and you met all kinds of crazy people there.
  • vypur85 vypur85:
    I gotten a teaching position for Biology in a college in Wuhan (yes, there)... I suppose it should be fine there (I hope). Many of my ex colleagues are teaching in China as well currently (none in Wuhan though)
  • vypur85 vypur85:
    And I signed the contract already. I guess there's no turning back....
  • jonas jonas:
    @Accname how many hours do you work? I heard in some sectors IT consultants rack up insane hours
  • jonas jonas:
    @vypur85 sounds nice, have fun : )
  • Accname Accname:
    I am supposed to work 40 hrs a week, but I can work more if I like and I will be paid for those hours (as long as I don't go too far, there are laws and company policies, etc)
  • Accname Accname:
    In practice its basically work as much as you like, as long as the job gets done in time.
  • jonas jonas:
    Haha, my job is like that as well... that usually means I have a few 70-80 hours weeks a year, and lots of 20 hours weeks...
  • jonas jonas:
    a few weeks ago, one of my friends basically said "jonas, I received an invitation to submit something to conference X but I'm too lazy to do it and also the conference isn't advanced enough for my high level of research*, why don't you write something? Oh by the way, the deadline is in two weeks. Enjoy!" so I got two 80 hour weeks out of that kind offer. (*of course he didn't say those parts, but it's a better story this way)
  • jonas jonas:
    now I'll have next week off to make up for overtime :p and I'll play some good old gothic 2

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome
    Top