当用户选择它们并按下播放按钮时,我想要播放 23 个音乐文件(长度为 10-20 秒)。它可以在我的 iPad 和模拟器上找到,带或不带耳机。但在不带耳机的 iPhone 设备上,大约 80% 的文件听起来是乱码,音量很低,有些几乎无法辨认。我可以插入耳机,它们听起来不错。刚拔下耳机,声音又是乱码。
由于有些文件总是可以正常播放,因此我将我使用的所有原始 mp3 转换为采样率为 44100 的 AAC 格式。但是相同的文件播放正常,而大多数文件在 iphone 设备上播放时会失真。
我尝试了 AVAudioSession 类别的几种设置:播放、playAndRecord、环境。我尝试将文件存储在核心数据中并使用 initWithData 而不是 initWithContentsOfURL。谁能建议我还可以尝试什么?或者我如何才能找到我为什么会遇到这个问题的线索?
这是我的代码。有3个相关文件。ViewController 有一个按钮来播放选定的音乐文件,一个 AVPlaybackSoundController 有所有的音频逻辑,还有一个 Singleton 有一个带有文件名的数组。
我的 ViewController 通过 Nib 创建了一个 AVPlaybackSoundController。
IBOutlet AVPlaybackSoundController *avPlaybackSoundController;
@property (nonatomic, retain) AVPlaybackSoundController *avPlaybackSoundController;
- (IBAction) playButtonPressed:(id)the_sender
{
self.currentRhythmSampleIndex = arc4random() % [[CommonData sharedInstance].rhythmSampleArray count];
[self.avPlaybackSoundController playMusic:self.currentRhythmSampleIndex];
self.playBtn.hidden=YES;
self.pauseBtn.hidden=NO;
}
AVPlaybackSoundController.m
- (void) awakeFromNib
{
[self initAudioSession:AVAudioSessionCategoryPlayback];
[self initMusicPlayer];
}
- (void) initAudioSession:(NSString *const)audioSessionCategory
{
NSError* audio_session_error = nil;
BOOL is_success = YES;
is_success = [[AVAudioSession sharedInstance] setCategory:audioSessionCategory error:&audio_session_error];
if(!is_success || audio_session_error){
NSLog(@"Error setting Audio Session category: %@", [audio_session_error localizedDescription]);
}
[[AVAudioSession sharedInstance] setDelegate:self];
audio_session_error = nil;
is_success = [[AVAudioSession sharedInstance] setActive:YES error:&audio_session_error];
if(!is_success || audio_session_error){
NSLog(@"Error setting Audio Session active: %@", [audio_session_error
localizedDescription]);
}
}
- (void) initMusicPlayer
{
NSError* file_error = nil;
NSURL* file_url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"musicFile1" ofType:@"m4a"]
isDirectory:NO];
avMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file_url
error:&file_error];
if(!file_url || file_error){
NSLog(@"Error loading music file: %@", [file_error
localizedDescription]);
}
self.avMusicPlayer.delegate = self;
self.avMusicPlayer.numberOfLoops = 0xFF;
[file_url release];
}
-(void) playMusic:(NSUInteger)rhythmSampleIdx
{
CommonData *glob = [CommonData sharedInstance];
if (rhythmSampleIdx > [glob.rhythmSampleArray count])
return; // bug if we hit here
// if we were already playing it, just resume
if ((self.avMusicPlayer) && (rhythmSampleIdx == self.currentRhythmSampleIndex)){
[self.avMusicPlayer play];
}
else{ // re-init player with new rhythm
if (self.avMusicPlayer){
[self.avMusicPlayer stop];
[self.avMusicPlayer setCurrentTime:0.0];
}
NSError* error = nil;
RhythmSample *rhythmSample = [glob.rhythmSampleArray objectAtIndex:rhythmSampleIdx];
NSString* rhythm_filename = [self getRhythmFileName:rhythmSampleIdx];
NSURL* file_url = [[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle]
pathForResource:rhythm_filename ofType:@"m4a"] isDirectory:NO]autorelease];
self.avMusicPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:file_url error:&error]autorelease];
[self.avMusicPlayer prepareToPlay]; // shouldn't be needed since we are playing immediately
if(error){
NSLog(@"Error loading music file: %@", [error localizedDescription]);
}else{
self.avMusicPlayer.delegate = self;
self.avMusicPlayer.numberOfLoops = 0xFF; // repeat infinitely
self.currentRhythmSampleIndex = rhythmSampleIdx;
[self.avMusicPlayer play];
}
}
}