cannyboy
cannyboy

Reputation: 24446

AVAudioPlayer working in Simulator, but not on device

My mp3 playing code is:

NSError *error;
soundObject = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPathString] error:&error];
if (soundObject == nil) NSLog(@"%@", [error description]);
soundObject.delegate = self;
soundObject.numberOfLoops = 0;
soundObject.volume = 1.0;
NSLog(@"about to play");
[soundObject prepareToPlay];
[soundObject play];
NSLog(@"[soundObject play];");

The mp3 used to play fine, and it still does on the simulator. But not on the device.

I've recently added some sound recording code (not mine) to the software. It uses AudioQueue stuff which is slightly beyond me. Does that conflict with AVAudioPlayer? Or what could be the problem? I've noticed that as soon as the audiorecording code starts working, I can't adjust the volume on the device anymore, so maybe it blocks the audio playback?.


EDIT

The solution seems to be to put this in my code. I put it in applicationDidFinishLaunching:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

The first line allows both play and record, whilst the other lines apparently reroute things to make the volume louder.

All audio code is voodoo to me.

Upvotes: 18

Views: 5453

Answers (7)

Mujahed Ansari
Mujahed Ansari

Reputation: 469

I set following line into the code.

     do { 
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
        } catch {
            print ("There is an issue with this code!")
        }

Upvotes: 1

swapnali patil
swapnali patil

Reputation: 304

If you are running code on iOS 10 simulator it does not ask for security permissions but, If you are running code on ios 10 device then you need to add audio permission key in plist. Add Privacy - Microphone Usage Description key in plist.

Upvotes: 0

brendan
brendan

Reputation: 1735

I had sound in simulator, but not on device. It started working when I set AVAudioPlayer delegate

Upvotes: 0

John Erck
John Erck

Reputation: 9538

Make sure your iPhone's "Ringer Button" isn't showing the color red.

Upvotes: 15

user1046037
user1046037

Reputation: 17735

when something works on simulator and not on the device, the first thing to do is to restart the device and check. sometimes restarting the device has worked for me and saved a lot of time. Otherwise then you can start debugging

Upvotes: 0

iwasrobbed
iwasrobbed

Reputation: 46713

Some code I know works:

- (void)playOnce:(NSString *)aSound {

// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];  

// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = soundURL;
[soundURL release];

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];  
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded

[newAudio release]; // release the audio safely

[theAudio prepareToPlay];

// set it up and play
[theAudio setNumberOfLoops:0];
[theAudio setVolume: volumeLevel];
[theAudio setDelegate: self];
[theAudio play];

}

Upvotes: 2

Stephen Furlani
Stephen Furlani

Reputation: 6866

Here's a quick, simple thing to try:

I ran into a similar issue with images as well as sounds. The iPhone is cap-sensitive, even the file extensions, but the simulator is not. Check the NSURL object != nil in addition to soundObject.

You may also want to try [[NSBundle mainBundle] URLForResource:@"sound" ofType:@"mp3"]; and make sure the file is being copied into the .app under YourApp.app/Resources/

-Stephen

Upvotes: 1

Related Questions