Shikha Sharma
Shikha Sharma

Reputation: 469

Download Audio file from URL and play it in iOS App

I have an URL which I get from JSON response, When I hit the URL in browser the file is downloaded, Now am wondering how can I play such URL which is a Download URL.I've been looking around many post But none of them could help me.

type of url am getting : http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1

Below is the code I tried.

  NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
  NSLog(@"url:%@",audio);

  NSURL  *url = [NSURL fileURLWithPath:audio];
  NSData *soundData = [NSData dataWithContentsOfURL:url];
  NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                                                           fileURLWithPath:filePath] error:&error];
    [_audioPlayer play];
    NSLog(@"error %@", error);

Upvotes: 2

Views: 3688

Answers (5)

Jessicardo
Jessicardo

Reputation: 869

Just use AVPlayer instead of AVAudioPlayer

@property (nonatomic, strong) AVPlayer *player;

AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:@"https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a"]];

    // Create player item
    
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];

self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    
[self.player seekToTime:kCMTimeZero];

[self.player play];

Upvotes: 0

Shikha Sharma
Shikha Sharma

Reputation: 469

Thank You Dipankar Das for you help. I am posting the whole solution here. which worked for me absolutely.

    NSString *audio=@"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
    NSURL  *url = [NSURL URLWithString:audio];
    NSData *soundData = [NSData dataWithContentsOfURL:url];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];

    // get the file from directory
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [pathArray objectAtIndex:0];
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundUrl;
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
    {
        soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
    }

    // plau audio file
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [_audioPlayer prepareToPlay];
    [_audioPlayer play];
    [_audioPlayer setVolume:5.0];

Upvotes: 2

Habib Ali
Habib Ali

Reputation: 280

Here is the mistake

NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
NSURL  *url = [NSURL fileURLWithPath:audio];

I assume that it is a remote url and you cannot treat it as a file url. So you must use

NSURL  *url = [NSURL urlWithString:audio];

Upvotes: 0

Dipankar Das
Dipankar Das

Reputation: 700

Try this:

    //download your audio file
    NSString *urlString = @"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
    NSURL  *url = [NSURL fileURLWithPath:urlString];
    NSData *soundData = [NSData dataWithContentsOfURL:url];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                               NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];

    // get the file from directory
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [pathArray objectAtIndex:0];
    NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundUrl;
    if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
    {
        soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
    }

    // play audio file
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [audioPlayer prepareToPlay];
    [audioPlayer play];

You should download your audio file asynchronously.

Upvotes: 2

Marek Staňa
Marek Staňa

Reputation: 564

try use AVPlayer or AVPlayerViewController, you can use direct URL to play mp3 files

even with local file as you will first download it to device, then play from local URL

do not forget that object AVPlayer has to be as property of your Class not local object because of auto-releasing, so declare your player variable in class and set in function not just locally in function and try to play ...

[update]

more details how to do it -> How to play video with AVPlayerViewController (AVKit) in Swift

Upvotes: 2

Related Questions