简体   繁体   English

AVAudioPlayer无法仅在iPhone 4S上播放声音

[英]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. 一年以来,我在商店里有一个应用程序,几乎没有拥有iPhone 4S的人告诉我,他们的设备上没有声音播放。 (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. 我仍然在真实设备(iPhone 3G,3GS,4,ipod 1G,2G,3G,4G,ipad 1,2,3)上进行了测试,而且一切似乎都是正确的。 Sounds are playing. 声音正在播放。 (tested on various OS : iOS 3.1.2 --> 6.0, and it works also in simulator) (已在各种OS上测试:iOS 3.1.2-> 6.0,它也可在模拟器中使用)

Unfortunately i have not myself an iphone 4S for testing, so it's very hard to know what's wrong with this particular device. 不幸的是,我自己没有用于测试的iPhone 4S,因此很难知道这款特定设备的问题所在。

Could it be a problem with iPhone sound settings ? iPhone声音设置可能有问题吗?

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 ...: 在.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. 您检查是否为nil ,仍然正确。

if (myPlayer == nil) 

But then you call release on a nil object, which wil do nothing since the object is nil: 但是随后您对nil对象调用release ,因为该对象为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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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