How does iOS get the audio files downloaded in NSData?
Publish Time: 2014/12/12 7:48:06
In my project I have the Url of the audio ... Take a look at my code ...
ViewController.h
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface Viewcontroller: uiviewcontroller<avaudioplayerdelegate,nsurlconnectiondelegate>{Avaudioplayer *song; Nsmutabledata *filedata; NSData *mydata; } @property (nonatomic, retain) Avaudioplayer *song; @property (nonatomic,retain) NSData *mydata; @end
viewcontroller.m
#import "ViewController.h" @interface ViewController () @end @implementation viewcontroller@synthesize song,mydata;- (void) viewdidload{ [super viewdidload]; nsurl *myurl; myurl = [nsurl urlwithstring:@ "http://p0.bcbits.com/download/track/60523f9784a2912348eff92f7b7e21e8/mp3-128/1269403107? C22209f7da1bd60ad42305bae71896761f6cd1f4d6c29bf401ef7e97b90c3d5939b57f5479b37ad4d41c48227740d7ba8b5f79b76aa8d6735b8f87c78 1f44fb019762a2903fe65aeafb30d90cb56b385e27cd770cc9f9d60e5ea3f130da6ad3db728ff7e24a09fab9f351120810ee91f78f1e94fcabc98aabb 37d4248358f6da4a0fa7a74fe8c89da2a768&fsig=cd06c325fc41b6f8edb0409011e8839c&id=1269403107&stream=1 &ts=1395489600.0 "]; mydata = [nsdata datawithcontentsofurl:myurl";} - (void) didreceivememorywarning{ [super didreceivememorywarning]; // D ispose of any resources that can be recreated.} @end
In the above code how can the conversion be mydata to an array??? Or how can I download this audio and store it in the phone memory??
Workaround 1:
You can store device memory as the audio data like below
MyData = [NSData Datawithcontentsofurl:myurl]; [MyData writetofile:[nshomedirectory () stringbyappendingpathcomponent:@ "Documents/audio.mp3"] atomically:YES];
To play the audio files that you have in your device:
Add AudioToolbox.framework and AVFoundation.framework build for your project
In the. h file
#import <AudioToolbox/AudioToolbox.h> #import <avfoundation/avfoundation.h>avaudioplayer *player;
In the. m file
-(void) initplayer{ avaudiosession *audiosession = [avaudiosession sharedinstance]; uint32 audiorouteoverride = kaudiosessionoverrideaudioroute_speaker; audiosessionsetproperty (kaudiosessionproperty_overrideaudioroute,sizeof (audioRouteOverride),& Audiorouteoverride); nserror *err; [audiosession setcategory:avaudiosessioncategoryplayandrecord error:&err]; Audiosession.delegate = self; [audiosession setactive:yes error: &err]; player = [[avaudioplayer alloc]initwithcontentsofurl:[nsurl fileurlwithpath:[nshomedirectory () stringbyappendingpathcomponent:@ "Documents/audio.mp3"]] Error:nil];} -(void) playfile{ if (player. playing) { [player pause]; isPlaying = NO; } else { [player play]; isplaying = yes; }}
Thank you!
How does iOS get the audio files downloaded in NSData?