我一直在使用此处iOS background music problems中所述的代码,我无法弄清楚如何在AppDelegate.h文件中声明出口的答案...我一直把它放在任何地方但它仍然没有...
这些是我的代码:
AppDelegate.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
AVAudioPlayer *audio2;
IBOutlet UISlider *VolumeS2;
AVAudioPlayer *audioPlayer2;
NSTimer *VolumeTimer2;
}
@property (nonatomic, retain)AVAudioPlayer *audio2;
@property (strong, nonatomic) UIWindow *window;
@end
Appdelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; }
ViewController.m
- (void)viewDidLoad
{
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) {
NSError *err;
NSString *path2 = [[NSBundle mainBundle]
pathForResource:@"EasyLemon" ofType:@"mp3"];
self.audio2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path2] error:&err];
audio2.delegate = self;
audio2.numberOfLoops = -1;
[audio2 play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"city.png"],
[UIImage imageNamed:@"city2.png"],
[UIImage imageNamed:@"city3.png"], nil];
[animation setAnimationRepeatCount:0];
animation.animationDuration = 1;
[animation startAnimating];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSError *err;
NSString *path2 = [[NSBundle mainBundle]
pathForResource:@"EasyLemon" ofType:@"mp3"];
self.audio2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path2] error:&err];
audio2.delegate = self;
audio2.numberOfLoops = -1;
[audio2 play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
很抱歉长代码......我只是想不通怎么做。