playing a .wav file

删除回忆录丶 提交于 2019-12-10 22:22:23

问题


I'm using visual studio 2010 express and I'm trying to write a simple program that will repeat a wave file 5 times(I'm running a windows xp sp3).

This is as far as I got:

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;   
int main ()
{
    int a = 5;
        while(a >= 1){
            cout << "It's a wav file";
            PlaySound(L"SomeAudioFile-01.wav", NULL, SND_FILENAME);
            --a;
        }
    return 0;
 }

The problem is I keep getting this error message when I'm building it:

1>------ Build started: Project: It's a F**king Country, Configuration: Release Win32 --    ----
1>  mycode.cpp
1>..\..\..\..\..\Shaul's documents\Visual Studio 2010\Projects\MyProject\Release\SomeAudioFile-01.wav : fatal error LNK1136: invalid or corrupt file
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The file is perfectly fine and plays with no problems whatsoever outside visual studio.

Help would be truly appreciated.


回答1:


Instead of adding the WAV file to the project files, add it to the resources and use SND_RESOURCE instead of SND_FILENAME.




回答2:


You include the sound file as a object file, so the compiler tries to link with it. But it's a binary file that's not linkable (which is what the error message says).

Don't include the sound file in the project, so the environment won't link with it.

P.S. In the future, please refrain from using "bad" words on a public site like this.




回答3:


To get rid of the linker error, you need to tell the IDE to link with the winmm.lib library also, so open Project/Properties/Configuration Properties/Linker/Input and append winmm.lib in the Additional Dependencies field.

Also, use the following function profile:

PlaySound(L"audio.wav", NULL, SND_APPLICATION);



来源:https://stackoverflow.com/questions/15384276/playing-a-wav-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!