You can try this small piece of code to play audio from an URL
Create a new helper calls and add the following code to PlayerHelper.h
@interface PlayerHelper : NSObject
{
AVPlayer *player;
}
+ (PlayerHelper*)shared;
- (BOOL)playAudio:(NSString*)urlPath;
- (void)stopPlayer;
- (void)pausePlayer;
- (void)seekTo:(float)time;
- (float)duration;
@end
Add following code to PlayerHelper.m
file
#import "PlayerHelper.h"
@implementation PlayerHelper
+ (PlayerHelper*)shared{
static PlayerHelper *helper = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[PlayerHelper alloc] init];
});
return helper;
}
- (void)stopPlayer{
if (player != nil) {
player = nil;
}
}
- (void)pausePlayer{
[player pause];
}
- (BOOL)playAudio:(NSString*)urlPath{
NSURL *URL = [NSURL URLWithString:urlPath];
NSError *error = nil;
if (player == nil){
player = [[AVPlayer alloc] initWithURL:URL];
}
if (error == nil) {
[player play];
return TRUE;
}
NSLog(@"error %@",error);
return FALSE;
}
- (void)seekTo:(float)time{
[player seekToTime:CMTimeMake(time, 1)];
}
- (float)duration{
return player.currentTime.value;
return 0;
}
@end
Usage,
NSString *url = @"https://isongs/media/rapon.mp3";
[[PlayerHelper shared] playAudio:url];