1. Multiple players in an app can play at the same time. If the music playback done before is not handled well, two songs will be played at the same time o(╯□╰)o 2. The advantage of putting it in the global view It just doesn’t require so much initialization and settings, and it will save a lot of time 3. Whether to interrupt the mode and other settings of AVAudioSessionCategoryPlayAndRecord and other values in the setCategory method cannot be explained one by one. I remember someone went through a table. Explain whether each option interrupts (including this app and other apps), whether it occupies input, whether it occupies output, etc. I can’t find it now, so I will give you some settings I have used. When recording, I use AVAudioSessionCategoryPlayAndRecord and play. Most of the time, AVAudioSessionCategoryPlayback means that even if the hardware is muted, the sound cannot be prevented. For other settings, please refer to the apple doc. There are too many changes and it is difficult to explain. Similar code:
//recorder设置
recordSession = AVAudioSession.sharedInstance()
recordSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
//player设置
self.player.volume = 1.0
self.player.delegate = self
let session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
//Player的设置有多种玩法
/* MixWithOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
static var MixWithOthers: AVAudioSessionCategoryOptions { get }
/* DuckOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
static var DuckOthers: AVAudioSessionCategoryOptions { get }
/* AllowBluetooth is only valid with AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord */
static var AllowBluetooth: AVAudioSessionCategoryOptions { get }
/* DefaultToSpeaker is only valid with AVAudioSessionCategoryPlayAndRecord */
static var DefaultToSpeaker: AVAudioSessionCategoryOptions { get }
4. Regarding cloud music, there may be some flaws in the experience. The simple method is as follows, play the streaming media first and download at the same time. Progress is actually the progress of the download. If the streaming media is stuck, get the currentTime. Play locally, and keep streaming if there is no lag. There may be a better way, just to start the discussion.
1. Multiple players in an app can play at the same time. If the music playback done before is not handled well, two songs will be played at the same time o(╯□╰)o
2. The advantage of putting it in the global view It just doesn’t require so much initialization and settings, and it will save a lot of time
3. Whether to interrupt the mode and other settings of AVAudioSessionCategoryPlayAndRecord and other values in the setCategory method cannot be explained one by one. I remember someone went through a table. Explain whether each option interrupts (including this app and other apps), whether it occupies input, whether it occupies output, etc. I can’t find it now, so I will give you some settings I have used. When recording, I use AVAudioSessionCategoryPlayAndRecord and play. Most of the time, AVAudioSessionCategoryPlayback means that even if the hardware is muted, the sound cannot be prevented. For other settings, please refer to the apple doc. There are too many changes and it is difficult to explain. Similar code:
4. Regarding cloud music, there may be some flaws in the experience. The simple method is as follows, play the streaming media first and download at the same time. Progress is actually the progress of the download. If the streaming media is stuck, get the currentTime. Play locally, and keep streaming if there is no lag. There may be a better way, just to start the discussion.
Hope it can help you