2013-03-13 31 views
21

使用AVAudioPlayer向正在運行的應用程序添加聲音時,它停止/掛在prepareToPlay上。注意它也停止/暫停播放。點擊「繼續執行程序」幾次使應用程序恢復。只有在模擬器中運行程序時纔會出現此問題。Xcode在prepareToPlay上停止

使用的Xcode 4.6

代碼段:

ViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController <AVAudioPlayerDelegate> 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
} 

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self initilizeAudio]; 
} 

- (void)initilizeAudio 
{ 
    NSError *error = nil; 
    NSURL *audioURL = [[NSBundle mainBundle] URLForResource:@"Background" withExtension:@"mp3"]; 

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]; 
    if (error) 
    { 
     NSLog(@"Error in audioPlayer: %@", [error localizedDescription]); 
    } 
    else 
    { 
     [self.audioPlayer setDelegate:self]; 
     [self.audioPlayer prepareToPlay]; 
    } 
} 

@end 

堆棧跟蹤

0 __cxa_throw 
21 -[AVAudioPlayer prepareToPlay] 
22 -[ViewController viewDidLoad] 
. 
. 
. 
+5

顯然我不允許回答我自己的問題。所以在評論中提供答案。 問題是我通常使用設置爲「所有異常」的斷點開發,實際引發的異常是__cxa_throw。這顯然是在用於實現AVAudioPlayer的C++庫中。通過將斷點更改爲「所有Objective-C異常」,程序運行良好。 (這可以通過編輯斷點並將異常字段更改爲Objective-C來完成。) – zermat 2013-03-13 21:44:16

+1

根據[此Meta on上的帖子](http://meta.stackexchange.com/a/17467),您必須等待8小時,如果你有小於100代表。 48小時後你可以接受你自己的答案。 – Sebastian 2013-03-13 22:03:01

回答

66

問題是我通常使用設置爲「所有異常」的斷點來開發,而拋出的實際異常是__cxa_throw。這顯然是在用於實現AVAudioPlayer的C++庫中。通過將斷點更改爲「所有Objective-C異常」,程序運行良好。 (這可以通過編輯斷點,改變例外領域的Objective-C來完成。

+1

太棒了,我很高興你發佈這個,因爲我在這一個上撓頭! – LilMoke 2013-03-26 08:55:33

+1

你先生,救了我的一天!我不能夠感謝你!我遇到了完全相同的問題,這讓我發瘋。 :-) – thomas 2013-05-01 06:35:18

+0

謝謝!解決了我的問題呢! – Marky 2013-12-11 08:50:39

4

我有這個問題了。上述工程的解決方案,但我覺得有一個更好的辦法來解決它。

的作者建議忽略的錯誤是指示mac正試圖發送音頻到一個不工作的設備,我猜測代碼是用C編寫的,所以把異常轉換爲「僅限於objective-c」會使異常

真正的解決方法是讓音頻設備重新工作對我來說,失敗的原因是我切換了一些音頻輸入和輸出,但(看起來)IOS模擬器已經記住了舊的設置。所以,當我試圖在這個線程的各個步驟:

Error '!dat' trying to set the (null) audio devices' sample rate

我的聲音重新工作,並停止拋出任何異常。

+0

問題不在於模擬器上沒有音頻硬件,而且這似乎導致可以(應該)忽略的中間C++異常(我的機制在接受的答案中提出)。 – Drux 2015-02-25 10:49:47