您好我想创建一个音频播放器应用程序,其中包含大约250首歌曲。应用必须离线工作。所以,我试图使用.aac
和.mp3
格式的音频,但应用程序的大小增加到300Mb
附近。
现在我想要使用.amr
格式,但模拟器不适用于.amr
格式。需要建议。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
playList = [
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn01", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn02", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn01", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn04", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn05", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn06", ofType: "aac")!),
URL(fileURLWithPath:Bundle.main.path(forResource: "vpn07", ofType: "aac")!)]
Check the following points:
不再支持AMR(对于≥iOS4.3,请参阅Apple iOS SDK文档中支持的音频格式)。您想使用AVPlayer(音频和视频)还是只想要音频?仅用于音频AVAudioPlayer。以下代码段显示了如何处理它。如果你想使用AVAudioPlayer,你的自我实例是否实现了AVAudioPlayerDelegate协议?您的自我实例是否实现了AVAudioPlayerDelegate协议?你有没有正确添加和链接AVAudioFoundation框架(#import)?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audio" ofType:@"m4a"]];
NSError *error = noErr;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
}
else
{
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
}
}
- (void)playAudio
{
[self.audioPlayer play];
}