iOS 网络请求的Url(音频视频的URL)本地存储

19 篇文章 0 订阅
/存储


  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *ad_voice =@"http://fdfs.xmcdn.com/group11/M00/AD/5F/wKgDa1ZfFi-gRNYMABuxsyS4ea4688.mp3
";
             NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
             NSString *filePath = [NSString stringWithFormat:@"%@/adVideo",docDirPath];
           if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {//如果不存在就存储 存在就不存
            NSString *urlStr = [NSString stringWithFormat:@"%@",ad_voice];
            NSURL *url1 = [[NSURL alloc]initWithString:urlStr];
            NSData * audioData = [NSData dataWithContentsOfURL:url1];
            [audioData writeToFile:filePath atomically:YES];
               }
             });
//调用


 NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                NSString *filePath = [NSString stringWithFormat:@"%@/adVideo",docDirPath];
 BOOL isHave = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
 if (isHave) {    
  NSURL *url  = [NSURL fileURLWithPath:filePath];
     //在此直接调用URL即可      

          }

想要了解更多请关注公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将网络音频URL存储到本地并播放,可以按照以下步骤进行操作: 1. 首先,使用AVPlayer或AVPlayerViewController来播放网络音频。例如: ```swift let url = URL(string: "http://example.com/audio.mp3") let player = AVPlayer(url: url!) let playerViewController = AVPlayerViewController() playerViewController.player = player present(playerViewController, animated: true) { player.play() } ``` 2. 接下来,使用URLSession来下载音频文件并将其保存到本地。例如: ```swift let url = URL(string: "http://example.com/audio.mp3") let task = URLSession.shared.downloadTask(with: url!) { (location, response, error) in guard let location = location else { return } let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let destinationUrl = documentsUrl.appendingPathComponent("audio.mp3") do { try FileManager.default.moveItem(at: location, to: destinationUrl) // 播放本地音频 let player = AVPlayer(url: destinationUrl) let playerViewController = AVPlayerViewController() playerViewController.player = player present(playerViewController, animated: true) { player.play() } } catch { print(error) } } task.resume() ``` 3. 最后,为了节省空间,可以在不需要时删除本地音频文件。例如: ```swift let fileManager = FileManager.default let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! let destinationUrl = documentsUrl.appendingPathComponent("audio.mp3") do { try fileManager.removeItem(at: destinationUrl) } catch { print(error) } ``` 以上就是实现将网络音频URL存储到本地并播放的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值