Play multiple Audio Files with AVPlayer

前端 未结 4 1474
深忆病人
深忆病人 2020-12-17 15:23

I\'m trying to play multiple sounds at the same time.

The approach initially I\'ve taken was to create several players , but it seems wrong one.

What\'s the

4条回答
  •  囚心锁ツ
    2020-12-17 15:40

    Basically what everyone else is saying, make sure you create an audio player for each source.

    What you also must do is KEEP A STRONG REFERENCE TO ALL THE PLAYER OBJECTS.

    If you don't do this they get released and playback stops.

    I had this issue when I was only keeping a reference to the last source I wanted to play back. This meant that I would only hear the last data source and I thought the issue was something to do with the simultaneous playback configuration - but in reality it was just the other players were being dealloc'd and thus playback would stop.

     try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
                try? AVAudioSession.sharedInstance().setActive(true)
    

    I also used the above snippet before creating and playing back my audio sources.

提交回复
热议问题