从 URL 下载音频文件并在 iOS App 中播放

Download Audio file from URL and play it in iOS App

我有一个 URL 是我从 JSON 响应中得到的,当我在浏览器中点击 URL 时,文件已下载,现在我想知道如何播放这样的 URL 这是一个下载 URL.I 已经四处寻找了很多 post 但其中 none 可以帮助我。

url 的类型正在获取:http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1

下面是我试过的代码。

  NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
  NSLog(@"url:%@",audio);

  NSURL  *url = [NSURL fileURLWithPath:audio];
  NSData *soundData = [NSData dataWithContentsOfURL:url];
  NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                                                           fileURLWithPath:filePath] error:&error];
    [_audioPlayer play];
    NSLog(@"error %@", error);

尝试使用AVPlayer或AVPlayerViewController,可以直接使用URL播放mp3文件

即使使用本地文件,您也会先将其下载到设备,然后从本地播放 URL

不要忘记对象 AVPlayer 必须是 Class 的 属性 而不是本地对象,因为 auto-releasing,所以在 class 中声明播放器变量并在功能中设置,而不仅仅是在功能中设置并尝试播放...

[更新]

更多详细信息 -> How to play video with AVPlayerViewController (AVKit) in Swift

试试这个:

    //download your audio file
    NSString *urlString = @"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
    NSURL  *url = [NSURL fileURLWithPath:urlString];
    NSData *soundData = [NSData dataWithContentsOfURL:url];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];

    // get the file from directory
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [pathArray objectAtIndex:0];
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundUrl;
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
    {
        soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
    }

    // play audio file
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [audioPlayer prepareToPlay];
    [audioPlayer play];

您应该异步下载音频文件。

这里是错误的

NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
NSURL  *url = [NSURL fileURLWithPath:audio];

我假设它是一个远程文件 url,您不能将其视为一个文件 url。所以你必须使用

NSURL  *url = [NSURL urlWithString:audio];

谢谢 Dipankar Das 的帮助。 我在这里发布整个解决方案。 这绝对对我有用。

    NSString *audio=@"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
    NSURL  *url = [NSURL URLWithString:audio];
    NSData *soundData = [NSData dataWithContentsOfURL:url];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];

    // get the file from directory
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [pathArray objectAtIndex:0];
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundUrl;
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
    {
        soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
    }

    // plau audio file
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [_audioPlayer prepareToPlay];
    [_audioPlayer play];
    [_audioPlayer setVolume:5.0];