possible fix for #141 - Sound device access

This commit is contained in:
doitux
2012-08-20 18:46:34 +02:00
parent 5c266f12a5
commit 35c0216f78
3 changed files with 18 additions and 33 deletions
+1 -1
View File
@@ -845,7 +845,7 @@ void gameTableImpl::applySettings(settingsDialogImpl* mySettingsDialog)
} }
// Re-init audio. // Re-init audio.
mySDLPlayer->audioDone(); mySDLPlayer->closeAudio();
mySDLPlayer->initAudio(); mySDLPlayer->initAudio();
} }
+15 -29
View File
@@ -32,12 +32,7 @@ SDLPlayer::SDLPlayer(ConfigFile *c)
: soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c) : soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c)
#endif #endif
{ {
#ifndef ANDROID
SDL_Init(SDL_INIT_AUDIO);
initAudio();
myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str()); myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
#endif
} }
@@ -45,13 +40,13 @@ SDLPlayer::~SDLPlayer()
{ {
#ifndef ANDROID #ifndef ANDROID
closeAudio(); closeAudio();
SDL_Quit();
#endif #endif
} }
void SDLPlayer::initAudio() void SDLPlayer::initAudio()
{ {
#ifndef ANDROID #ifndef ANDROID
SDL_Init(SDL_INIT_AUDIO);
if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
int audio_rate = 44100; int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
@@ -63,6 +58,9 @@ void SDLPlayer::initAudio()
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
audioEnabled = 1; audioEnabled = 1;
} }
else {
qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :(";
}
} }
#endif #endif
} }
@@ -70,6 +68,7 @@ void SDLPlayer::initAudio()
void SDLPlayer::playSound(string audioString, int playerID) void SDLPlayer::playSound(string audioString, int playerID)
{ {
#ifndef ANDROID #ifndef ANDROID
initAudio();
if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav"); QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav");
@@ -124,15 +123,11 @@ void SDLPlayer::playSound(string audioString, int playerID)
break; break;
} }
// audioDone();
QDataStream in(&myFile); QDataStream in(&myFile);
soundData = new Uint8[(int)myFile.size()]; soundData = new Uint8[(int)myFile.size()];
in.readRawData( (char*)soundData, (int)myFile.size() ); in.readRawData( (char*)soundData, (int)myFile.size() );
sound = Mix_QuickLoad_WAV(soundData); sound = Mix_QuickLoad_WAV(soundData);
// set channel 0 to settings volume // set channel 0 to settings volume
Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10); Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10);
@@ -143,26 +138,11 @@ void SDLPlayer::playSound(string audioString, int playerID)
} }
currentChannel = Mix_PlayChannel(-1, sound,0); currentChannel = Mix_PlayChannel(-1, sound,0);
} }
// else cout << "could not load " << audioString << ".wav" << endl;
//test
// audioDone();
// sound = Mix_LoadWAV( QString(QString::fromStdString(audioString)+QString(".wav")).toStdString().c_str() );
// currentChannel = Mix_PlayChannel(-1, sound,0);
//TESTING: release audio after every sound when there is no other sound currently played
if(SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) {
closeAudio();
} }
#endif
}
void SDLPlayer::audioDone()
{
#ifndef ANDROID
if(audioEnabled) {
Mix_HaltChannel(currentChannel);
Mix_FreeChunk(sound);
sound = NULL;
delete[] soundData;
soundData = NULL;
} }
#endif #endif
} }
@@ -171,9 +151,15 @@ void SDLPlayer::closeAudio()
{ {
#ifndef ANDROID #ifndef ANDROID
if(audioEnabled) { if(audioEnabled) {
audioDone(); Mix_HaltChannel(currentChannel);
Mix_FreeChunk(sound);
sound = NULL;
delete[] soundData;
soundData = NULL;
Mix_CloseAudio(); Mix_CloseAudio();
audioEnabled = false; audioEnabled = false;
} }
SDL_Quit();
#endif #endif
} }
-1
View File
@@ -47,7 +47,6 @@ public:
void initAudio(); void initAudio();
void playSound(std::string, int playerID); void playSound(std::string, int playerID);
void audioDone();
void closeAudio(); void closeAudio();
private: private: