Home AVAudioPlayer not playing sound from temp file path
Reply: 1

AVAudioPlayer not playing sound from temp file path

Niall Kiddle
Niall Kiddle Published in 2018-01-10 12:46:18Z

I have saved a recording to my Temp directory and can verify this using iExplorer

The file path structure is TempFolder/UserID/SoundName001.wav The bit I save to the database is UserID/SoundName001.wav

Here is the code I use to play the file

@property (nonatomic, strong) AVAudioPlayer *player;

-(void) playSound: (NSIndexPath*)recordingIndex {
    NSArray *recording = [recordingArray objectAtIndex:recordingIndex.item-1];
    NSString *filePath = [NSString stringWithFormat:@"%@%@",NSTemporaryDirectory(),[recording objectAtIndex:2]];
    NSURL *recorderURL = [[NSURL alloc] initFileURLWithPath: filePath];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorderURL error:nil];
    [self.player prepareToPlay];
    [self.player setMeteringEnabled:YES];
    [self.player setDelegate:self];
    [self.player play];

I set a breakpoint and recorderURL returns the following file:///private/var/mobile/Containers/Data/Application/0883D0DC-B1FD-4D06-A7BB-EBD9EEF5D607/tmp/1/Dog001.wav

When I call this method, nothing happens, no crash, no sound... Am I missing something obvious here?

Pranay Reply to 2018-01-10 19:08:45Z

Did you check the silent switch on your device? I think AVPlayer doesn't play sound if the silent switch is turned on. Try putting this line of code:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

in your viewDidLoad or before [self.player play]; and see if the audio plays.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298463 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO