Home > other >  PlaySound plays default windows error sound
PlaySound plays default windows error sound

Time:07-09

I'm trying to make a simple audio player in C using the Win32 API library.

How this program currently works, is you select a file via file explorer, which then the file's location is saved onto a list box. When you press the "play" button, it takes the file location from the list box, and parses it to a function that uses the parameter to play the desired file.

But for some reason, instead of playing the desired audio file. It plays the default windows error sound, even though that the file is found.

I tried this with a test sound file, that is in the projects structure. I know the program can find it, but still gives me the same issue.

If somebody could help me, that would be great. I'm more than happy to update this post with more code if you guys want me to.


Window Procedure (In oshyClient.cpp)

LRESULT CALLBACK WindProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {

    switch (msg) {

        case WM_COMMAND:
            switch (wp) {

                case MENU_EXIT:
                    PostQuitMessage(0);
                    break;

                case MENU_ADDAUDIO:
                    queue.addNewAudioToQueue(hWnd);
                    break;

                case BUTTON_PLAY:
                    char text[100];
                    SendMessage(queue.hAudioQueue, LB_GETTEXT, 0, (LPARAM)text);
                    audio.playAudio(text); // The audio issue is here.
                    break;

            }
            break;

        case WM_CREATE:

            menu.displayMenu(hWnd);
            queue.displayAudioQueue(hWnd);
            audio.displayAudioControls(hWnd);

            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProcW(hWnd, msg, wp, lp);

    }

}

oshyClientAudioPlayer.cpp

#include "oshyClientAudioPlayer.h"
#include "oshyClient.h"
#include <iostream>

void AudioPlayer::displayAudioControls(HWND hWnd) {
    CreateWindow(L"Button", L"Play", WS_VISIBLE | WS_CHILD, 5, 10, 35, 25, hWnd, (HMENU)BUTTON_PLAY, NULL, NULL);
}

void AudioPlayer::playAudio(const char* audioLocation) {
    PlaySound((LPCWSTR)audioLocation, 0, SND_FILENAME);
}

oshyClientAudioQueue.cpp

#include "oshyClientAudioQueue.h"
#include "oshyClient.h"

void AudioQueue::displayAudioQueue(HWND hWnd) {

    hAudioQueue = CreateWindowEx(WS_EX_CLIENTEDGE, L"listbox", L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL | 0, 4, 92, 474, 250, hWnd, (HMENU)ID_LISTBOX, 0, 0);

}

void AudioQueue::addNewAudioToQueue(HWND hWnd) {

    OPENFILENAMEA file;
    char fileName[100];

    ZeroMemory(&file, sizeof(OPENFILENAME));

    file.lStructSize = sizeof(OPENFILENAME);
    file.hwndOwner = hWnd;
    file.lpstrFile = fileName;
    file.lpstrFile[0] = '\0';
    file.nMaxFile = 100;
    file.lpstrFilter = "All Files\0*.*";
    file.nFilterIndex = 1;

    if (GetOpenFileNameA(&file)) {
        SendMessageA(hAudioQueue, LB_ADDSTRING, 0, (LPARAM)file.lpstrFile);
    }

}

CodePudding user response:

When calling PlaySound(), you are type-casting a char* to a wchar_t*. Don't do that. Use PlaySoundA() when passing in a char* string, eg:

void AudioPlayer::playAudio(const char* audioLocation) {
    PlaySoundA(audioLocation, 0, SND_FILENAME);
}

However, you are creating your ListBox as a Unicode window, so you should be using wchar_t strings instead of char strings, eg:

case BUTTON_PLAY:
    wchar_t text[MAX_PATH];
    SendMessageW(queue.hAudioQueue, LB_GETTEXT, 0, (LPARAM)text);
    audio.playAudio(text);
    break;
void AudioPlayer::playAudio(const wchar_t* audioLocation) {
    PlaySoundW(audioLocation, 0, SND_FILENAME);
}
void AudioQueue::addNewAudioToQueue(HWND hWnd) {

    wchar_t fileName[MAX_PATH];
    fileName[0] = L'\0';

    OPENFILENAMEW file;
    ZeroMemory(&file, sizeof(file));
    file.lStructSize = sizeof(file);
    file.hwndOwner = hWnd;
    file.lpstrFile = fileName;
    file.nMaxFile = MAX_PATH;
    file.lpstrFilter = L"All Files\0*.*\0";
    file.nFilterIndex = 1;
    file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    if (GetOpenFileNameW(&file)) {
        SendMessageW(hAudioQueue, LB_ADDSTRING, 0, (LPARAM)fileName);
    }
}
  • Related