Discussion Forums  >  Audio, Video, Playing, Recording

Replies: 24    Views: 910

AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
12/04/12 07:49 AM (11 years ago)

Background Audio stops when iPhone sleeps

On BT 2.0, I use the background audio feature, but I would like the audio track (about 6 mins long) to continue playing when the phone goes to sleep; which is usually a minute or so after starting. Is there any way to allow audio to continue to play?
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/04/12 08:04 AM (11 years ago)
Hi Have you add " Required background modes " to your info.plist file?
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 08:11 AM (11 years ago)
I'm not familiar with that! Could you explain what I need to do please?
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 08:25 AM (11 years ago)
Never done iphone but Google searched and find this. http://stackoverflow.com/questions/1816825/avoid-the-iphone-go-into-sleep-mode May or may not help
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/04/12 08:40 AM (11 years ago)
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 08:55 AM (11 years ago)
I bookmarked that Mackimack as will probably require it in future
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 09:35 AM (11 years ago)
Hi @Mackimack, I've gone through the post, cut and pasted into this post the two things that I think was needed that relate to BT 2.0 (which has no BT_Screen_Audio.m file). First, from @rossideas :- --------------------------- Sorry... right click add a row and name it UIBackgroundModes then the drop down arrows add array then click drop down arrow just left of UIBackgroundModes then you add an item 0 string = audio --------------------------- Then, from @Laurand, around line number 909 :- --------------------------- I did all the audio requirements with info.plist, and then i open BT_Config appDelgate.m i scrolled down until i found the coding. //setup audio session for background sounds. Allow iPod to continue if it's already playing. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil] I selected and removed whole this code you see above, that was all it plays the audio in background, hope so that is not a issue but my app is in review by today. --------------------------- I made the changes, ran it in my iPhone, no diference, the audio peters out when phone gooes to sleep. Did I miss something?
 
Alex@TM
Apple Fan
Profile
Posts: 956
Reg: Dec 20, 2011
London, UK
10,560
like
12/04/12 09:38 AM (11 years ago)
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 09:47 AM (11 years ago)
Hi @Alex@TM, I read your post, but I cannot find the BT_Screen_Audio.m file. I think it is no longer there in BT 2.0 , so I don't know where that code would go...
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 10:10 AM (11 years ago)
Interesting, the mods I made on @Mackimack's link had no effect on the playing of background audio in the custom/html plugin, but it *did* have an effect on html player. The html 5 player doesn't stop when I leave the page, which is a different problem, is this a clue to somebody?
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 10:16 AM (11 years ago)
@AlanMac I ran into the problem of the audio not stopping on screen exit with html player and found the solution in this post. http://www.buzztouch.com/forum/thread.php?tid=F560C8D4CA4A25A965FEA4A&currentPage=1 I guess it would just be a case of finding an ios equivalent. Hope this helps you in some way.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 10:21 AM (11 years ago)
Cheers @Mr stuck, it's another piece of the puzzle, I just don't know where it fits! PS - love the alias
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 10:26 AM (11 years ago)
Sure someone can tell how to easily adapt it for ios use. Its as easy as it looks for eclipse though.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/04/12 10:27 AM (11 years ago)
@AlanMac I found new stuff. Do you use Ios6? Look here: http://stackoverflow.com/questions/11616001/uiwebview-html5-audio-pauses-in-ios-6-when-app-enters-background/12414719 You have to add this to your custom html plugin.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 10:35 AM (11 years ago)
I don't use IOS 6, but if i ever get this app released, I will need it, so thanks @MackiMack. However, I am useless on Objective C, where does it go exactly please?
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/04/12 10:39 AM (11 years ago)
If look at you custom/html plugin go to .m file and I thing you can put this code where ever you want. if you get warnings try to put in ViewDidLoad or WillAppear. AVAudioSession *audioSession = [AVAudioSession sharedInstance]; BOOL ok; NSError *setCategoryError = nil; ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; if (!ok) { NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError); }
 
Alex@TM
Apple Fan
Profile
Posts: 956
Reg: Dec 20, 2011
London, UK
10,560
like
12/04/12 10:45 AM (11 years ago)
Sorry, not tried on v2 but will take a look when I return home from 2 hours at the skatepark with my son :/ It's a cold one tonight!
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 10:53 AM (11 years ago)
Yep it sure is cold. Its snowing where i am.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 11:24 AM (11 years ago)
Code added @Mackimack, didn't make anything worse on my 3GS with 5.1. I noticed the html5 plugin keeps on playing now even when I leave the app, so it isn't right yet, but I appreciate everybody's help here, it seems that playing audio ought to be a lot simpler than it seems to be. Wrap up warm guys!
 
Mr stuck
Android Fan
Profile
Posts: 974
Reg: Apr 09, 2012
Fife, Scotland
15,740
like
12/04/12 12:45 PM (11 years ago)
The solution i posted for android should be able to be applied to ios somehow. It just tells the webview to load a blank url which kills the sound on screen exit otherwise it will play to the end. There must a similar way to achieve the same with ios. Might be worth posting again asking how to do it. It works great on android.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/04/12 01:20 PM (11 years ago)
Cheers @Mr stuck, I agree that tip has possibilities, but I am really bad at Objective C. I'll have a look around and see if I can find anything on stackoverflow while watching Arsenal on TV.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/11/12 03:32 AM (11 years ago)
I have finally worked out how to allow audio to continue playing in the background when the phone goes into sleep mode. Hooray! Step 1 ------- In directory BT_Config open BT_info.plist Position your mouse below the key entries, right click and Add Row Name the row UIBackgroundModes, of Type Array Add a Row - it will be called item 0 Give the row a Value called audio Step 2 ------- In directory BT_Config open the file ending with appDelgate.m Search for //setup audio session for background sounds. (line 952 or thereabouts) Add code below the following lines :- //setup audio session for background sounds. Allow iPod to continue if it's already playing. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; So that the finished changes look like this :- //setup audio session for background sounds. Allow iPod to continue if it's already playing. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; BOOL ok; NSError *setCategoryError = nil; ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; if (!ok) {NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError); } Thanks and credit to @mackimackfor the smarts that made this work. Tested on an iPhone 3GS and an iPod 3, both running IOS 5.1.1
 
MDG2
Apple Fan
Profile
Posts: 102
Reg: May 08, 2012
Princeton, New ...
5,020
like
12/17/12 10:05 AM (11 years ago)
When listening to an audio file on my BT App and when the screen times out the audio stops. Is there anyway to prevent this from happening so that the audio continues to play when the screen times out? BTW, I'm using the custom url to the audio file so it opens in Quicktime. This only happens on a iPhone 5 running IOS 6.0.1. Tested on a 2nd Generation iPod Touch running IOS 4.2.1 and it works fine and continues to play when the screen times out. Anybody know how to fix this issue for IOS 6.0.1?
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
12/17/12 12:20 PM (11 years ago)
hi @MDG, Have you tried the solution I posted here?
 
MDG2
Apple Fan
Profile
Posts: 102
Reg: May 08, 2012
Princeton, New ...
5,020
like
12/17/12 12:22 PM (11 years ago)
No, but I will give it a try. I will keep you posted.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.