Audioplayer不会停止其他视图

时间:2012-12-18 17:21:32

标签: iphone objective-c ios

在第一个视图中,我从头开始创建音频播放器并加载

@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];
}

好的,我有很多观点,我很高兴音乐剧在我工作的观点中总是无关紧要。但我需要停止音乐(我的设置视图)。我在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];}

我错在哪里?

2 个答案:

答案 0 :(得分: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];}

..和Voila

答案 1 :(得分: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;

所以现在我可以使用

从任何ViewController播放/停止音乐
#import "AudioViewController.h"
...
[[AudioViewController sharedplayer]player:YES]//for playing
[[AudioViewController sharedplayer]player:NO]//for stoping

我的问题有决定