I need to play a audio file(mp3) that i am fetching from json in a table view, on clicking row respective song path is passing to another UIView..on loading view it should play.
I know there is a lot of similar problem pasted over internet and i tried many ways nothing worked for me.
Here is the code snippet.
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryAmbient error:&error];
// audioFile--contains path--
// NSString *filePath = [[NSBundle mainBundle] pathForResource:audioFile ofType:@"mp3"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:audioFile];
NSURL *url = [NSURL fileURLWithPath:filePath];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if([self.player prepareToPlay])
{
NSLog(@"preparing");
}
else
{
NSLog(@"some error");
}
if([self.player play])
{
NSLog(@"playing");
}
NSLog(@"not playing");
NSLog(@"\n\nfile path-> %@\n\n url-> %@",filePath,url);
}
Where player is an object of AVAudioPlayer class
@property (strong, nonatomic) AVAudioPlayer *player;
From above, Values coming from NSLog() are(for a particular row)
file path-> /Users/ensignweb/Library/Application Support/iPhone Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine of Creation vs The Lie of Evolution.mp3
url-> file://localhost/Users/ensignweb/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine%20of%20Creation%20vs%20The%20Lie%20of%20Evolution.mp3
When i used NSBundle filePath was null so i commented it.Even at the end If Loop escaping to else.I guess there could be the problem.
Please help me out.