【发布时间】:2013-10-06 07:42:28
【问题描述】:
我已经尽我所能尝试让我的应用播放 mp3。我有一些 2-3 秒的 mp3 可以作为音效播放,但我正在尝试播放 15 秒的 mp3,但没有任何运气。
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"test" withExtension: @"mp3"] error:&error];
if (error) {
NSLog(@"Error creating audio player: %@", [error userInfo]);
} else {
if([audioPlayer play] == FALSE) {
NSLog(@"Fail %@", error);
}
}
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 15.0 target: self
selector: @selector(stopMusic) userInfo: nil repeats: NO];
我怀疑计时器与它有什么关系,但我想我会包括它以防万一。基本上我的错误检查根本没有被触发。它“应该”可以正常播放,但没有声音发出,也没有显示错误。
我尝试了其他一些方法来尝试使其正常工作,并且我尝试过不使用计时器。我找不到像样的指南。我不需要完整的媒体播放器,只需要 UIScreenViewController 的一些背景音乐。
我还尝试了以下代码,它适用于短 MP3,而不适用于我想播放的较长 MP3。
NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
请帮忙,我已经花了好几个小时试图对此进行排序。似乎苹果每次发布操作系统时都会重新调整音频。
【问题讨论】:
-
你的播放器认为它正在播放吗?在 if 块之后调用并记录
[audioPlayer playing]。也许与 mp3 文件有关?你的代码对我来说很合适。 -
它打印的是真的,所以它认为它正在播放。