-1

I try to play audio from server url but nothing play, But i try to play audio from document directory path url and it play fine.

NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString:
[NSString stringWithFormat:@"%@",aSongURL]]];
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil];
abc.numberOfLoops=0;
[abc prepareToPlay];
[abc play];
Inder_iOS
  • 1,636
  • 1
  • 12
  • 20

2 Answers2

1

You can try this. Hope it helps.

AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
Rohan Bhale
  • 1,323
  • 1
  • 11
  • 29
  • @Inderpalsingh You can check if NSData instance is actually created. If it is created then please pass a pointer of NSError type to the error parameter. If after initWithData:error: NSError is pointer is not nil, you checkout what the error is. – Rohan Bhale May 25 '17 at 14:26
0

My answer is below

ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
@property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
@synthesize player;

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender {
    NSString *strAudioFileURL = @"dev.epixelsoft.co/love_app/audio/img_14957068361.caf";
    NSURL *soundFileURL = [NSURL fileURLWithPath:strAudioFileURL];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    [player play];
}
user3182143
  • 9,459
  • 3
  • 32
  • 39