使用 AVAudioPlayer 向正在运行的应用程序添加声音,它会在 prepareToPlay 上停止/挂起。请注意,它也会停止/挂起。多次点击“继续执行程序”可使应用程序恢复。只有在模拟器中运行程序时才会出现此问题。
使用 Xcode 4.6
代码片段:
视图控制器.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@end
视图控制器.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]
.
.
.