iPhone background audio without remote - ios

I've been searching how to start an audio file after a couple of seconds when the app enters background, and it's working, but there's a small issue I'd love to get rid of.
Without the following code, the AVAudioPlayer won't begin playing:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
But with this code, notification center starts acting like a remote.
I've seen (alarm) apps play a sound after some time, without notification center acting like a remote.
Any suggestions?
You can find the example code here:
https://github.com/SabatinoMasala/iPhone-background-audio

If you are not playing audio when entering in background mode, your app will be suspended.
Maybe adding [[UIApplication sharedApplication] beginReceivingRemoteControlEvents] will prevent your app to be suspended but I am not sure.
Have to tried to create a backgroundTask and play this audio file after a delay?
Also if you want to stay playing in background you have to loop a sound containing silence.

Related

How to play longer music as Notification alert?

I want to set a music which is longer than 30sec as notification soound. But after searching about this I came to know that it is not possible to add these type of music as notification sound. notification sound must be in 30sec longer otherwise it will not play music. Now how can I add more than 30sec longer music as notification sound?
I had a similar issue for my alarm app as well. What I did was a workaround when your app is in background. You play a music file in your code in background and increase the volume of the device to maximum. You can play as much longer music file as you want. Stop playing the music when someone clicks on the notification.
The only problem with this is that your app should not be killed or terminated.

Start Playing sound in background IOS 7

i found some useful tips here but to be sure if my requirements are possible to implement here is a short list.
i have an app which runs in background most of the time. the app "lives" for up to some hours and on a certain point i need to play an alarm sound.
so what i have found is that i need to start a AVQueuePlayer and add a mutesound which is playing constantly until i need to play my other sound. is that right?
so i create a singleton class (SoundService) where i initialize the silentsound and the alarmsound in the queue player.
when the app is going to the background i start the player and play the silent sound. if i then need to play my alarm sound while the app is in the background it should work, am i right?
right now my implementation does not work, but am i on the right way?
thanks
You cannot initiate audio in the background. The only thing the audio background mode allows you to do is to continue producing sound as the app goes from the foreground to the background.
However, if your app is capable of receiving remote events, and if it has produced sound so that it is the remote event target, then, with audio background mode, it can go on being the remote event target and thus can produce sound in the background, as long as no other app becomes the remote event target in the meantime.
The most reliable way to produce a sound while in the background is by attaching the sound to a local notification.
notification.soundName = UILocalNotificationDefaultSoundName;
notification.soundName = #"mySound.caf";
Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4).
You can convert from wav and mp3 using:
afconvert -f caff -d LEI16#44100 -c 1 original.wav mySound.caf

Sometimes Play icon on status bar disappear while using AVQueueplayer

I want to show the play icon on status bar in iOS while app is playing audio. I use AVQueuePlayer to play audio items. All works well but problem I am facing is play icon doesn't show on status bar sometimes.
The app has to support play/pause. When user taps on pause, I pause the queue player and tapping on play button will start playing the queue again. When user taps on play again, I do seeking of the player item so that it will be in sync with what it is supposed to be played at that time. But some times after tapping on play again, the queue seeks to proper time and plays but play icon doesn't show up in the status bar.
I tried by commenting out the seek code and just did play/pause. The play icon shows/hides correctly, but I am not sure what is happening with seekTime API. If I use this to seek, sometimes the icon doesn't show time.
Is this something issue happening in seekTime: API? I am using the following code for seek
[self.player.currentItem seekToTime:seekTime];
Please help me on this issue.
Thanks
I don't use AVQueuePlayer but I had a similar problem. I'm using AVAudioPlayer only and when the application starts I play an audio which is then repeated constantly. For some reason, at different times, the play icon disappears but the audio is still playing.
My solution was to apply the response of "One Man Crew" in How to display playing indication icon on status bar?
But, in addition, every time I play again, I use the following:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
I don't know if it is correct, I am new in ios development, but currently is working to me.
Please let me know if there is a better solution.
Diego.

background options and air play

How can I get my video to play via air play when the device falls asleep? I'm finding some things to do like this but it sounds like most of these things will get me banned from the app store. I just want my app's video to play on airplay without the user have to make sure their device stays awake. What should I do?
If I understand correctly, you want to prevent the device from going to sleep so you can play your video without interruptions. You can have your app prevent the device from "going to sleep" like this:
[UIApplication sharedApplication].idleTimerDisabled = YES;
Just remember to set it back to NO when you are done playing your video like this:
[UIApplication sharedApplication].idleTimerDisabled = NO;
I don't know how you are playing your video, but try you can probably just call those functions before you play your video, or immediately after you are done.
You can prevent the device from sleeping like this:
UIApplication* app = [UIApplication sharedApplication];
[app setIdleTimerDisabled:true];
when you're done with the video, don't forget to let it sleep again.
[app setIdleTimerDisabled:false];

How to shut down the music in background mode on IOS program?

I make a iOS program to use the AVAudioPlayer to play music on the background. But I don't know how to shut down the music after a certain time.
I try to use the UILocalNotification to stop music in the method:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification
But the way only effectly after the user recieved the Notification and click to shut down!
How can I stop the music automatically after a certain time?
If you use AVPlayer instead of AVAudioPlayer you can achieve this using addBoundaryTimeObserverForTimes:queue:usingBlock:
I have tried this with AVQueuePlayer (which is a subclass of AVPlayer) and it worked.
Try something like this:
id observer = [avPlayer addBoundaryTimeObserverForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMake(60, 1)]] queue:NULL usingBlock:^{
[avPlayer pause];
}];
And somewhere later:
[avPlayer removeTimeObserver:observer];
I would love to be proved wrong, but I believe that what you're asking to do might be impossible. My impression is that if the user actually clicks the Home button to send your app into the background, it can't function as a "sleep timer", because you can't run a timer. Apple's own apps can do this sort of thing, but they have special privileges.
For example, look at this app:
https://itunes.apple.com/us/app/music-sleep-timer/id320583424?mt=8
Look at how he says, "Remember to keep this app in the foreground!" Clearly that's because once the app is background, the timer stops working.
You might be better off advising the user to resort to the countdown timer in Apple's Clock app. It can shut down your music and put the device to sleep.

Resources