Question

in my project i have Url of audio...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?c22209f7da1bd60ad42305bae71896761f6cd1f4d6c29bf401ef7e97b90c3d5939b57f5479b37ad4d41c48227740d7ba8b5f79b76aa8d6735b8f87c781f44fb019762a2903fe65aeafb30d90cb56b385e27cd770cc9f9d60e5ea3f130da6ad3db728ff7e24a09fab9f351120810ee91f78f1e94fcabc98aabb37d4248358f6da4a0fa7a74fe8c89da2a768&fsig=cd06c325fc41b6f8edb0409011e8839c&id=1269403107&stream=1&ts=1395489600.0"];


    myData = [NSData dataWithContentsOfURL:myUrl];



}




- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];


    // Dispose of any resources that can be recreated.
}

@end

in above code how can convert mydata into one array????? or how can i download this audio and store in phone memory??

Was it helpful?

Solution

You can store your audio data to device memory as like below

myData = [NSData dataWithContentsOfURL:myUrl];

[myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"] atomically:YES];

To play an audio file that you saved in device :

Add AudioToolbox.framework and AVFoundation.framework to your project build

In .h file

#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *player;

In .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;

    }

}

Thanks!

OTHER TIPS

Your code for downloading the file has several problems. First, as you say, it will download the file every time. Second, you are using a synchronous download call. That code will lock up the app's user interface until the download completes or fails. If there is a network problem that could take up to 2 minutes. Rule of thumb: Don't EVER download synchronously.

You should take a look at the NSURLConnection method sendAsynchronousRequest:queue:completionHandler:. That method will let you download a file asynchronously and execute a block of code once the download is complete.

If you want to be able to play your sound quickly, it's a good idea to move the code to download the file into your applicationDidFinishLaunching:withOptions: code in the app delegate. Put code there that checks to see if the file exists, and starts downloading it if not using sendAsynchronousRequest:queue:completionHandler:. Then, by the time you need to play the sound, it should be loaded. (The download might have failed or not be completed, so you do still need to check if it is available at the time you need to play it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top