zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Dynamically allocated memory 动态分配内存【malloc】Memory leaks 内存泄漏
    const pointers
    heap是堆,stack是栈
    Java中使用Cookie
    Postman注册、登录、导出、导入
    HttpServletRequest get post 入参
    判断设置的时间是否大于当前时间
    JS回车登录
    一个普通的Ajax
    Java工具类
  • 原文地址:https://www.cnblogs.com/foxhengxing/p/1910815.html
Copyright © 2011-2022 走看看