我正在尝试通过播放 MP3AVAudioPlayer
我认为相当简单的 MP3。不幸的是,它并不完全有效。这是我所做的一切:
- 为了测试,我在 Xcode 中创建了一个新的 iOS 应用程序(Single View)。
#import <AVFoundation/AVFoundation.h>
我将 AVFoundation 框架添加到项目以及ViewController.m
我在 Apps 'Documents' 文件夹中添加了一个 MP3 文件。
我将其更改
ViewControllers
viewDidLoad:
为以下内容:
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil];
audioPlayer.numberOfLoops = 1;
[audioPlayer play];
//[NSThread sleepForTimeInterval:20];
}
不幸的是,音频显然在开始播放后立即停止。如果我取消注释sleepForTimeInterval
它会播放 20 秒然后停止。仅在使用 ARC 编译时才会出现此问题,否则,它可以完美运行。