SDL_mixer 播放mp3

#include <SDL.h>
#include <SDL_mixer.h>
//
//Code irrelevant to the situation
//
//Code irrelevant to the situation
//

void musicFinished() {
	int musicPlaying = 0;
}

int main(int argc, char *argv[]) {
	// Initialize SDL's subsystems
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) < 0)
	{
		fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}

	int audio_rate = 44100;
	Uint16 audio_format = AUDIO_S16SYS;
	int audio_channel = 1;
	int audio_buffer = 2048;

	if (Mix_OpenAudio(audio_rate, audio_format, audio_channel, audio_buffer) != 0) {
		fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError());
		exit(1);
	}


	Mix_Music *music;
	music = Mix_LoadMUS("test3.mp3");
	if (music == NULL) {
		// This is where the error occurs.
		fprintf(stderr, "Unable to load mp3 file: %s\n", Mix_GetError());
		exit(1);
	}

	if (Mix_PlayMusic(music, 0) == -1)
	{
		fprintf(stderr, "Unable to play mp3 file: %s\n", Mix_GetError());
		exit(1);
	}

	int musicPlaying = 1;
	Mix_HookMusicFinished(musicFinished);

	while (musicPlaying) {
		// do nothing
		SDL_Delay(2500);
	}
	musicFinished();
	Mix_HaltMusic();
	Mix_FreeMusic(music);
	Mix_CloseAudio();

	atexit(SDL_Quit);

	return 0;
}
posted @ 2010-12-19 22:01  网络小虫  阅读(1356)  评论(0编辑  收藏  举报