zoukankan      html  css  js  c++  java
  • SDL_mixer 播放wav

    环境 vs 2005

    编译时需要引入三个库  sdl.lib sdlmain.lib sdl_mixer.lib

    以下为源代码

    #include <SDL.h>
    #include <SDL_thread.h>
    
    #ifdef __MINGW32__
    #undef main /* Prevents SDL from overriding main() */
    #endif
    
    #include <stdio.h>
    #include <sdl_mixer/SDL_mixer.h>
    #undef main /* Prevents SDL from overriding main() */
    
    int main(int argc, char *argv[]) {
    	SDL_Surface *screen;			//Pointer to the main screen surface
    	Mix_Chunk *sound = NULL;		//Pointer to our sound, in memory
    	int channel;				//Channel on which our sound is played
    
    	int audio_rate = 22050;			//Frequency of audio playback
    	Uint16 audio_format = AUDIO_S16SYS; 	//Format of the audio we're playing
    	int audio_channels = 2;			//2 channels = stereo
    	int audio_buffers = 4096;		//Size of the audio buffers in memory
    
    	//Initialize BOTH SDL video and SDL audio
    	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
    		printf("Unable to initialize SDL: %s\n", SDL_GetError());
    		return 1;
    	}
    
    	//Initialize SDL_mixer with our chosen audio settings
    	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
    		printf("Unable to initialize audio: %s\n", Mix_GetError());
    		exit(1);
    	}
    
    	//Load our WAV file from disk
    	sound = Mix_LoadWAV("sound.wav");
    	if(sound == NULL) {
    		printf("Unable to load WAV file: %s\n", Mix_GetError());
    	}
    
    	//Set the video mode to anything, just need a window
    	screen = SDL_SetVideoMode(320, 240, 0, SDL_ANYFORMAT);
    	if (screen == NULL) {
    		printf("Unable to set video mode: %s\n", SDL_GetError());
    		return 1;
    	}
    
    	//Play our sound file, and capture the channel on which it is played
    	channel = Mix_PlayChannel(-1, sound, 0);
    	if(channel == -1) {
    		printf("Unable to play WAV file: %s\n", Mix_GetError());
    	}
    
    	//Wait until the sound has stopped playing
    	while(Mix_Playing(channel) != 0);
    
    	//Release the memory allocated to our sound
    	Mix_FreeChunk(sound);
    
    	//Need to make sure that SDL_mixer and SDL have a chance to clean up
    	Mix_CloseAudio();
    	SDL_Quit();	
    
    	//Return success!
    	return 0;	return 0;
    }
  • 相关阅读:
    【ES6】函数的扩展
    NSFileManger使用介绍
    委托,曾将让我头疼难以理解
    【HDOJ】1914 The Stable Marriage Problem
    MySQL修改配置优化插入性能
    MySQL配置文件的编码问题
    MyBatis批量更新时提示"You have an error in your SQL syntax"
    MyBatis批量更新返回受影响数
    log4j.properties配置说明
    删除Win10的OneDrive
  • 原文地址:https://www.cnblogs.com/foxhengxing/p/1910826.html
Copyright © 2011-2022 走看看