使用SDL播放声音

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38126105/article/details/85227616

原生的SDL的接口实在是太难用了,我们可以借助SDL提供的其他工具SDL_mixer.地址 http://www.libsdl.org/projects/SDL_mixer/

#include "SDL_image.h"
#include "SDL_mixer.h"
int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window* window = SDL_CreateWindow("hello SDL", 100, 100, 600, 800, SDL_WINDOW_SHOWN);
	SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	SDL_Event event;
	//加载声音文件,44100声音一般采样率,2双声道,2048把每段分成2048个字节
	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
	Mix_Music *sound = Mix_LoadMUS("C:/Users/xxxx/Desktop/2.wav");
	if (nullptr == sound)
	{
		printf("声音失败,%s", Mix_GetError());
	}
	Mix_PlayMusic(sound, 1);

	bool quit = false;
	while (false == quit)
	{
		while (SDL_PollEvent(&event))
		{
			if (SDL_QUIT == event.type)
			{
				quit = true;
			}
		}

		//SDL_RenderPresent(render);
		//SDL_Delay(40);
	}
	//Mix_VolumeMusic(audio_volume); //设置音量
	Mix_FreeMusic(sound); //关闭打开音频文件
	Mix_CloseAudio();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38126105/article/details/85227616
今日推荐