Search code examples
iosavplayernsdocumentdirectory

AVAudioPlayer Doesn't play the songs inside the folders?


I used this code to play songs it is not playing the songs inside the folders or folders of folders except the Documents directory.

NSString *songname=[NSString stringWithFormat:@"%@",songArray[songIndex]];
NSLog(@"songname:%@",songname);

NSString* saveFileName = songname;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];

Solution

  • NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"github.mp4"];
    NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path];
    avPlayer = [AVPlayer playerWithURL:url1] ;
    [self.avplayer play];