1.导入代理<NSURLConnectionDataDelegate>

  1. @interface ViewController ()<NSURLConnectionDataDelegate>
  2. {
  3. long long alllength; //下载总长度
  4. long long currlenth; //当期下载长度
  5. }
  6. //存放下载的mp3 数据流
  7. @property(nonatomic,strong)NSMutableData *msicDate;
  8. //下载进度显示
  9. @property(nonatomic,strong)UIProgressView *msicProgress;
  10. //下载按钮
  11. @property(nonatomic,strong)UIButton *but;
  12. //音乐播放
  13. @property(nonatomic,strong)AVAudioPlayer *player;
  1. //添加下载图片地址
  2. NSString *path=@"http://pic1.nipic.com/2008-10-27/200810278105878_2.jpg";
  3.  
  4. //调用下载图片方法
  5. [self addTheimg:path];
  6.  
  7. //进度条设置
  8. self.msicProgress =[[UIProgressView alloc]initWithFrame:CGRectMake(, , , )];
  9. self.msicProgress.progress=;
  10. //设置下载进度颜色
  11. self.msicProgress.progressTintColor=[UIColor colorWithRed:0.007 green:0.868 blue:0.000 alpha:1.000];
  12. //进度条背景颜色
  13. self.msicProgress.trackTintColor=[UIColor grayColor];
  14.  
  15. //下载按钮点击下载
  16. self.but=[UIButton buttonWithType:UIButtonTypeCustom];
  17. _but.frame=CGRectMake(, , , );
  18. [_but setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
  19. [_but setTitle:@"下载" forState:UIControlStateNormal];
  20. _but.backgroundColor=[UIColor orangeColor];
  21. [_but addTarget:self action:@selector(plarmusic:) forControlEvents:UIControlEventTouchUpInside];
  22.  
  23. [self.view addSubview:_msicProgress];
  24. [self.view addSubview:_but];

方法的实现

  1. //按钮点击时间相应方法
  2. -(void)plarmusic:(UIButton *)sender
  3. {
  4. //歌曲下载总的长度
  5. alllength=;
  6. //当前获取的量
  7. currlenth = ;
  8. self.msicDate =[[NSMutableData alloc]init];
  9. //设置歌曲下载的地址
  10. NSString *path=@"http://yinyueshiting.baidu.com/data2/music/124345627/124104509190800128.mp3?xcode=cb12e87f8333e5370d9b8f0c677c76d2";
  11. NSURL *url=[NSURL URLWithString:path];
  12. //方式1 网络请求
  13. NSURLRequest *requst=[NSURLRequest requestWithURL:url];
  14. //方式2
  15. //NSURLRequest *resq=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
  16.  
  17. //使用代理进行请求 创建连接服务器
  18. [NSURLConnection connectionWithRequest:requst delegate:self];
  19. }
  1. //实现网络请求代理方法
  2. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  3. {
  4. //获取要下载的MP3 的长度,收到响应即可得知,不用下载完毕
  5. //此代理方法只执行一次,获取总的数据量
  6. alllength =[response expectedContentLength];
  7. }
  8.  
  9. //获取每次下载的数据量,此方法可执行多次
  10. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  11. {
  12. //将每次下载的数据放入总得数据区域内
  13. [self.msicDate appendData:data];
  14. //将本次获取的数据长度追加到当期长度上
  15. currlenth +=data.length;
  16. //更新进度条
  17. self.msicProgress.progress=(float)currlenth/alllength;
  18. }
  19.  
  20. //下载结束调用,播放音乐
  21. -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  22. {
  23. NSError *error=nil;
  24. //下载完成后,初始化player 然后进行歌曲播放
  25. self.player = [[AVAudioPlayer alloc]initWithData:self.msicDate error:&error];
  26. [self.player play];
  27. }
  28.  
  29. //请求错误调用
  30. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  31. {
  32. if (error) {
  33. NSLog(@"error == %@",error);
  34. }
  35. }
  1. //调用图片的方法
  2. -(void)addTheimg:(NSString *)path
  3. {
  4. // 1.通过字符串创建一个url
  5. NSString *imgstr=path;
  6. NSURL *imgurl=[NSURL URLWithString:imgstr];
  7.  
  8. //2. 创建网络请求
  9. NSURLRequest *requst=[NSURLRequest requestWithURL:imgurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
  10.  
  11. NSError *error=nil;
  12. //3. 链接服务器
  13. NSData *imgDate = [NSURLConnection sendSynchronousRequest:requst returningResponse:nil error:&error];
  14. if (error)
  15. {
  16. //如果错误,打印错误
  17. NSLog(@"%@",error);
  18. }else
  19. {
  20. //如果找到图片,创建一个图片视图接收图片
  21. UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
  22. img.image=[UIImage imageWithData:imgDate];
  23. //将图片显示在视图
  24. [self.view addSubview:img];
  25. }
  26.  
  27. }

iOS 通过网络请求获取图片的下载歌曲的更多相关文章

  1. ios 从网络上获取图片并在UIImageView中显示

    ios 从网络上获取图片   -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...

  2. ios 从网络上获取图片

    -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIImage * result; N ...

  3. 【iOS开发-从网络上获取图片尺寸】

    实际开发过程中,容易碰到从网络上获取图片尺寸的场景,比如一个UIImageView要装载从网络上获取的图片,但要先设置其frame,此时又不知道图片尺寸,就要从网络上获取尺寸了.为了最好的用户体验,一 ...

  4. ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)

    当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...

  5. AFNetworking网络请求与图片上传工具(POST)

    AFNetworking网络请求与图片上传工具(POST) .h文件 #import <Foundation/Foundation.h> /** 成功Block */ typedef vo ...

  6. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

  7. Android 多线程:使用Thread和Handler (从网络上获取图片)

    当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分 ...

  8. css3如何让div一直循环自转圈,附带:网络请求通知图片一直在转圈实例

    css3如何让div一直循环自转圈 代码如下: div{ -webkit-transition-property: -webkit-transform; -webkit-transition-dura ...

  9. iOS开发网络请求——大文件的多线程断点下载

    iOS开发中网络请求技术已经是移动app必备技术,而网络中文件传输就是其中重点了.网络文件传输对移动客户端而言主要分为文件的上传和下载.作为开发者从技术角度会将文件分为小文件和大文件.小文件因为文件大 ...

随机推荐

  1. Java线程和多线程(十三)——Callable,Future,FutureTask

    在Java多线程之中,Callable和Future的使用时非常广泛的.在之前的文章中,我们了解了关于Java线程池基础的一些内容,知道如何提交Runnable的任务.但是,Runnable的任务是无 ...

  2. CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression

    题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...

  3. 英特尔近日发布最新版实感™ SDK R5 (v7)

    英特尔开发人员专区 原文地址 英特尔® 实感™ SDK 的 7.0.23.8048 版本(也称为 R5)现已推出.您将看到的主要变化包括: 支持英特尔® 实感™ SR300 摄像头:应于 2016 年 ...

  4. Nginx之一:Nginx的编译安装

    一.Nginx简介 官方网址:http://nginx.org/ Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始, ...

  5. 785. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. 【python 2.7】python读取json数据存入MySQL

    同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...

  7. xshell—实现Linux与Windows之间的文件传递

    在Windows系统上,通过xshell连接Linux系统. 第一种使用方式:从Linux系统上下载文件到Windows系统. 准备工作: $ sudo apt-get install lrzsz 安 ...

  8. Python基础灬dict&set

    字典dict 字典使用键-值(key-value)存储,具有极快的查找速度. dict基本操作 取值 a_dict = {'name': 'jack', 'age': 18} print(a_dict ...

  9. win7重装系统后设置Python2.7环境

    起因 台式机的主板莫名出现问题,显示器画面卡顿不能动,鼠标键盘无反应,在这种情况下只好按住电源键断电.下面重启后,显示器无画面,猜测开机后没有进BIOS.然后就拆机箱,拔下电源线后撬起主板电池几秒再放 ...

  10. leetcode个人题解——#43 Multiply Strings

    思路:高精度乘法就可以了. 有两个错误以前没在意,1.成员属性定义时候不能进行初始化, vector<); 这样隐性调用了函数进行初始化的形式特别要注意,也是错误的: 2.容器类只有分配了空间时 ...