Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding code to stop player when headphones disconnected. not working yet

  • Loading branch information...
commit 7c9242b8a1e49649beb5ebcb8a4320de9428410c 1 parent b986fe7
Bo Peterson bopeterson authored
11 suelta-la-voz/src/testApp.h
... ... @@ -1,3 +1,4 @@
  1 +
1 2 #pragma once
2 3 #include "ofxOpenCv.h"
3 4 #include "ofMain.h"
@@ -216,9 +217,17 @@ class testApp : public ofxiPhoneApp {
216 217
217 218 //variables
218 219 bool speaker;
219   -
  220 + bool receiver;
  221 + bool receiverOld;
220 222 bool headphonesConnected;
221 223 bool dockConnected;
  224 + bool headphonesConnectedOld;
  225 + bool dockConnectedOld;
  226 +
  227 +
  228 + const char * audioroute;
  229 +
  230 +
222 231
223 232 unsigned char * cameraPixelsNoAlpha;
224 233 unsigned char * pixelsTemp;
261 suelta-la-voz/src/testApp.mm
... ... @@ -1,91 +1,3 @@
1   -/* some snippets to try to detect headphone connection
2   - CFStringRef route;
3   - UInt32 propertySize = sizeof(CFStringRef);
4   - AudioSessionInitialize(NULL, NULL, NULL, NULL);
5   - AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
6   -
7   - if((route == NULL) || (CFStringGetLength(route) == 0)){
8   - // Silent Mode
9   - NSLog(@"AudioRoute: SILENT");
10   - } else {
11   - NSString* routeStr = (NSString*)route;
12   - NSLog(@"AudioRoute: %@", routeStr);
13   -
14   -// Known values of route:
15   -// * "Headset"
16   -// * "Headphone"
17   -// * "Speaker"
18   -// * "SpeakerAndMicrophone"
19   -// * "HeadphonesAndMicrophone"
20   -// * "HeadsetInOut"
21   -// * "ReceiverAndMicrophone"
22   -// * "Lineout"
23   -
24   -
25   -NSRange headphoneRange = [routeStr rangeOfString : @"Headphone"];
26   -NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
27   -NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];
28   -NSRange speakerRange = [routeStr rangeOfString : @"Speaker"];
29   -NSRange lineoutRange = [routeStr rangeOfString : @"Lineout"];
30   -
31   -if (headphoneRange.location != NSNotFound) {
32   - // Don't change the route if the headphone is plugged in.
33   -} else if(headsetRange.location != NSNotFound) {
34   - // Don't change the route if the headset is plugged in.
35   -} else if (receiverRange.location != NSNotFound) {
36   - // Change to play on the speaker
37   - UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
38   - AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
39   -} else if (speakerRange.location != NSNotFound) {
40   - // Don't change the route if the speaker is currently playing.
41   -} else if (lineoutRange.location != NSNotFound) {
42   - // Don't change the route if the lineout is plugged in.
43   -} else {
44   - NSLog(@"Unknown audio route.");
45   -}
46   -}
47   -*/
48   -
49   -//eller kanske detta:
50   -
51   -
52   -/*
53   -(BOOL)isHeadsetPluggedIn {
54   - UInt32 routeSize = sizeof (CFStringRef);
55   - CFStringRef route;
56   -
57   - OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
58   - &routeSize,
59   - &route);
60   -
61   -// * Known values of route:
62   -// * "Headset"
63   -// * "Headphone"
64   -// * "Speaker"
65   -// * "SpeakerAndMicrophone"
66   -// * "HeadphonesAndMicrophone"
67   -// * "HeadsetInOut"
68   -// * "ReceiverAndMicrophone"
69   -// * "Lineout"
70   -// *
71   -
72   - if (!error && (route != NULL)) {
73   -
74   - NSString* routeStr = (NSString*)route;
75   -
76   - NSRange headphoneRange = [routeStr rangeOfString : @"Head"];
77   -
78   - if (headphoneRange.location != NSNotFound) return YES;
79   -
80   - }
81   -
82   - return NO;
83   -}
84   -
85   -*/
86   -
87   -
88   -
89 1 #import <UIKit/UIKit.h>
90 2 #include "testApp.h"
91 3
@@ -107,20 +19,6 @@ int tick(int last, const char * s) {
107 19
108 20
109 21
110   -
111   -
112   -
113   -
114   -
115   -
116   -
117   -
118   -
119   - speaker=false;
120   - headphonesConnected=false; //check later;
121   - dockConnected=false; //check later
122   -
123   -
124 22 ofBackground(0,0,0);
125 23 filtercountdown=0;
126 24
@@ -154,67 +52,42 @@ int tick(int last, const char * s) {
154 52
155 53 propertySize = sizeof(CFStringRef);
156 54
157   - AudioSessionInitialize(NULL, NULL, NULL, NULL);
158   - AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
159   -
160   - if((route == NULL) || (CFStringGetLength(route) == 0)){
161   - // Silent Mode
162   - routeStr=@"silent";
163   - } else {
164   - routeStr = (NSString*)route;
165   - }
166   -
167   - tickstring = [routeStr UTF8String];
168   - cout << "AudioSessionGetProperty före ljud setup " << tickstring << endl;
169   -
170   -
171   -
172   -
173   -
174   - //this might be useful to keep music playing in locked mode
175   - //void interruptionListenerCallback(void *inClientData, UInt32 inInterruptionState) {
176   - // NSLog(@"interruptionListenerCallback");
177   - //}
178   - //this is to keep music playing in locked mode
179   - //and woyoyoy! it works!
180 55 OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);
181   - //OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, ofxiPhoneGetAppDelegate());
182   - //both OSStatus result... seem to work equally well. The second
183   - //needs interruptionListenerCallback above uncommented
184   - UInt32 category = kAudioSessionCategory_PlayAndRecord; //used to be kAudioSessionCategory_MediaPlayback; but the output disables input
185   - //and it works great with headphones, but without headphones, the earpiece instead of speaker is used for output.
186   - //does it work with headphones without mic? yes
187   - result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
188   -
189   -
190   - //following code makes speaker sound out, even if headphones are connected which is not what I want
191   -// UInt32 audioRouteOverride = 'spkr';
192   -// AudioSessionSetProperty (
193   -// 'ovrd',
194   -// sizeof (audioRouteOverride),
195   -// &audioRouteOverride
196   -// );
197   - //maybe better way of doing it:
198   - // UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
199   - //AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
200   -//exactly the sam thing but property instead of string. more elegant
201   -
202   -
  56 + UInt32 category = kAudioSessionCategory_PlayAndRecord;
  57 + result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
203 58 AudioSessionSetActive(YES);
204   -
205   - AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
206   -
207   - if((route == NULL) || (CFStringGetLength(route) == 0)){
208   - // Silent Mode
209   - routeStr=@"silent";
210   - } else {
211   - routeStr = (NSString*)route;
212   - }
213   -
214   - tickstring = [routeStr UTF8String];
215   - cout << "AudioSessionGetProperty efter ljud setup " << tickstring << endl;
216   -
217   -
  59 + speaker=false;
  60 + headphonesConnected=false;
  61 + dockConnected=false;
  62 + receiver=false;
  63 + receiverOld=false;
  64 + audioroute = new char[50];
  65 +
  66 +// AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
  67 +// if((route == NULL) || (CFStringGetLength(route) == 0)){
  68 +// // Silent Mode
  69 +// routeStr=@"silent";
  70 +// } else {
  71 +// routeStr = (NSString*)route;
  72 +// }
  73 +//
  74 +// audioroute=[routeStr UTF8String];
  75 +// //har inte testat både lineout och hörlurar. vad blir det då?
  76 +// if (strcmp(audioroute,"LineOut")==0) {
  77 +// dockConnected=true;
  78 +// } else if (strcmp(audioroute,"HeadsetInOut")==0) {
  79 +// headphonesConnected=true;
  80 +// } else if (strcmp(audioroute,"HeadphonesAndMicrophone")==0) {
  81 +// headphonesConnected=true;
  82 +// } else if (strcmp(audioroute,"ReceiverAndMicrophone")==0) {
  83 +// receiver=true;
  84 +// }
  85 +// if (receiver) {
  86 +// //redirect sound from receiver to speaker
  87 +// UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
  88 +// AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
  89 +// receiver=false;
  90 +// }
218 91
219 92 chune.loadSound("sounds/suelta-la-voz.caf");
220 93 chune.setVolume(1.0f);
@@ -310,6 +183,61 @@ int tick(int last, const char * s) {
310 183 //--------------------------------------------------------------
311 184 void testApp::update()
312 185 {
  186 +
  187 + //check headphones or lineout
  188 + //check if headphones or lineout connected
  189 + // HeadsetInOut
  190 + // ReceiverAndMicrophone
  191 + // SpeakerAndMicrophone
  192 + // LineOut
  193 + // HeadphonesAndMicrophone
  194 + AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
  195 + if((route == NULL) || (CFStringGetLength(route) == 0)){
  196 + // Silent Mode
  197 + routeStr=@"silent";
  198 + } else {
  199 + routeStr = (NSString*)route;
  200 + }
  201 +
  202 + audioroute=[routeStr UTF8String];
  203 + //har inte testat både lineout och hörlurar. vad blir det då?
  204 + if (strcmp(audioroute,"LineOut")==0) {
  205 + dockConnected=true;
  206 + } else if (strcmp(audioroute,"HeadsetInOut")==0) {
  207 + headphonesConnected=true;
  208 + } else if (strcmp(audioroute,"HeadphonesAndMicrophone")==0) {
  209 + headphonesConnected=true;
  210 + } else if (strcmp(audioroute,"ReceiverAndMicrophone")==0) {
  211 + receiver=true;
  212 + }
  213 + if (receiver) {
  214 + //redirect sound from receiver to speaker
  215 + UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
  216 + AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
  217 + receiver=false;
  218 + cout << "redirecting to speaker" << endl;
  219 + }
  220 +
  221 + if ((!dockConnected && dockConnectedOld) || (!headphonesConnected && headphonesConnectedOld)) {
  222 + //stop chune
  223 + if (chune.getIsPlaying()) {
  224 + chuneposition=chune.getPosition();
  225 + cout << "chune was stopped by removed headphones/lineout" << endl;
  226 + chune.stop();
  227 + chunepaused=true;
  228 + } else if (chuneReverse.getIsPlaying()) {
  229 + cout << "chuneReverse was stopped by removed headphones/lineout" << endl;
  230 + chuneposition=1.0-chuneReverse.getPosition();
  231 + chuneReverse.stop();
  232 + chuneReversepaused=true;
  233 + }
  234 +
  235 +
  236 + }
  237 + dockConnectedOld=dockConnected;
  238 + headphonesConnectedOld=headphonesConnected;
  239 +
  240 +
313 241 if(camera->imageUpdated){
314 242 if (filtercountdown>0) {
315 243 //this is to prevent the preview screen from sticking until filtered image is drawn
@@ -1102,17 +1030,6 @@ int tick(int last, const char * s) {
1102 1030
1103 1031 if(touch.id == 1)
1104 1032 {
1105   - //this directs sound to speaker even if headphones or dock connected
1106   - speaker=!speaker;
1107   - if (speaker) {
1108   - UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
1109   - AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
1110   - } else {
1111   - //this direct sound to receiver (earpiece) if nothing is connected and headphone/dock if connected.
1112   - //so, everything is ok if headphone or dock connected, but if nothing is connected, then sound must be (re)directe to speaker
1113   - UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
1114   - AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
1115   - }
1116 1033
1117 1034
1118 1035
BIN  ...suelta-la-voz.xcodeproj/project.xcworkspace/xcuserdata/k3bope.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown

0 comments on commit 7c9242b

Please sign in to comment.
Something went wrong with that request. Please try again.