2012-12-18 25 views
0

在第一視圖停止我從一開始Audioplayer不會從其他視圖

@interface SplashViewController : UIViewController 
... 
@property (strong, nonatomic) AVAudioPlayer *mp3; 
... 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self viewDidAppear:YES]; 
NSString *path = [[NSBundle mainBundle]pathForResource:@"sooner" ofType:@"mp3"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
NSError *error; 
mp3 = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; 
[mp3 setNumberOfLoops:-1]; 
[mp3 setVolume:1.0]; 
[mp3 play]; 
} 

好吧,IHAVE很多意見創建音頻播放器和負載,我高興的是,MUSIK發揮始終都無所謂在什麼看法我的工作。但我需要在其他(我的SettingView)中停止音樂。我有SettingView中的下一個代碼,但我沒有結果 - 音樂播放和播放,並且不想停止

@class SplashViewController; 
@interface SettingsViewController : UIViewController 

@property (strong, nonatomic) SplashViewController *splashViewController; 

________________________________________________________ 
#import "SplashViewController.h" 
... 
@implementation SettingsViewController 
@synthesize pauseMusik; //UISwitch 
@synthesize splashViewController = _splashViewController; 
... 
-(IBAction)playPauseMusik:(id)sender 
{ 
if(!self.splashViewController) 
    self.splashViewController = [[SplashViewController 
alloc]initWithNibName:@"SplashViewController" bundle:nil]; 
if (pauseMusik.on) { 
    [self.splashViewController.mp3 play]; 
} 
else 
    [self.splashViewController.mp3 stop];} 

我在哪裏弄錯了?

+0

嘗試:1)明確設置mp3 p roperty通過使用[self setMp3:...]; 2)使用大於0的數字作爲循環的數量3)使用[_splashViewController.mp3 stop]; –

+0

2和3沒有幫助,約1 - 我不明白^我必須做什麼=( – Neznajka

+0

替換行= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; with [self setMp3:[[ AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]]; –

回答

1

我創建單獨的類AudioViewController,並創建一個方法:

@implementation AudioViewController 
@synthesize mp3; 
static AudioViewController * sharedPlayer = NULL; 
+ (AudioViewController *) sharedPlayer { 
if (!sharedPlayer || sharedPlayer == NULL) { 
    sharedPlayer = [AudioViewController new]; 
} 
return sharedPlayer; 
} 

而且創建方法播放/暫停:

-(void)player:(BOOL)playPause 
{ 
if (playPause==YES) 
    [mp3 play]; 
else [mp3 stop]; } 

哪裏MP3是

AVAudioPlayer *mp3; 

所以,現在我可以簡單地從任何視圖控制器與

#import "AudioViewController.h" 
... 
[[AudioViewController sharedplayer]player:YES]//for playing 
[[AudioViewController sharedplayer]player:NO]//for stoping 

有播放/停止MUSIK我的問題的決定

+0

另一個好方法。 – TonyMkenu

1

您可以使用NSNotificationCenter。例如..您的IBAction爲(來自SettingController):

if.. { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"actionChangedStop" object:nil]; 
} 
else 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"actionChangedPlay" object:nil]; 
} 

NSNotificationCenter發送 「廣播」 消息到整個應用程序。

現在我們需要一個觀察者。在SplashController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
.. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(actionChangedStop) 
               name:@"actionChangedStop" 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(actionChangedPlay) 
               name:@"actionChangedPlay" 
               object:nil]; 
... 

.. 一個給定的方法被稱爲..

-(void)actionChangedStop{[audioPlayer stop];} 
-(void)actionChangedPlay{[audioPlayer play];} 

..瞧

+0

Thnaks,但我發現決定 – Neznajka