2011-12-11 205 views
1

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

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

后来我用[clickSound play]播放;

回答

3

确保u有

#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"]; 

在你 - (无效)玩{}方法你有

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

enjoi

+0

谢谢。它并没有真正解决我的问题,但它是一个优雅的解决方案。我认为这是它与我同时运行的AVAudioRecorder(某种VU Meter)相冲突的原因。所以我以此为解决方案,并将找出一个新问题。谢谢! – Snilleblixten

1

当你的应用转到后台(或时钟闹铃响起,手机接到电话或屏幕锁定),应用程序的音频会话中断,音频播放器暂停。处理这种中断的推荐方法是在您的音频播放器的代表中实现AVAudioPlayerDelegate方法– audioPlayerBeginInterruption:-audioPlayerEndInterruption:。从苹果公司的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到您的音频播放器的实例。如果你在中断之后不打电话,可能会导致你的音频会话没有重新启动,这会产生上面描述的效果 - 神秘死亡的音频。

相关问题