2016-03-18 28 views
1

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

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

任何建议,将不胜感激。

谢谢

进口的UIKit 进口AVFoundation

类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

有几个方法。一种方法是委派,其他方式是PostNotification。如果只有一个玩家,那么应该使用授权,如果更多的玩家停止,那么应该使用Notification Observer。 – iMuzahid

+0

同意,最佳做法与上述一致。但是,如果你想采取捷径......(通常被认为是不好的做法)......让你的球员变得更加全球化,或者更好地使他们成为单身。 – MikeG

+0

为什么让球员全球化是不好的做法? – hoboman

回答

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]; 
0

为了解决这个问题声明audioPlayer中的appDelegate

// 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 
    } 
}