首页 > 解决方案 > 想用 .amr 音频格式文件快速创建一个应用程序

问题描述

您好我想创建一个音频播放器应用程序,其中包含大约 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")!)]

标签: iosiphoneswiftxcodeavaudioplayer

解决方案


Check the following points:

不再支持 AMR(对于 ≥ iOS 4.3,请参阅 Apple iOS SDK 文档中支持的音频格式)。您想使用 AVPlayer(音频和视频)还是只需要音频?仅用于音频的 AVAudioPlayer。以下代码片段显示了如何处理它。如果你想使用 AVAudioPlayer,你自己的实例是否实现了 AVAudioPlayerDelegate 协议?您的 self 实例是否实现了 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];
}

推荐阅读