0

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

@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)中停止音乐。我在 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];}

我哪里做错了?

4

2 回答 2

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

..和瞧

于 2012-12-19T00:04:26.987 回答
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

我的问题有一个决定

于 2012-12-19T06:59:35.960 回答