iOS Learning-10 downloads (3) nsurlsession Music Chapter

Source: Internet
Author: User

With nsurlsession download, it is important to note that the file download file is automatically saved to a temporary directory, requiring the developer to re-put this file in other specified directories

////VIEWCONTROLLER.M//Web-related////copyright©2016 year Asamu. All rights reserved.////Http://mr7.doubanio.com/832d52e9c3df5c13afd7243a770c094f/0/fm/song/p294_128k.mp3#import "ViewController.h"#import<AVFoundation/AVFoundation.h>@interfaceViewcontroller () @property (nonatomic,strong) Avaudioplayer*Avaudioplayer;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; [Self downloadFile];}-(void) playmusic{//Get Cache directoryNSString *cachepath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; //get the songs under the cache directoryNSString *filepath = [CachePath stringbyappendingpathcomponent:@"Xxx.mp3"]; /*fileurlwithpath: File link urlwithstring:http link*/Nsurl*fileurl =[Nsurl Fileurlwithpath:filepath]; //determine if the file does not exist    if([[[Nsfilemanager Defaultmanager]fileexistsatpath:filepath]) {NSLog (@"exist"); Nserror*error; _avaudioplayer= [[Avaudioplayer alloc]initwithcontentsofurl:fileurl error:&ERROR]; if(Error) {NSLog (@"%@", error.localizeddescription); }    //Add Cache[_avaudioplayer Preparetoplay]; //Play[_avaudioplayer play]; }}#pragmaMark-Private Method-(void) downloadfile{NSString*filename =@"Xxx.mp3"; //Get URLNSString *urlstr = [NSString stringWithFormat:@"Http://mr7.doubanio.com/832d52e9c3df5c13afd7243a770c094f/0/fm/song/p294_128k.mp3", filename]; Nsurl*url =[Nsurl Urlwithstring:urlstr]; //Create requestNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //Create a session (global session)Nsurlsession *session =[Nsurlsession sharedsession]; //Create a taskNsurlsessiondownloadtask *downloadtak = [Session downloadtaskwithrequest:request completionhandler:^ (NSURL * _ Nullable location, Nsurlresponse * _nullable response, Nserror *_nullable Error) {        //Get Cache directoryNSString *cachepath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; //The song is stored in the cache directory and namedNSString *savepath =[CachePath Stringbyappendingpathcomponent:filename]; //get the path, open the terminal opening to remove Xxx.mp3 directory, you can visually see the download of MP3 fileNSLog (@"%@", Savepath); Nsurl*saveurl =[Nsurl Fileurlwithpath:savepath]; /*1.location is the temporary save path after the download, you need to move it to the location you want to save 2.move faster than copy (1). Because copy needs to generate a new file on disk, this The speed is very slow; (2). After the copy, but also to delete the temporary files, move this step is ok = (copy + remove) 3.move has two functions one is to move two is to rename*/Nserror*Saveerror; [[Nsfilemanager defaultmanager]moveitematurl:location tourl:saveurl Error:&Saveerror]; //If the error exists, the output        if(saveerror) {NSLog (@"%@", saveerror.localizeddescription); }        //Play[self playmusic];    }]; //Perform Tasks[Downloadtak resume];}- (void) didreceivememorywarning {[Super didreceivememorywarning];}@end

iOS Learning-10 downloads (3) nsurlsession Music Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.