Index: apps/app_queue.c
===================================================================
--- apps/app_queue.c	(revision 29163)
+++ apps/app_queue.c	(working copy)
@@ -367,6 +367,9 @@
 static struct ast_call_queue *queues = NULL;
 AST_MUTEX_DEFINE_STATIC(qlock);
 
+/* Prototype declaration of background_file */
+static int background_file(struct queue_ent *qe, struct ast_channel *chan, char *filename);
+
 static void set_queue_result(struct ast_channel *chan, enum queue_result res)
 {
 	int i;
@@ -1107,20 +1110,20 @@
 	ast_moh_stop(qe->chan);
 	/* Say we're next, if we are */
 	if (qe->pos == 1) {
-		res = play_file(qe->chan, qe->parent->sound_next);
-		if (res && valid_exit(qe, res))
+		res = background_file(qe, qe->chan, qe->parent->sound_next);
+		if (res)
 			goto playout;
 		else
 			goto posout;
 	} else {
-		res = play_file(qe->chan, qe->parent->sound_thereare);
-		if (res && valid_exit(qe, res))
+		res = background_file(qe, qe->chan, qe->parent->sound_thereare);
+		if (res)
 			goto playout;
 		res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, (char *) NULL); /* Needs gender */
 		if (res && valid_exit(qe, res))
 			goto playout;
-		res = play_file(qe->chan, qe->parent->sound_calls);
-		if (res && valid_exit(qe, res))
+		res = background_file(qe, qe->chan, qe->parent->sound_calls);
+		if (res)
 			goto playout;
 	}
 	/* Round hold time to nearest minute */
@@ -1141,14 +1144,14 @@
 	   supposed to be only once and we have already said it, say it */
 	if ((avgholdmins+avgholdsecs) > 0 && (qe->parent->announceholdtime) &&
 	    (!(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE) && qe->last_pos)) {
-		res = play_file(qe->chan, qe->parent->sound_holdtime);
-		if (res && valid_exit(qe, res))
+		res = background_file(qe, qe->chan, qe->parent->sound_holdtime);
+		if (res)
 			goto playout;
 
 		if (avgholdmins>0) {
 			if (avgholdmins < 2) {
-				res = play_file(qe->chan, qe->parent->sound_lessthan);
-				if (res && valid_exit(qe, res))
+				res = background_file(qe, qe->chan, qe->parent->sound_lessthan);
+				if (res)
 					goto playout;
 
 				res = ast_say_number(qe->chan, 2, AST_DIGIT_ANY, qe->chan->language, (char *)NULL);
@@ -1160,8 +1163,8 @@
 					goto playout;
 			}
 			
-			res = play_file(qe->chan, qe->parent->sound_minutes);
-			if (res && valid_exit(qe, res))
+			res = background_file(qe, qe->chan, qe->parent->sound_minutes);
+			if (res)
 				goto playout;
 		}
 		if (avgholdsecs>0) {
@@ -1169,8 +1172,8 @@
 			if (res && valid_exit(qe, res))
 				goto playout;
 
-			res = play_file(qe->chan, qe->parent->sound_seconds);
-			if (res && valid_exit(qe, res))
+			res = background_file(qe, qe->chan, qe->parent->sound_seconds);
+			if (res)
 				goto playout;
 		}
 
@@ -1180,14 +1183,14 @@
 	if (option_verbose > 2)
 		ast_verbose(VERBOSE_PREFIX_3 "Told %s in %s their queue position (which was %d)\n",
 			    qe->chan->name, qe->parent->name, qe->pos);
-	res = play_file(qe->chan, qe->parent->sound_thanks);
+	res = background_file(qe, qe->chan, qe->parent->sound_thanks);
 
  playout:
 	/* Set our last_pos indicators */
  	qe->last_pos = now;
 	qe->last_pos_said = qe->pos;
 	/* Don't restart music on hold if we're about to exit the caller from the queue */
-	if (res)
+	if (!res)
 		ast_moh_start(qe->chan, qe->moh);
 
 	return res;
@@ -1554,22 +1557,27 @@
 	res = ast_streamfile(chan, filename, chan->language);
 
 	if (!res) {
+		res = 1;
+		while (res) {
 		/* Wait for a keypress */
 		res = ast_waitstream(chan, AST_DIGIT_ANY);
-		if (res <= 0 || !valid_exit(qe, res))
+		if (res <= 0) {
 			res = 0;
-
-		/* Stop playback */
-		ast_stopstream(chan);
+			ast_stopstream(chan);
+			break;
+		} else {
+			if (valid_exit(qe, res)) {
+				ast_stopstream(chan);
+				res = 1;
+				return res;
+			}
+		}
+		
+		}
 	} else {
 		res = 0;
 	}
 	
-	/*if (res) {
-		ast_log(LOG_WARNING, "ast_streamfile failed on %s \n", chan->name);
-		res = 0;
-	}*/
-
 	return res;
 }
 
@@ -1595,7 +1603,8 @@
 	res = background_file(qe, qe->chan, qe->parent->sound_periodicannounce);
 
 	/* Resume Music on Hold */
-	ast_moh_start(qe->chan, qe->moh);
+	if (!res)
+		ast_moh_start(qe->chan, qe->moh);
 
 	/* update last_periodic_announce_time */
 	qe->last_periodic_announce_time = now;
@@ -2962,13 +2971,12 @@
 				}
 				res = -1;
 				break;
+			} else if (res || valid_exit(&qe, res)) {
+				ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%s|%d", qe.digits, qe.pos);
+				break;
 			}
 			if (!res) 
 				break;
-			if (valid_exit(&qe, res)) {
-				ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%s|%d", qe.digits, qe.pos);
-				break;
-			}
 		}
 		if (!res) {
 			int makeannouncement = 0;
@@ -3005,7 +3013,7 @@
 				if (qe.parent->periodicannouncefrequency && !ringing)
 					res = say_periodic_announcement(&qe);
 
-				if (res && valid_exit(&qe, res)) {
+				if (res || valid_exit(&qe, res)) {
 					ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
 					break;
 				}
