How to read data from NSDocumentDirectory
Asked Answered
D

2

5

Hi everyone I am working on an application which records the user sound and save that to NSDocumentDirectory.The data files are saving well.But the problem is that i want to play those files.I am using the table view for this to populate it with the number of files in the NSDocumentDirectory.But the table shows only one cell the current sound file.I am doing mistake somewhere please help me out. I am using this code to play.

-(void)PlayAudio
{


    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSLog(@"strffdsa %@",documentsDirectory);
    NSURL *fileURL = [NSURL fileURLWithPath:recorderFilePath];

    player =    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    player.numberOfLoops = 0;
    //[fileURL release];

    [player play];
    [player setDelegate: self];

}

Where recorderFilePath is the path of file.The path is new everytime with the help of NSDate frmattor.But not getting how to populate the table view with all the files residing in the DocumentDirectory.

Please-2 help me...I am new to this technology.

Thanks in advance to all ........

Diao answered 18/11, 2010 at 12:18 Comment(0)
I
13

Use an enumerator to simplify iterating through the Documents directory to search for music files.

        //Use an enumerator to store all the valid music file paths at the top level of your App's Documents directory
        NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

        for (NSString *path in directoryEnumerator) 
        {
            //check for all music file formats you need to play
            if ([[path pathExtension] isEqualToString:@"mp3"] || [[path pathExtension] isEqualToString:@"aiff"] ) 
            { 
                [myMusicFiles addObject:path];         
            }
        }

        //the NSArray 'myMusicFiles' will be the datasource for your tableview
Indra answered 18/11, 2010 at 12:53 Comment(1)
Hey thanks NP it really helped me out and sorted my problem.You are really great buddy i appreciate this..Hope to help me in the future as well.Well it would be good if we can communicate even through email.My email address is [email protected]Diao
E
1

You need NSFileManager's contentsOfDirectoryAtPath:error: method to get an array of files in the request directory. Then iterate over that array to find the file name you're looking for.

Edgebone answered 18/11, 2010 at 12:36 Comment(1)
Hey thanks DarkDust ,NP sorted out the problem.Thanks again for helping me.Take care Bro.........Diao

© 2022 - 2024 — McMap. All rights reserved.