search

Home  >  Q&A  >  body text

objective-c - iOS AVAudioPlayer播放 AVAudioRecorder录音问题

如何保证只有1个Player在播放或者?

目前是,对于1个app内,很多个AVAudioPlayer Play.或者,AVAudioRecorder record .其他可以被打断?不需要我自己弄个全局变量保存正在播放的player.和正在录音的 recorder.
众所周知,我们的app播放可以打断别人的app的声音。

个人感觉打断也分为,直接中断和减弱别人的声音?不知道有没有

题外话,AVAudioPlayer的网络链接直接播放缓存问题

AVAudioPlayer 可以直接通过网址播放,但是这样看不到下载的进度,和最后要不要保留已经下好的。如果重新播放,都将进行重新下载。如网易云音乐就做的很好。

黄舟黄舟2772 days ago1037

reply all(1)I'll reply

  • 阿神

    阿神2017-04-17 17:15:21

    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.

    Hope it can help you

    reply
    0
  • Cancelreply