切换ViewControllers时如何保持播放背景音乐?

时间:2017-04-18 04:15:43

标签: ios objective-c xcode

我目前有一个SettingsViewController,用于处理开始/停止音乐和调整音乐音量。

在展开SettingsViewController后,是否可以让音乐保持打开状态?打开音乐并切换ViewControllers后,我可以重新打开SettingsViewController并关闭音乐吗?请让我知道这些限制。

以下是我的SettingsViewController.h的代码

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface SettingsViewController : UIViewController <AVAudioPlayerDelegate>
{
    AVAudioPlayer *audioPlayer;
    IBOutlet UISwitch *Switch;
    IBOutlet UISlider *Slider;

}
-(IBAction)Switch:(UISwitch *)sender;
-(IBAction)Slider:(UISlider *)sender;
@end

这是我的SettingsViewController.m的代码

#import "SettingsViewController.h"

@interface SettingsViewController ()

@end

@implementation SettingsViewController

-(IBAction)Switch:(UISwitch *)sender{
    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];

    if(sender.tag == 0){
        if(sender.on){
            [standardDefaults setObject:@"On" forKey:@"keyName"];
            //choosing and setting the music file
            NSString *music = [[NSBundle mainBundle]pathForResource:@"bgmusic1" ofType:@"mp3"];
            audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
            audioPlayer.delegate = self;
            audioPlayer.numberOfLoops= -1; //sets the music to loop infinitely
            [audioPlayer play]; //plays the music


        } else if (sender.on == 0){
            [standardDefaults setObject:@"Off" forKey:@"keyName"];
            [audioPlayer stop]; //stops the music
        }
    }
    [standardDefaults synchronize];

}

-(IBAction)Slider:(UISlider *)sender{
    audioPlayer.volume = sender.value / 100.0; //will adjust the volume of the music according the slider value
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

让我知道需要修改的内容!

2 个答案:

答案 0 :(得分:1)

在AppDelegate中执行此操作。

要从任何地方访问它,请在ViewControllers .m文件中导入它的.h文件并使用

访问它
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

将audioplayer设置为AppDelegate.h中的属性以允许公共访问(例如,您的其他viewControllers)

@interface AppDelegate: NSAppDelegate
 {
    IBOutlet UISwitch *Switch;
    IBOutlet UISlider *Slider; 
}

@property AVAudioPlayer *audioPlayer; // <------
-(IBAction)Switch:(UISwitch *)sender;
-(IBAction)Slider:(UISlider *)sender;
@end

然后调整m文件中audioplayer的每次调用,以采用h文件中的更改。

//Instead of [audioplayer doSomething] write...
[self.audioplayer doSomething];

// in modern objective-c you can use also
[_audioplayer doSomething];

要从其他ViewControllers调用您的audioplayer然后实现第一个提到的代码并像这样调用您的播放器

[appDelegate.audioplayer doSomething]

答案 1 :(得分:0)

因为audioPlayer将在其父SettingsViewController发布后发布。

所以你应该把它添加到全局,你可以使用Singleton Pattern。

创建BackgroundPlayer.h

@interface BackgroundPlayer: NSObject {
   AVAudioPlayer *audioPlayer;
}

+ (id)sharedManager;

@end

BackgroundPlayer.m

@implementation BackgroundPlayer

static BackgroundPlayer* sharedInstance = nil;

+ (BackgroundPlayer*) sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];

    return self;
}

-(void)playAudio:(NSString *) audioPath
{
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:NULL];
    audioPlayer.delegate = self;
    audioPlayer.numberOfLoops= -1; //sets the music to loop infinitely
    [audioPlayer play]; //plays the music
}

-(void)setVolum:(float) volum {
    if (audioPlayer) {
        audioPlayer.volume = sender.value / 100.0;
    }
}

然后你打电话

NSString *music = [[NSBundle mainBundle]pathForResource:@"bgmusic1" ofType:@"mp3"];
[[BackgroundPlayersharedInstance] playAudio:music];
相关问题