【问题标题】:How to perform a function when a sound finish playing in swift当声音快速播放完毕时如何执行功能
【发布时间】:2018-12-06 22:34:38
【问题描述】:

我正在构建一个声音测验应用程序,要求用户从多个声音选项中猜出正确的声音。我想知道是否有任何功能可以在声音播放完毕时执行操作,例如 AVAudioPlayer .isplaying 或 .stop。

【问题讨论】:

    标签: ios swift avaudioplayer


    【解决方案1】:

    我想这就是你要找的东西

    struct Manager
    {    
        //Required Objects - AVFoundation
        ///AVAudio Session
        static var recordingSession: AVAudioSession!
    
        ///AVAudio Recorder
        static var recorder: AVAudioRecorder?
    
        ///AVAudio Player
        static var player: AVAudioPlayer?
    }
    

    播放音乐

    //Set player with audio File
    do
    {
        try Manager.player = AVAudioPlayer.init(contentsOf: returnPathAtSelectedIndex(fileName: fileName))
        //Set required delegates and Values
    
        Manager.player?.delegate = self
        Manager.player?.volume = 1.0
        Manager.player?.prepareToPlay()
        Manager.player?.play()
    }
    catch
    {
        print("Error while playing music: \(error.localizedDescription)")
    }
    

    音频播放器代表

    //MARK:- Audio Player Delegates
    extension RecordingManager: AVAudioPlayerDelegate
    {
        //MARK: Audio Player Finishes Playing audio
        /**
         Called when a sound has finished playing.
         - parameter player: player instance
         - parameter flag: Bool player is running or not successfully
         */
        func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
        {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "StoppedPlaying"), object: nil)
            player.stop()
            Manager.player?.stop()
            Manager.recordingalreadyPlayedStatus = false
            print("Finish Playing")
        }
    
        //MARK: Audio Player error occur while Playing
        /**
         Called when an audio player encounters a decoding error during playback.
         - parameter player: player instance
         - parameter error: Error if occurs
         */
        func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer,error: Error?)
        {
            print("Encoding Error: \(String(describing: error?.localizedDescription))")
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      创建您的AVAudioPlayer

      将自己设置为玩家的代表。

      实现audioPlayerDidFinishPlaying(_:successfully:) 委托方法。声音播放完毕后会调用它

      有关更多信息,请参阅有关 AVAudioPlayerAVAudioPlayerDelegate 协议和 audioPlayerDidFinishPlaying(_:successfully:) 委托方法的文档。

      【讨论】:

      • 你能给我看一下 audioPlayerDidFinishPlaying(_:successfully:) 和 avaudioelegate 代码的屏幕截图,以便我了解如何操作
      • 我没有任何代码可以截图给你。我得写一些,就像你一样。我告诉过你需要遵循的步骤。阅读文档,然后按照步骤操作。如果您遇到困难或无法使其正常工作,请使用您的代码编辑您的问题,我们将帮助您调试它,但您需要自己编写一些原始代码,而不是在互联网上获取大量示例代码。如果您不迈出这一步,您将永远无法成为一名开发人员。
      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多