在xcode 4.2上播放时没有背景音乐

时间:2012-08-27 12:56:46

标签: iphone objective-c ios xcode ipad

我正在使用以下代码:mY声音放在myapp / Shared Resources / background_music.aiff

@synthesize window = _window;
- (void)dealloc
{
    [backgroundSound release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    sleep(3);
     //this variable can be named differently

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/background_music.aiff",[[NSBundle mainBundle] resourcePath]]];
    NSError *error;

    backgroundSound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

     backgroundSound.numberOfLoops = -1;
    //>> this will allow the file to //play an infinite number of times */

    return YES;
}



@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    AVAudioPlayer *backgroundSound;

}

@property (strong, nonatomic) UIWindow *window;
@end

没有错误,也没有循环声。请告诉我发生了什么。

最好的问候

2 个答案:

答案 0 :(得分:2)

好吧,我添加了[background play];,它已经开始正常工作.....我在跟随的教程中没有提到..

答案 1 :(得分:0)

 player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    player.movieSourceType = MPMovieSourceTypeStreaming;
    if(player.playbackState == MPMoviePlaybackStatePlaying)
        [player stop];
    [player play];