Question

I'm integrating a third-party SDK for voice messaging. It saves audio files to Documents/audio/. I've peeked into the bundle on my test device using iFunBox and found the files are named like this: "b3abwx78...wav". There is no file extension, the end of the name is just "wav", rather than ".wav", which could be causing my issues. The SDK I'm using is proprietary and doesn't give me any control over how the files are saved.

The file is referenced by the SDK as "elem.text", which logs as something like this: "audio://a8c0cd0c4138d66cc32cbdfb3c213f3bwav"

So, what I'm trying to do is simply load that file into AVAudioPlayer and play it. Here's what I'm trying so far. "path" logs as null, as does URL, so obviously I've done something wrong:

-(void)playVoiceMail:(NSString *)filename
{
NSError *error;
NSString *escapedFilename = [filename stringByReplacingCharactersInRange:NSMakeRange(0, 8) withString:@""];
NSLog(@"escaped filename: %@", escapedFilename);

NSString *path = [[NSBundle mainBundle] pathForResource:escapedFilename
                                                 ofType:nil
                                            inDirectory:@"Documents/audio"];

NSLog(@"path: %@", path);

NSURL *URL = [NSURL URLWithString:path];

NSLog(@"URL: %@", URL);


AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:URL
                                                               error: &error];

NSLog(@"Player url: %@", player.url);

if (error)
    NSLog(@"ERROR: %@", error.localizedDescription);

if (!player.playing)
    if (![player play])
        NSLog(@"ERROR playing");
}
Was it helpful?

Solution

Were are you stocking the audio files ? if they are stored in your application bundle, try a code like this :

...
NSString *path = [[NSBundle mainBundle] pathForResource:escapedFilename
                                                 ofType:nil];
...

If they are stored in the Documents folder, you should access them like this :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"filename"];
NSURL *url ....

EDIT : If your url is not null, the then problem is With ARC, the AudioPalyer instance is released after the playVoiceMail method reaches the end. You should create a strong property like this :

@property (nonatomic, strong) AVAudioPlayer *player;

...
self.player= [[AVAudioPlayer alloc] initWithContentsOfURL:URL
                                                               error: &error];

[self.player play];
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top