简体   繁体   中英

AVAudioPlayer not playing sounds only on iPhone 4S

I have an app on the store since one year, and few persons owning an iPhone 4S tell me that sounds are not playing on their devices. (they do not hear anything, either with earphones or with internal speaker)

I have still tested on real devices (iPhone 3G, 3GS, 4, ipod 1G, 2G, 3G, 4G, ipad 1,2,3) and all seems correct. Sounds are playing. (tested on various OS : iOS 3.1.2 --> 6.0, and it works also in simulator)

Unfortunately i have not myself an iphone 4S for testing, so it's very hard to know what's wrong with this particular device.

Could it be a problem with iPhone sound settings ?

But maybe it's a problem regarding my code ? So, here is the code i use to play sounds.

Is it correct ?

in the .h ...:

@private AVAudioPlayer* myPlayer; 

in the .m ...:

-(IBAction)playMySound{
playerPlay = YES; // it's a bool variable used at an other place in the code
NSString *path;
if((path = [[NSBundle mainBundle]  
       pathForResource:[NSString stringWithFormat:@"%d", idsound ] 
       ofType:@"m4a" 
       inDirectory:@"sounds"]))
    {
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error;
    myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (myPlayer == nil) {[myPlayer release]; return;}
    myPlayer.delegate = self;
    [myPlayer play];
    }
else return; 
}

Any advice would be appreciated !

You code is really weird:

Here you correctly create the player:

myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

The you check if it is nil , still correct.

if (myPlayer == nil) 

But then you call release on a nil object, which wil do nothing since the object is nil:

{[myPlayer release]; return;}

This should be a bit better:

NSError *error = nil;
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (myPlayer == nil) {
   NSLog(@"Error creating AVAudioPlayer: %@", error); 
   return;
}
myPlayer.delegate = self;
[myPlayer prepareToPlay];
[myPlayer play];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM