2011-12-11 204 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到您的音頻播放器的實例。如果你在中斷之後不打電話,可能會導致你的音頻會話沒有重新啓動,這會產生上面描述的效果 - 神祕死亡的音頻。

相關問題