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;
    }
  • 相关阅读:
    【文学文娱】《屌丝逆袭》-出任CEO、迎娶白富美、走上人生巅峰
    天纵英才-阿里巴巴《马云》
    我的《大宋王朝》
    《1024 程序员节》—我喂自己袋盐
    【文学文娱】《失控》读后感
    《由河南人--首富许家印说起》
    《将博客搬至CSDN》
    【置顶】技术每天一点点--2017.09-2018.10月
    saltstack的简单搭建
    rabbitMQ基础应用
  • 原文地址:https://www.cnblogs.com/foxhengxing/p/1910826.html
Copyright © 2011-2022 走看看