Index: pjproject/trunk/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c	(revision 3438)
+++ pjproject/trunk/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c	(revision 3454)
@@ -37,4 +37,16 @@
 
     #define AudioDeviceID unsigned
+
+    /**
+     * As in iOS SDK 4 or later, audio route change property listener is
+     * no longer necessary. Just make surethat your application can receive
+     * remote control events by adding the code:
+     *     [[UIApplication sharedApplication] 
+     *      beginReceivingRemoteControlEvents];
+     * Otherwise audio route change (such as headset plug/unplug) will not be
+     * processed while your application is in the background mode.
+     */
+    #define USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER 0
+
 #endif
 
@@ -224,5 +236,4 @@
     unsigned i;
     OSStatus ostatus;
-    UInt32 audioCategory;
 #endif
 
@@ -297,16 +308,6 @@
     }
 
-    /* We want to be able to open playback and recording streams */
-    audioCategory = kAudioSessionCategory_PlayAndRecord;
-    ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
-				      sizeof(audioCategory),
-				      &audioCategory);
-    if (ostatus != kAudioSessionNoError) {
-	PJ_LOG(4, (THIS_FILE,
-		   "Warning: cannot set the audio session category (%i)",
-		   ostatus));
-    }
-
-    /* Listen for audio routing change notifications */
+    /* Listen for audio routing change notifications. */
+#if USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER != 0
     ostatus = AudioSessionAddPropertyListener(
 	          kAudioSessionProperty_AudioRouteChange,
@@ -317,5 +318,6 @@
 		   "notifications (%i)", ostatus));
     }
-    
+#endif
+
     cf_instance = cf;
 #endif
@@ -337,6 +339,8 @@
 
 #if !COREAUDIO_MAC
+#if USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER != 0
     AudioSessionRemovePropertyListenerWithUserData(
         kAudioSessionProperty_AudioRouteChange, propListener, cf);
+#endif
 #endif
     
@@ -1089,13 +1093,4 @@
 	    continue;
 
-	/* This does not seem necessary anymore. Just make sure	
-	 * that your application can receive remote control
-	 * events by adding the code:
-	 *     [[UIApplication sharedApplication] 
-	 *      beginReceivingRemoteControlEvents];
-	 * Otherwise audio route change (such as headset plug/unplug)
-	 * will not be processed while your application is in the 
-	 * background mode.
-	 */
 	/*
 	status = ca_stream_stop((pjmedia_aud_stream *)it->stream);
@@ -1131,4 +1126,7 @@
 	    it->stream->interrupted == PJ_TRUE)
 	{
+	    UInt32 audioCategory;
+	    OSStatus ostatus;
+
 	    /* Make sure that your application can receive remote control
 	     * events by adding the code:
@@ -1138,4 +1136,16 @@
 	     * application is in the background mode.
 	     */
+	    /* Make sure we set the correct audio category before restarting */
+	    audioCategory = kAudioSessionCategory_PlayAndRecord;
+	    ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
+					      sizeof(audioCategory),
+					      &audioCategory);
+	    if (ostatus != kAudioSessionNoError) {
+		PJ_LOG(4, (THIS_FILE,
+			   "Warning: cannot set the audio session category (%i)",
+			   ostatus));
+	    }
+	    
+	    /* Restart the stream */
 	    status = ca_stream_start((pjmedia_aud_stream*)it->stream);
 	    if (status != PJ_SUCCESS) {
@@ -1202,5 +1212,17 @@
 {
     OSStatus ostatus;
-
+#if !COREAUDIO_MAC
+    UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
+    /* We want to be able to open playback and recording streams */
+    ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
+				      sizeof(audioCategory),
+				      &audioCategory);
+    if (ostatus != kAudioSessionNoError) {
+	PJ_LOG(4, (THIS_FILE,
+		   "Warning: cannot set the audio session category (%i)",
+		   ostatus));
+    }    
+#endif
+    
     /* Create an audio unit to interface with the device */
     ostatus = AudioComponentInstanceNew(io_comp, io_unit);
@@ -1434,4 +1456,5 @@
 	strm->audio_buf->mBuffers[0].mNumberChannels =
 		strm->streamFormat.mChannelsPerFrame;
+	
 #endif
 
