Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • AVAudioPlayer implementation in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.08k
    Comment on it

    An easy to implement simple swift class working on AVAudioPlayer depicting various features like play, pause and stop.

     

    // creating single instance of class
        static let sharedInstance = MyAudioPlayer()
        
        var myAudioPlayer:AVAudioPlayer?
        
        //MARK: Player Key Functions
        
        // checking if playing is running/playing or not
        func isRunningAudio()->Bool{
            
            if let audioPlayer = myAudioPlayer{
                
                return audioPlayer.playing
            }
            return false;
        }
        
        // funtion to play audio
        func playAudio(audio:String, type:String){
            
            if(myAudioPlayer != nil){
                
                myAudioPlayer?.play()
            }
            else{
                
                do{
                    
                    try myAudioPlayer = AVAudioPlayer(contentsOfURL: NSURL(string: NSBundle.mainBundle().pathForResource(audio, ofType: type)!)!)
                    myAudioPlayer?.delegate = self
                    myAudioPlayer?.prepareToPlay()
                    myAudioPlayer?.play()
                } catch {
                    print(error)
                }
            }        
        }
        
        // will pause the current playing audio
        func pauseAudio(){
            
            myAudioPlayer?.pause()
        }
        
        // will stop the player
        func stopAudio(){
            
            if let audioPlayer = myAudioPlayer{
                
                audioPlayer.stop()
                myAudioPlayer = nil
            }
        }
        
        //MARK: Player Delegate
        // delegate method, will be called when audioplayer is finished playing audio
        func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool){
            
            if(flag){
            
                myAudioPlayer = nil
            }
        }
    
        // delegate method, will be called when audioplayer encounters any interruption(like call) while playing audio
        func audioPlayerBeginInterruption(player: AVAudioPlayer){
            
            myAudioPlayer?.pause()
        }
        
        // delegate method, will be called when interruption ends.
        func audioPlayerEndInterruption(player: AVAudioPlayer, withOptions flags: Int){
            
            myAudioPlayer?.play()
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: