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

时间:2017-01-12 10:12:54

标签: ios objective-c avaudioplayer

我有一个来自JSON响应的URL,当我点击浏览器中的URL时,文件被下载,现在我想知道如何播放这样的URL,这是一个下载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);

4 个答案:

答案 0 :(得分:2)

试试这个:

class MyClass
  def initialize(with_account = nil)
    @account = Account.new if with_account
  end

  def current_status
    account.status
  end

  def account
    @account || NilAccount.new
  end
end

class Account
  def status
    "Up!"
  end
end

class NilAccount
  def status
    "Down!"
  end
end

puts MyClass.new(:with_account).current_status
#=> "Up!"
puts MyClass.new.current_status
#=> "Down!"

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

答案 1 :(得分:2)

谢谢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];

答案 2 :(得分:1)

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

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

不要忘记,由于自动释放,对象AVPlayer必须作为Class而不是本地对象的属性,因此在类中声明你的播放器变量并在函数中设置不仅仅是本地函数并尝试播放... < / p>

[更新]

更多细节如何操作 - &gt; How to play video with AVPlayerViewController (AVKit) in Swift

答案 3 :(得分:0)

这是错误

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

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

NSURL  *url = [NSURL urlWithString:audio];