如何停止从另一个ViewController播放音频?

时间:2016-03-18 13:29:22

标签: ios swift audio uiviewcontroller

我在xcode中创建了四个ViewControllers,每个按下播放按钮时都会播放不同的音频混合。如果我导航到另一个视图控制器并按下播放按钮...如何停止播放所有其他音频,并确保新音频混合播放成功?

下面详细介绍的代码存在于四个ViewControllers中。唯一的区别是我创建了三个新的musicPlayer实例。他们是musicPlayerSummer,musicPlayerWinter,musicPlayerAutumn。我已经为每个视图控制器创建了新按钮。

任何建议都将不胜感激。

谢谢

导入UIKit 导入AVFoundation

class ViewControllerSpring:UIViewController {

var musicPlayer: AVAudioPlayer!
var mySongs = ["1", "2", "3", "4"]



override func viewDidLoad() {
    super.viewDidLoad()


    initAudio()


    // Do any additional setup after loading the view.

}

func initAudio() {



    let path = NSBundle.mainBundle().pathForResource(mySongs[0], ofType: "mp3")!

    do {

        musicPlayer = try AVAudioPlayer(contentsOfURL: NSURL(string: path)!)
        musicPlayer.prepareToPlay()
        musicPlayer.numberOfLoops = -1



    } catch let err as NSError {
       print(err.debugDescription)


    }




    let session:AVAudioSession = AVAudioSession.sharedInstance()

    do {
        try session.setCategory(AVAudioSessionCategoryPlayback)
    } catch {
        //catching the error.
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

override func prefersStatusBarHidden() -> Bool {
    return true
}






@IBAction func springPlayPressed(sender: UIButton) {

    if musicPlayer.playing {

        musicPlayer.stop()


        // for normal state
        sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)


    } else {

        musicPlayer.play()





        // for Highlighted state
        sender.setImage(UIImage(named: "Pause.png"), forState: UIControlState.Normal)
    }
}

}

2 个答案:

答案 0 :(得分:1)

要解决此问题,请在appDelegate中声明audioPlayer

// inside AppDelegate
var audioPlayer : AVAudioPlayer? = nil

然后在你的viewController中声明:

var audioPlayer : AVAudioPlayer? {
    get {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        return appDelegate.audioPlayer
    }
    set {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.audioPlayer = newValue
    }
}

答案 1 :(得分:0)

您可以使用通知。

音频结束时发送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAudioTerminatedNotification" object:self];

接收到您的其他视图:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioTerminatedNotification:) name:@"MyAudioTerminatedNotification" object:nil];

你采取行动:

- (void)audioTerminatedNotification:(NSNotification *)notification {
   // Do you action
}

处理它:

[[NSNotificationCenter defaultCenter] removeObserver:self];