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



i当前有一个设置视图controller,可以处理启动/停止音乐并调整音乐音量。

在放松设置视图controller之后,可以让音乐继续下去吗?在打开音乐并切换了ViewControllers之后,我可以重新打开"设置"浏览器并关闭音乐?请让我知道限制。

这是我的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.
}

让我知道需要修改什么!

在您的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,然后实现第一个提到的代码,然后称您的播放器为SO

[appDelegate.audioplayer doSomething]

,因为您的 audioPlayer将在其父 SettingsViewController发布时发布。

因此,您应该将其添加到全球中,并且可以使用Singleton模式进行。

创建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];

最新更新