I hope that someone will help this little guide to writing an iPhone-client for Internet radio. Recently, I needed to write this. In self-educational purposes. I will try to cover the topic as widely as possible in the future, but now I would like to focus on a specific point that caused me difficulties no further than today, namely, playing radio in the background.
For this we need a couple of simple manipulations.
First, you need to tell our application that it should play music even after clicking on “HOME”
This is done in the project Info.plist file:
Select a file in the Project Navigator

Click the right mouse button on the free space and select "Add row"

')
In the “Key” field, enter “Required background modes”,

this will create an array for us, we will add “App plays audio” as the value for the first element.

Everything, our program knows that in the background it should not fall asleep completely, but should continue to play music.
Another quick way to do the same is to open Info.plist with a regular editor (in fact, it’s just an XML file) and add
<key>UIBackgroundModes</key> <array> <string>audio</string> </array>
Now we initialize our player.
In general, the iOS SDK has several different classes for working with sound. I first used the
AVQueuePlayer , then decided to give preference to the
AVPlayer as the most, in my opinion, flexible.
In any case,
AVFoundation to help you, to work with audio.
- (void)viewDidLoad { [super viewDidLoad];
And now we create session:
That's basically all, the player can and will work in the background. You can minimize the program by clicking on "Home" and the music will not stop.
And the most important point: the playback of background audio for some reason does not work in the simulator, so it is necessary to run and test on the device.