gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:20:57 25 4
gpt4 key购买 nike

一年以来,我在商店里有一个应用程序,几乎没有拥有iPhone 4S的人告诉我,他们的设备上没有声音播放。 (无论是耳机还是内置扬声器,他们都听不到任何声音)

我仍在真实设备(iPhone 3G,3GS,4,ipod 1G,2G,3G,4G,ipad 1,2,3)上进行了测试,并且一切似乎都是正确的。声音正在播放。
(已在各种操作系统上测试:iOS 3.1.2-> 6.0,并且在模拟器中也可以使用)

不幸的是,我自己没有iPhone 4S可以进行测试,因此很难知道这款特定设备的问题所在。

iPhone声音设置可能有问题吗?

但这可能是关于我的代码的问题吗?
因此,这是我用来播放声音的代码。

这是正确的吗 ?

在.h ...中:

@private AVAudioPlayer* myPlayer; 

在他们中 ...:
-(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;
}

任何意见,将不胜感激 !

最佳答案

您的代码真的很奇怪:

在这里,您可以正确创建播放器:

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

您检查它是否是 nil,仍然正确。
if (myPlayer == nil) 

但是随后您在一个nil对象上调用 release,因为该对象为nil,所以它什么也不做:
{[myPlayer release]; return;}

这应该更好一些:
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];

关于iphone - AVAudioPlayer无法仅在iPhone 4S上播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12855753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com