按主页按钮后,iPhone应用程序中没有声音

时间:2011-12-11 14:43:51

标签: iphone ios5 avaudioplayer

我的声音在模拟器中工作正常,但是当我在手机上运行应用程序时,按下主页按钮然后返回应用程序后声音停止工作。这是在IOS5中。我怎么解决这个问题? AVAudioPlayer的委托事情似乎也停止了。该应用程序不会崩溃。

NSString *path = [[NSBundle mainBundle]
    pathForResource:@"Beep01" ofType:@"wav"];
clickSound =[[AVAudioPlayer alloc] initWithContentsOfURL:
    [NSURL fileURLWithPath:path] error:NULL];
clickSound.delegate = self;
[clickSound prepareToPlay];

后来我用[clickSound play]播放它;

2 个答案:

答案 0 :(得分:3)

确保你有

#import <AVFoundation/AVFoundation.h>
在标题中

。 然后在你的AppDelegate中你应该有这个方法:

- (AVAudioPlayer *) getSound: (NSString *) soundName {  
@try {              
    AVAudioPlayer *sound = [[self getDictionary] objectForKey: soundName];      
    if (!sound) {           
        NSError *error;
        NSString *path = [[NSBundle mainBundle] pathForResource: soundName ofType: nil];             
        sound = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] 
                                                       error: &error];             
        if (!sound) {
            //NSLog(@"ERROR: Wrong sound format: %@. Description: %@",  soundName,  [error localizedDescription]);
        } else {
            sound.volume = 0.7;
            //int len = sound.duration;
            [[self getDictionary] setObject: sound forKey: soundName];
            // NSLog(@"%@ loaded, duration: %i sec", soundName, len);
            [sound release];
        }    
    } 
    return sound;
} 
@catch (id theException) {
    NSLog(@"ERROR: %@ not found!", soundName);
} 
return nil;

}

- (NSMutableDictionary *) getDictionary {
if (!dictionary) { //Hashtable
    dictionary = [[NSMutableDictionary alloc] init]; 
    NSLog(@"new Dictionary");
}
return dictionary;
}
- (void) playSound: (NSString *) soundName {
AVAudioPlayer *sound = [self getSound: soundName];
if (sound) {   
    sound.currentTime = 0;        
    if (!sound.playing) {
        sound.numberOfLoops = 0;
        [sound play];
    }    
} 
}

- (void) stopSound: (NSString *) soundName {
AVAudioPlayer *sound = [self getSound: soundName];
if (sound && sound.playing) {           
    [sound stop];         
} 
}

在您的AppDelegateDidFinishLaunching中,您预先加载您将使用的所有声音:

//pre-Load sounds
[self getSound: @"testSong.wav"];

你的 - (void)play {}方法

    YourAppDel *appDel = [UIApplication sharedApplication].delegate;
    [appDel playSound:@"testSong.wav"];

enjoi

答案 1 :(得分:1)

当您的应用程序进入后台(或时钟闹钟响起,或手机接到电话或屏幕锁定)时,您应用的音频会话会中断,您的音频播放器会暂停。处理此中断的推荐方法是在音频播放器的委托中实施AVAudioPlayerDelegate方法– audioPlayerBeginInterruption:-audioPlayerEndInterruption:。来自Apple的documentation

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
    if (playing) {
        playing = NO;
        interruptedOnPlayback = YES;
        [self updateUserInterface];
    }
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
    if (interruptedOnPlayback) {
        [player prepareToPlay];
        [player play];
        playing = YES;
        interruptedOnPlayback = NO;
    }
}

请注意,调用-audioPlayerEndInterruption:时,会再次向您的音频播放器实例发送-prepareToPlay。如果您在中断后没有调用它,那么您的音频会话可能不会重新启动,这将产生您上面描述的效果 - 神秘的死音频。