[英]AVAudioPlayer not playing sounds only on iPhone 4S
一年以來,我在商店里有一個應用程序,幾乎沒有擁有iPhone 4S的人告訴我,他們的設備上沒有聲音播放。 (無論是耳機還是內置揚聲器,他們都聽不到任何聲音)
我仍然在真實設備(iPhone 3G,3GS,4,ipod 1G,2G,3G,4G,ipad 1,2,3)上進行了測試,而且一切似乎都是正確的。 聲音正在播放。 (已在各種OS上測試: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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.