TOTALLY STUPID BUG how could I lose 3 days debugging it. SDL is working perfeclty, that was the application crashing which could not load music file.

This commit is contained in:
pelya
2010-07-01 18:48:00 +03:00
parent cacfd5760f
commit 2b2248f27f
3 changed files with 68 additions and 31 deletions

View File

@@ -24,7 +24,9 @@ using namespace std;
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#ifdef ANDROID
#include <android/log.h> #include <android/log.h>
#endif
Mixer * mixerInstance = NULL; Mixer * mixerInstance = NULL;
@@ -91,13 +93,39 @@ void Mixer::freeMixer() {
int Mixer::loadSample(string fileName, int volume) { int Mixer::loadSample(string fileName, int volume) {
if (enabled) { if (enabled) {
if (fn2snd.find(fileName) == fn2snd.end()) { if (fn2snd.find(fileName) == fn2snd.end()) {
// open the file for reading
ifstream inputFile ( fileName.c_str(), ios::in); string fn = fileName;
if (!inputFile.good()) { string fn1 = fn;
cout << "ERROR: file " << fileName << " does not exist!" << endl; __android_log_print(ANDROID_LOG_INFO, "Alien Blaster", (string( "Loading sound " ) + fn).c_str() );
exit(1);
} // Check if file exist
mixChunks.push_back(Mix_LoadWAV(fileName.c_str())); FILE * inputFile = fopen( fn1.c_str(), "rb");
if (!inputFile) {
if( fn1.size() > 4 && fn1.find(".wav") != string::npos ) {
fn1 = fn1.substr( 0, fn1.size() - 4 ) + ".ogg";
inputFile = fopen( fn1.c_str(), "rb");
}
if (!inputFile) {
cout << "ERROR: file " << fn1 << " does not exist!" << endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() );
#endif
exit(1);
}
}
fclose(inputFile);
// TODO: error-handling
Mix_Chunk *newSound = Mix_LoadWAV(fn1.c_str());
if( !newSound ) {
cout << "ERROR: file " << fn1 << " cannot be loaded!" << endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() );
#endif
exit(1);
}
mixChunks.push_back(newSound);
fn2snd[ fileName ] = mixChunks.size() - 1; fn2snd[ fileName ] = mixChunks.size() - 1;
if ( 0 <= volume && volume < 128 ) { if ( 0 <= volume && volume < 128 ) {
Mix_VolumeChunk( mixChunks[ mixChunks.size() - 1 ], volume ); Mix_VolumeChunk( mixChunks[ mixChunks.size() - 1 ], volume );
@@ -151,14 +179,39 @@ void Mixer::fadeOut(int mSecs) {
int Mixer::loadMusic( string fn ) { int Mixer::loadMusic( string fn ) {
if (enabled) { if (enabled) {
if (fn2mus.find(fn) == fn2mus.end()) { if (fn2mus.find(fn) == fn2mus.end()) {
// open the file for reading
ifstream inputFile ( fn.c_str(), ios::in); __android_log_print(ANDROID_LOG_INFO, "Alien Blaster", (string( "Loading sound " ) + fn).c_str() );
if (!inputFile.good()) {
cout << "ERROR: file " << fn << " does not exist!" << endl; string fn1 = fn;
exit(1); // Check if file exist
} FILE * inputFile = fopen( fn1.c_str(), "rb");
if (!inputFile) {
if( fn1.size() > 4 && fn1.find(".wav") != string::npos ) {
fn1 = fn1.substr( 0, fn1.size() - 4 ) + ".ogg";
inputFile = fopen( fn1.c_str(), "rb");
}
if (!inputFile) {
cout << "ERROR: file " << fn1 << " does not exist!" << endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() );
#endif
exit(1);
}
}
fclose(inputFile);
// TODO: error-handling
Mix_Music *newSound = Mix_LoadMUS(fn1.c_str());
if( !newSound ) {
cout << "ERROR: file " << fn1 << " cannot be loaded!" << endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() );
#endif
exit(1);
}
musics.push_back(Mix_LoadMUS(fn.c_str())); musics.push_back(newSound);
fn2mus[ fn ] = musics.size() - 1; fn2mus[ fn ] = musics.size() - 1;
return musics.size() - 1; return musics.size() - 1;
} }

View File

@@ -100,7 +100,6 @@ static Uint8 *ANDROIDAUD_GetAudioBuf(_THIS)
static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture) static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture)
{ {
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio: enter");
SDL_AudioSpec *audioFormat = &this->spec; SDL_AudioSpec *audioFormat = &this->spec;
int bytesPerSample; int bytesPerSample;
JNIEnv * jniEnv = NULL; JNIEnv * jniEnv = NULL;
@@ -148,7 +147,6 @@ static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture)
SDL_CalculateAudioSpec(&this->spec); SDL_CalculateAudioSpec(&this->spec);
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio: exit, audioBufferSize %d", audioBufferSize);
return(1); return(1);
} }
@@ -184,7 +182,6 @@ static jmethodID JavaFillBuffer = NULL;
static void ANDROIDAUD_ThreadInit(_THIS) static void ANDROIDAUD_ThreadInit(_THIS)
{ {
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_ThreadInit: enter");
jclass JavaAudioThreadClass = NULL; jclass JavaAudioThreadClass = NULL;
jmethodID JavaInitThread = NULL; jmethodID JavaInitThread = NULL;
jmethodID JavaGetBuffer = NULL; jmethodID JavaGetBuffer = NULL;
@@ -212,8 +209,6 @@ static void ANDROIDAUD_ThreadInit(_THIS)
__android_log_print(ANDROID_LOG_ERROR, "libSDL", "ANDROIDAUD_OpenAudio(): JNI returns a copy of byte array - no audio will be played"); __android_log_print(ANDROID_LOG_ERROR, "libSDL", "ANDROIDAUD_OpenAudio(): JNI returns a copy of byte array - no audio will be played");
SDL_memset(audioBuffer, this->spec.silence, this->spec.size); SDL_memset(audioBuffer, this->spec.silence, this->spec.size);
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_ThreadInit: exit, audioBuffer %p", audioBuffer);
}; };
static void ANDROIDAUD_ThreadDeinit(_THIS) static void ANDROIDAUD_ThreadDeinit(_THIS)
@@ -223,15 +218,12 @@ static void ANDROIDAUD_ThreadDeinit(_THIS)
static void ANDROIDAUD_PlayAudio(_THIS) static void ANDROIDAUD_PlayAudio(_THIS)
{ {
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: enter, audiobuffer %p", audioBuffer);
jboolean isCopy = JNI_TRUE; jboolean isCopy = JNI_TRUE;
(*jniEnvPlaying)->ReleaseByteArrayElements(jniEnvPlaying, audioBufferJNI, (jbyte *)audioBuffer, 0); (*jniEnvPlaying)->ReleaseByteArrayElements(jniEnvPlaying, audioBufferJNI, (jbyte *)audioBuffer, 0);
audioBuffer = NULL; audioBuffer = NULL;
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: before JavaFillBuffer");
(*jniEnvPlaying)->CallIntMethod( jniEnvPlaying, JavaAudioThread, JavaFillBuffer ); (*jniEnvPlaying)->CallIntMethod( jniEnvPlaying, JavaAudioThread, JavaFillBuffer );
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: after JavaFillBuffer");
audioBuffer = (unsigned char *) (*jniEnvPlaying)->GetByteArrayElements(jniEnvPlaying, audioBufferJNI, &isCopy); audioBuffer = (unsigned char *) (*jniEnvPlaying)->GetByteArrayElements(jniEnvPlaying, audioBufferJNI, &isCopy);
if( !audioBuffer ) if( !audioBuffer )
@@ -239,7 +231,6 @@ static void ANDROIDAUD_PlayAudio(_THIS)
if( isCopy == JNI_TRUE ) if( isCopy == JNI_TRUE )
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio() JNI returns a copy of byte array - that's slow"); __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio() JNI returns a copy of byte array - that's slow");
__android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: exit audioBuffer %p", audioBuffer);
} }
#ifndef SDL_JAVA_PACKAGE_PATH #ifndef SDL_JAVA_PACKAGE_PATH

View File

@@ -34,14 +34,7 @@ class AudioThread {
public int fillBuffer() public int fillBuffer()
{ {
Log.i("libSDL", "JNI: fillBuffer() enter, mAudioBuffer len " + String.valueOf(mAudioBuffer.length)); mAudio.write( mAudioBuffer, 0, mAudioBuffer.length );
int ret = 0;
try{
ret = mAudio.write( mAudioBuffer, 0, mAudioBuffer.length );
} catch( Throwable t ) {
Log.i("libSDL", "JNI: fillBuffer() caught exception!");
}
Log.i("libSDL", "JNI: fillBuffer() exit, written " + String.valueOf(ret));
return 1; return 1;
} }