NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"mp3"];
if (musicPath)
{
if(TapSoud)
{
[TapSoud release];
TapSoud = nil;
}
TapSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
}
[TapSoud setDelegate:self];
[TapSoud prepareToPlay];
[TapSoud play];
NSString *musicPath1 = [[NSBundle mainBundle] pathForResource:@"roadrunner" ofType:@"mp3"];
if (musicPath1)
{
if(MatchSoud)
{
[MatchSoud release];
MatchSoud = nil;
}
MatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath1] error:nil];
}
[MatchSoud setDelegate:self];
[MatchSoud prepareToPlay];
NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:@"Wrong Answer" ofType:@"wav"];
if (musicPath2)
{
if(WrongMatchSoud)
{
[WrongMatchSoud release];
WrongMatchSoud = nil;
}
WrongMatchSoud = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath2] error:nil];
}
[WrongMatchSoud setDelegate:self];
[WrongMatchSoud prepareToPlay];
此代码可在模拟器中正常运行,但在设备上不产生声音。这里有什么问题?
最佳答案
最可能的问题是项目或资源复制阶段都缺少音频文件。模拟器会存储先前复制的资源,即使从项目中删除了该资源,直到将应用程序从模拟器中完全删除为止。
确保musicPath
,musicPath1
和musicPath2
不为空且有效。