zoukankan      html  css  js  c++  java
  • 音频下载

    - (void)requestMp3forNews:(NSString *)mp3String

    {

        self.progress = 0;

        NSURL * url = [NSURL URLWithString:mp3String];

        NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];

        [NSURLConnectionconnectionWithRequest:urlRequest delegate:self];//异步下载

        self.mp3Url = mp3String;

    }

    #pragma mark - NSURLConnectionDataDelegate

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

        _receiveData = [[NSMutableData alloc] init];

        _allLength = [response expectedContentLength];//获取文件的大小

        self.response = response;

    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

        

        [_receiveDataappendData:data];//下载文件的数据,下载的数据大小越来越多

        //改变进度条值

        if (_allLength) {

            self.progress = [_receiveDatalength]/(double)_allLength;//下载比例

        }

        if (self.delegate && [self.delegate respondsToSelector:@selector(downloadAudio:)]) {

            [self.delegate downloadAudio:self];

        }

    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{

        

        /*

         将下载好的数据写入沙盒的Documents

         */

        NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];//沙盒的Documents路径

    NSLog(@"+++++docPath = %@",docPath);

        NSString *filePath=[docPath  stringByAppendingPathComponent:[self.response  suggestedFilename]];

        NSLog(@"+++++filePath = %@",filePath);//带音频文件的所有路径

        

        [_receiveDatawriteToFile:filePath atomically:YES];//将文件路径写到文件里

        self.filePath = filePath;

        

        if (self.filePath &&self.delegate && [self.delegate respondsToSelector:@selector(downloadAudio:isDownLoad:)]) {

            [self.delegatedownloadAudio:selfisDownLoad:YES];

        }

    }

    #pragma mark - NSURLConnectionDelegate

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

        

        //下载发生错误

        if (self.delegate && [self.delegate respondsToSelector:@selector(downloadAudio:isDownLoad:)]) {

            [self.delegatedownloadAudio:selfisDownLoad:NO];

        }

        if (error) {

            NSLog(@"%@",[error localizedDescription]);

        }

    }

  • 相关阅读:
    学数据结构,仅仅须要主要的编程体验
    Android中的跨进程通信方法实例及特点分析(二):ContentProvider
    phpStorm打开提示 failed to create JVM 的解决的方法
    (转)Hibernate框架基础——Java对象持久化概述
    (转)版本管理工具介绍——SVN篇(二)
    (转)版本管理工具介绍——SVN篇(一)
    (转)全文检索技术学习(三)——Lucene支持中文分词
    (转)全文检索技术学习(二)——配置Lucene的开发环境
    (转)全文检索技术学习(一)——Lucene的介绍
    (转) 学习淘淘商城第一课
  • 原文地址:https://www.cnblogs.com/leevaboo/p/3230003.html
Copyright © 2011-2022 走看看