简体   繁体   中英

IOS Audio stops playing when screen goes off in swift

Hi am new to IOS and Swift.

I am making an app which contain two audio files which plays on the second ViewController . I use Navigation controller and push to go to the second ViewController . As I found when going back to the first ViewController , the audio will not stop, I used this code :

override func viewWillDisappear(animated: Bool) {
    audioPlaying.stop()
}

Now the audio stops when going back, But also audio will stop if screen goes off means when application is locked or in background, How can I get rid of this?

More code :

var audioPlaying = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()
    audioPlaying = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audioOne", ofType: "mp3")!), error: nil)
}

override func viewWillDisappear(animated: Bool) {
        audioPlaying.stop()
}
@IBAction func playPause(sender: AnyObject) {
    if buttonLabelChange == true {
        let image = UIImage(named: "btn_pause.png") as UIImage?
        playPauseButton.setBackgroundImage(image, forState: UIControlState.Normal)
        buttonLabelChange = false
        audioPlaying.play()
    }else{
        let image = UIImage(named: "btn_play.png") as UIImage?
        playPauseButton.setBackgroundImage(image, forState: UIControlState.Normal)
        buttonLabelChange = true
        audioPlaying.stop()
    }
}

@IBAction func sliderAction(sender: AnyObject) {
    audioPlaying.stop()
    audioPlaying.currentTime = NSTimeInterval(sliderOutlet.value)
    audioPlaying.prepareToPlay()
    audioPlaying.play()
    let image = UIImage(named: "btn_pause.png") as UIImage?
    playPauseButton.setBackgroundImage(image, forState: UIControlState.Normal)
    buttonLabelChange = false
}

Have you tried using the method isMovingFromParentViewController ?

The method returns true when the view is popped, but false when the phone locks.

override func viewWillDisappear(animated: Bool) {

        if self.isMovingFromParentViewController()
        {
                audioPlaying.stop()
        }
}

Try using it in deinit method. Hope this helps!!

deinit {

    if (audioPlayer.playing){
    audioPlayer.stop();
    }
}

do one thing register notification of the applicationWillResignActive in your view and when application is locked it goes in that methods and it stop the audioplayer.

1) put this code in viewDidLoad()

   NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"in-the-storm" ofType:@"mp3"]];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [audioPlayer play];

Remove the default navigation back button add your custom button with action.

//Hide Navigation bar - Add this code in your viewdidload(FirstViewController)

self.navigationController?.navigationBarHidden = true;

in the second view controller

@IBAction func popView (sender:AnyObject) {

   audioPlaying.stop()
    self.navigationController?.popViewControllerAnimated(true);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM