Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-22-2010, 06:29 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default OpenAL when application resigns active

Hi there!

I'm using OpenAL in my app to play sounds and AVAudioPlayer to play background music. When my app resigns active (due to a call, for example), audio of course gets mute, and when the application become active audio restarts. However when this happens music sounds correctly, but no sound is heard (using OpenAL just in sounds, remember). I think this can be because iPhone's bell takes the default audio channel to play, which has been taken before by OpenAL, but it's never returned to it, so OpenAL remains whit no assigned channel after a call (however I've tried reloading and reinitiating OpenAL before a call and it didn't worked).
Any clue about this? Any trick? I don't know how to make it working again...

Thanks in advance!
Johanovski is offline   Reply With Quote
Old 01-22-2010, 01:50 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You need to stop your openAL session, and restore it when the app resumes. This should help:
restarting openAL after application interruption on the iPhone|benbritten.com
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-25-2010, 04:20 AM   #3 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

Hi smasher!

Hm, I've tried following what your link said, but it doesn't work for me... What I've done before is to completely shutdown the sound manager when the application is interrupted, and restore again when it's restored. That way music sounds well (via AudioVideoPlayback), but sound doesn't get restored... This is how I'm doing it:

- In the AppDelegate class, when the applicationWillResignActive method is called an eaglView method named "shutDownAudio" is called, and when the applicationDidBecomeActive method is called another method named "reinstallAudio" is called. Here are that methods:

Code:
// EAGLView.m methods:

- (void) shutDownAudio
{
	if (sound) {
		[sharedSoundManager stopPlayingMusic];
	}
	
	if (menu) {
		[self netejaAudioMenu];
	}else{
		[self netejaAudioInterficie];
	}
	
	[sharedSoundManager shutdownSoundManager];

	sharedSoundManager = nil;
}

- (void) reinstallAudio
{
	if (!primerCop) {
                 [sharedSoundManager SingletonSoundManager];
	}
}
Code:
// mySoundManager class:

+ (SingletonSoundManager *)sharedSoundManager {
	
	// synchronized is used to lock the object and handle multiple threads accessing this method at
	// the same time
	@synchronized(self) {
		
		// If the sharedSoundManager var is nil then we need to allocate it.
		if(sharedSoundManager == nil) {
			// Allocate and initialize an instance of this class
			NSLog(@"SSM == nil");
			[[self alloc] init];
		}else {
			NSLog(@"SSM != nil");
		}


	}
	
	// Return the sharedSoundManager
	return sharedSoundManager;
}

- (id)init {
	NSLog(@"INIT SOUND");
	if(self = [super init]) {
		soundSources = [[NSMutableArray alloc] init];
		soundLibrary = [[NSMutableDictionary alloc] init];
		sourceLibrary = [[NSMutableDictionary alloc] init];
		musicLibrary = [[NSMutableDictionary alloc] init];
		
		// Set the default volume for music
		backgroundMusicVolume = 1.0f;
		
		// Set up the OpenAL
		BOOL result = [self initOpenAL];
		if(!result)	return nil;
		return self;
	}
	//[self release];
	return nil;
}

- (BOOL) initOpenAL {
	// Get the device we are going to use for sound.  Using NULL gets the default device
	device = alcOpenDevice(NULL);
	
	// If a device has been found we then need to create a context, make it current and then
	// preload the OpenAL Sources
	if(device) {
		//NSLog(@"INIT OPENAL SOUND");
		// Use the device we have now got to create a context "air"
		context = alcCreateContext(device, NULL);
		// Make the context we have just created into the active context
		alcMakeContextCurrent(context);
		// Pre-create 32 sound sources which can be dynamically allocated to buffers (sounds)
		NSUInteger sourceID;
		for(int index = 0; index < kMaxSources; index++) {
			//NSLog(@"new source:%d",index);
			// Generate an OpenAL source
			alGenSources(1, &sourceID);
			// Add the generated sourceID to our array of sound sources
			[soundSources addObject:[NSNumber numberWithUnsignedInt:sourceID]];
		}
		
		// Return YES as we have successfully initialized OpenAL
		//NSLog(@"YES!");
		return YES;
	}
	// Something went wrong so return NO
	//NSLog(@"NO!");
	return NO;
}

- (void) shutdownSoundManager {
	@synchronized(self) {
		if(sharedSoundManager != nil) {
			[self dealloc];
		}
	}
}

- (void)dealloc {
	// Loop through the OpenAL sources and delete them
	for(NSNumber *numVal in soundSources) {
		NSUInteger sourceID = [numVal unsignedIntValue];
		alDeleteSources(1, &sourceID);
	}
	
	// Loop through the OpenAL buffers and delete 
	NSEnumerator *enumerator = [soundLibrary keyEnumerator];
	id key;
	while ((key = [enumerator nextObject])) {
		NSNumber *bufferIDVal = [soundLibrary objectForKey:key];
		NSUInteger bufferID = [bufferIDVal unsignedIntValue];
		alDeleteBuffers(1, &bufferID);		
	}
	
	// Release the arrays and dictionaries we have been using
	[soundLibrary release];
	[soundSources release];
	[sourceLibrary release];
	[musicLibrary release];
	
	// Disable and then destroy the context
	alcMakeContextCurrent(NULL);
	alcDestroyContext(context);
	
	// Close the device
	alcCloseDevice(device);
	
	sharedSoundManager = nil;
	
	[super dealloc];
}
The music is restored well, but the sound never plays again after the app has become interrupted... Any clue about this?

Thanks in advance!


I've read something about audioSessions, but I don't understand where it have to be set, and no clue on how to use the listenerCallback function, or anything about this... I'm really unable to understand how to deal with that sound problem! Anyone can help me? :S

Last edited by Johanovski; 01-26-2010 at 07:17 AM.
Johanovski is offline   Reply With Quote
Old 01-26-2010, 07:18 AM   #4 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

I've read something about audioSessions, but I don't understand where it have to be set, and no clue on how to use the listenerCallback function, or anything about this... I'm really unable to understand how to deal with that sound problem!
Anyone can help me? :S
Johanovski is offline   Reply With Quote
Old 01-26-2010, 01:51 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by Johanovski View Post
I've read something about audioSessions, but I don't understand where it have to be set, and no clue on how to use the listenerCallback function, or anything about this... I'm really unable to understand how to deal with that sound problem!
Anyone can help me? :S
Are you using the "reset audio session" code from that link? Or are you saying you never created an audio session to begin with? Apple has a cookbook entry for creating an audio session:

iPhone Dev Center: Audio Session Programming Guide: Audio Session Cookbook
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-27-2010, 03:01 AM   #6 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

Hi there!

I've tried using the AudioSession features in my own code but they do nothing... Once the app is interrupted and restored again music restarts (it's played using the AVAudioPlayer class) but sounds doesn't (they're played using OpenAL). I'm using the two lines for switching the audioSession state:

Code:
AudioSessionSetActive(FALSE); // Interruption begins
(...)
AudioSessionSetActive(TRUE); // Interruption ends
However, this doesn't work for me... I've tried to follow the code from the link you posted in a blank OpenGL project, but I'm unable to even build it (17 errors and lots of warnings, and of course I've imported OpenAL and AVFoundation framework).
Where do the audioSession be initiated? I'm using a class called singletonSoundManager for my audio (which uses OpenAL and AVAudioPlayer for sounds and music) and I don't know what I need to modify to enable audioSessions, any idea about that?

Thanks!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Tags
becomeactive, call interruption, openal, resignactive, sound

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 259
24 members and 235 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Desert Diva, Dominus, glenn_sayers, HemiMG, JasonR, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:31 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0