Three ways to implement audio playback in iOS background

Source: Internet
Author: User

Often say that android how hard, now finally turn to iOS bitter force.
Android apps play music like music on the foreground and backstage, no one bird you.
iOS plays like a background sound, because iOS apps switch to the background, apps are suspended, and non-special apps can't play sounds. Through the efforts to finally find 3 solutions.
1. Put your app in the background play program, the new Xcode tick can be, the old version of Xcode only in the *.plist file added attribute value. This Apple audit is very strict, non-background player program, you set up will not let you pass.
To play a sound test using the background:
Continue playing music when your app enters the background while it's playing music.
When your app does not play sound before it goes into the background, your app plays a sound in the background, sorry your app doesn't have any fart release.
It seems that the background play sounds just for the player, your app is OK to open this function the risk of being rejected. Amen, good danger, hurry away from it.
2. The message that the service is pushed over through APNs contains small sound files. Your app will play this sound when it's received. APNs is for the entire mobile phone, that is, your phone shutdown, as long as the application does not understand the binding and service push connection, then your mobile phone can also receive notifications.
Most of the messages pushed through APNs are default, so you get the same sound, you can change the name of the sound file you send, but it certainly doesn't support. mp3 (tested). If you send a sound file by APNs, you receive a response super-delay is not generally large, delay a few minutes is also possible, it must be a large circle of your voice transmission. Plus APNs has a leak rate, through a push loss and more delay. This is the way you can tolerate this delay.
Such as:

 For more details, please refer to Apple Push Notification Service Programming Guide. The carrier (payload) is a json  string (the longest 256  bytes), encapsulating the text you sent to IOS The information that is applied. This is an example of a payload: { "APS" : { "alert" :  "Yougot a new message!" ,  "badge" : 5 , " sound ": " beep.wav "},  "acme1" :  "bar" ,  "acme2" : 42 }  

3. Pop up local notifications and play music in local notifications.
There are three scenarios for this type of notification:
The first scenario: the client (possibly in the foreground or in the background) polls the service for an HTTP request, and when the service receives the request, it gives the correct response, and you need to play a piece of music to the user (not sure the foreground or background) to alert the user.
The second scenario: the user in the foreground operation does not complete all processing and switch to the background, resulting in an HTTP request to the server, the server received a request to give the correct response, the client (possibly in the foreground, may also be in the background) to the user (not sure is the foreground or background) to play a piece of music to remind the user.
The third scenario: the server and the client has a long socket connection, the server sends over the long connection order notification, the client receives the order notification, the user may in the backstage, like immediately wants to play a piece of music to inform the user to have the new order, requests 0 delay.

Uilocalnotification *notification=[[uilocalnotification alloc] init];
if (Notification!=nil) {
NSDate *now=[nsdate New];
Notification.firedate=[now datebyaddingtimeinterval:0];//Immediate Notification
notification.repeatinterval=0;//cycle times,
Notification.timezone=[nstimezone Defaulttimezone];
notification.applicationiconbadgenumber=0; Applied Red number
Notification.soundname= uilocalnotificationdefaultsoundname;//Sound, can be changed to Alarm.soundname = @ "MYMUSIC.CAF"
Remove the following 2 lines without prompting the popup box
[Email protected] "notification content";//message Popup Prompt box
Notification.alertaction = @ "OPEN"; Prompt Box button
Notification.hasaction = NO; If the extra button is displayed, the alertaction disappears when no

   // NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];    //notification.userInfo = infoDict; //添加额外的信息    [[UIApplication sharedApplication] scheduleLocalNotification:notification];      }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three ways to implement audio playback in iOS background

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.