ユーザーがそれらを選択して再生ボタンを押したときに再生したい23の音楽ファイル(長さ10〜20秒)があります。それは私のiPadとシミュレーターで、イヤホンの有無にかかわらず動作します。しかし、イヤホンのないiPhoneデバイスでは、ファイルの約80%が文字化けし、音量が小さく、ほとんど判読できないものもあります。イヤホンを差し込むことができます、そしてそれらはうまく聞こえます。イヤホンのプラグを抜くとすぐに、音が再び文字化けします。
一部のファイルは常に正常に再生されるため、使用していた元のmp3をすべてサンプルレート44100のAAC形式に変換しました。ただし、同じファイルは正常に再生されますが、ほとんどのファイルはiphoneデバイスで再生すると歪んでしまいます。
AVAudioSessionカテゴリのいくつかの設定を試しました:再生、playAndRecord、アンビエント。ファイルをコアデータに保存し、initWithContentsOfURLの代わりにinitWithDataを使用してみました。誰かが私が他に何を試みるかもしれないか提案できますか?または、なぜこの問題が発生しているのかを知る手がかりを得るにはどうすればよいですか?
これが私のコードです。関連するファイルは3つあります。選択した音楽ファイルを再生するためのボタンを備えたViewController、すべてのオーディオロジックを備えたAVPlaybackSoundController、およびファイル名の配列を備えたSingleton。
私のViewControllerは、ニブを介して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];
}
}
}