想用 swift 中的 .amr 音频格式文件创建一个应用程序

want to create an app with .amr audio format file in swift

您好,我想创建一个音频播放器应用程序,其中包含大约 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 不再受支持(对于 ≥ iOS 4.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];
}