2014-12-05 35 views
-1

我有2視圖控制器的第一個是主視圖控制器,並將運行音樂時,視圖控制器加載和其他視圖控制器是設置內容的UISwitch所以我想要的是當用戶去設置視圖控制器,並打開關掉那麼音樂將在主視圖 停在這裏是代碼連接UISwitch與事件在其他視圖控制器

main.h

#import<AVFoundation/AVAudioPlayer.h> 

@interface main : UIViewController 
{ 

AVAudioPlayer *audioPlayer; 
BOOL didInitialize; 

}

的main.m

static BOOL didInitialize = NO; 
if (didInitialize == YES) 
return; 
didInitialize = YES; 
//Add Audio Sound to the current View Controller 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MLGBoxSoundTrack.mp3", [[NSBundle mainBundle] resourcePath]]]; 

NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
audioPlayer.numberOfLoops = -1; // -1 for looping forever 

// audioPlayer.volume = 0.5; // 0.0 - no volume; 1.0 full volume 
// NSLog(@"%f seconds played so far", audioPlayer.currentTime); 
// audioPlayer.currentTime = 5; // jump to the 10 second mark 
// [audioPlayer pause]; 
// [audioPlayer stop]; // Does not reset currentTime; sending play resumes 

if (audioPlayer == nil) 
{ 
    NSLog(@"No Audio Player"); 
} 
else 
{ 
    [audioPlayer play]; 
} 
//End Audio Sound 

回答

0

Use NSNotificationCenter

首先視圖控制器,用於切換通知,開關切換時第二視圖控制器交通知添加觀察者。

+0

可以請您提供此問題的代碼 – Mahmoud 2014-12-05 20:58:25

+0

網上有很多可用的資源,包括我的答案中包含的鏈接。 – shim 2014-12-06 05:06:58

相關問題