i have been looking answer long , haven't found it. i'd create app similar alarm clock.
one of it's features waking @ specified user time (nothing surprising). if @ sleep cycle app, notice wakes track sleep, has running @ background. can play song wakes until turn off (not 30 seconds, limit of length of notification sound). can turn volume of device.
if haven't seen app @ action wouldn't believe such functionality possible on iphone developers.
my current progress:
i can play sound @ time specified user if app in foreground. if sound played , user click home button sound still played (that cool) music can't start if app in background. code:
do { try avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, withoptions: avaudiosessioncategoryoptions.mixwithothers) print("avaudiosession category playback ok") { try avaudiosession.sharedinstance().setactive(true) print("avaudiosession active") } catch let error nserror { print(error.localizeddescription) } } catch let error nserror { print(error.localizeddescription) }
and use
play sound. first question is: how play sound background sleep cycle app does? , i'm sure sleep cycle doesn't use notification sound.avaudioplayer
and second question how change device volume (sleep cycle can on stack overflow there many people saying impossible).
please :)
ok, managed using tricks:
first of all, here function helps me set audio player:
func setupaudioplayerwithfile(file:nsstring, type:nsstring) -> avaudioplayer? { //1 let path = nsbundle.mainbundle().pathforresource(file string, oftype: type string) let url = nsurl.fileurlwithpath(path!) //2 var audioplayer:avaudioplayer? // 3 { try audioplayer = avaudioplayer(contentsofurl: url) } catch { nslog("player not available") } return audioplayer }
then when user press button witch "start alarm" that:
silence_audio = setupaudioplayerwithfile("silence", type:"wav"); silence_audio?.numberofloops = -1; silence_audio?.volume = 1; silence_audio?.play();
as can guess sound of nothing - empty sound. apple said:
for tasks require more execution time implement, must request specific permissions run them in background without being suspended. in ios, specific app types allowed run in background:
-apps play audible content user while in background, such music player app
an app plays or records audio continuously (even while app running in background) can register perform tasks in background. enable audio support background modes section of capabilities tab in xcode project. (you can enable support including uibackgroundmodes key audio value in app’s info.plist file.)
after app able run in background without limitations. if apple won't allow me publish start using microphone or that. without functionality isn't possible alarm clock app.
and changig volume of device simple:
let volumeview = mpvolumeview() if let view = volumeview.subviews.first as? uislider{ view.value = 0.3 }
and set view.value form 0 - 1.
hope :)
Comments
Post a Comment