/SDLTestWav
---- /build
---- /music
---- m2.wav
---- CMakeLists.txt
---- SdlPlayTest.c
SdlPlayTest.c 清單:
/*
Sound_Init();
Sound_Play("123.wav");
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#ifdef unix
#include <unistd.h>
#endif
#include "SDL.h"
#include "SDL_mixer.h"
static int audio_open = 0;
static Mix_Music *music = NULL;
static int next_track = 0;
static int initresult = -1;
void CleanUp(int exitcode)
{
if( Mix_PlayingMusic() ) {
Mix_FadeOutMusic(1500);
SDL_Delay(1500);
}
if ( music ) {
Mix_FreeMusic(music);
music = NULL;
}
if ( audio_open ) {
Mix_CloseAudio();
audio_open = 0;
}
SDL_Quit();
exit(exitcode);
}
void IntHandler(int sig)
{
switch (sig) {
case SIGINT:
next_track++;
break;
}
}
int Sound_Init()
{
const int TMP_FREQ = MIX_DEFAULT_FREQUENCY;
const Uint16 TMP_FORMAT = MIX_DEFAULT_FORMAT;
const int TMP_CHAN = 1;
const int TMP_CHUNK_SIZE = 4096;
return Mix_OpenAudio(TMP_FREQ,TMP_FORMAT,TMP_CHAN,TMP_CHUNK_SIZE);
}
int Sound_Play(const char *file_name)
{
printf("Enter %s filename = %s\n",__func__, file_name);
if((music = Mix_LoadMUS(file_name)) == NULL)
{
printf("call Mix_LoadMUS failed:%s\n",Mix_GetError());
return -1;
}
if(Mix_PlayMusic(music,-1) == -1)
{
printf("call Mix_PlayMusic failed\n");
return -1;
}
next_track = 0;
/* Play and then exit */
printf("Playing %s\n", file_name);
Mix_FadeInMusic(music,0,2000);
while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
SDL_Delay(100);
}
Mix_FreeMusic(music);
music = NULL;
/* If the user presses Ctrl-C more than once, exit. */
SDL_Delay(500);
CleanUp(0);
<span style="white-space:pre"> </span>printf("after call Mix_PlayMusic\n");
return 0;
}
int main(int argc, char *argv[])
{
/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
printf("Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}
signal(SIGINT, IntHandler);
signal(SIGTERM, CleanUp);
initresult = Sound_Init();
printf("call Sound_Init initresult = %d\n",initresult);
audio_open = 1;
Sound_Play("./music/m2.wav");
return 0;
}
cmake_minimum_required(VERSION 2.6)
# Set the project name.
project(sdlplaytest)
# Search for C header files in these directories.
include_directories(/usr/local/include/SDL)
link_directories(/usr/local/lib)
set(APP_SRC ./SdlPlayTest.c)
ADD_EXECUTABLE(sdlplaytest ${APP_SRC})
TARGET_LINK_LIBRARIES( sdlplaytest SDL SDL_mixer)
### CMakeLists.txt ends here