Lock screen or press home to continue playing music

Source: Internet
Author: User

Refer:
[1] http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AudioandVideoTechnologies/AudioandVideoTechnologies.html#//apple_ref/doc/uid/TP40007072-CH19-SW34

[2] http://www.cocoachina.com/bbs/read.php? Tid-26984.html

[3] http://diwublog.com/archives/89

Generally, apps that listen to music can play music after the screen lock and press the Home key. Some powerful apps can even play music after sending text messages without being interrupted, such as Baidu ting. This article describes how to enable screen lock and play music after pressing the Home key.
It is implemented mainly by avaudiosession. For more information about avaudiosession, see reference 1.
It can handle
1) Whether your audio changes to silent during ring/silent Switching
2) Whether your audio stops when the screen is locked
3) when your audio starts playing, the iPod Audio continues to play or becomes silent
A larger audio context includes changes made by users, such as inserting headphones, handling Warnings such as clock and calendar, or handling incoming calls. Audio sessions allow you to properly respond to such events.
1. If you can still play music after the screen lock, you only need to include the following code in the application.

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];[[AVAudioSession sharedInstance] setActive: YES error: nil];

Avaudiosessioncategoryplayback is set, indicating that the user ignores switching the mute mode or the screen lock and continues playing the music. And do not play music from other apps. Of course, you can set kaudiosessionproperty_overridecategorymixwithothers to mix music with other apps.
In addition to avaudiosessioncategoryplayback, the following categories are available.

NSString *const AVAudioSessionCategoryAmbient;NSString *const AVAudioSessionCategorySoloAmbient;NSString *const AVAudioSessionCategoryPlayback;NSString *const AVAudioSessionCategoryRecord;NSString *const AVAudioSessionCategoryPlayAndRecord;NSString *const AVAudioSessionCategoryAudioProcessing;

Avaudiosessioncategoryambient: In the mute mode or when the screen is locked, music is no longer played, which is mixed with other app sounds.
Avaudiosessioncategorysoloambient default mode. It can be used in mute mode or when the screen is locked, and cannot be used together with other apps.
Avaudiosessioncategoryrecord does not play music. The screen lock status continues recording.
Avaudiosessioncategoryplayandrecord
2. You can still play the video after pressing the Home Key.
Plist adds required background modes and sets the content to app plays audio, as shown in.

Note that this is ineffective on the simulator, But you can work on the real machine.

In addition, when someone says home, playing a song stops and stops playing it again. The method is to use another audioplay to play a silent sound.
When you finish playing a song, the system will not drop your app suspend, so that you can switch to the next one to continue playing.
Maybe there is a better way. This is just a mark.

In fact, this is equivalent to a background. If you can hide the app icon, It is a background and a pseudo background. This is also a method when Apple does not support the background, however, it must be expensive.

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.