没有耳机,AVAudioPlayer在iPhone上听起来很乱

时间:2012-11-17 20:47:18

标签: avaudioplayer

当用户选择并按下播放按钮时,我想播放23个音乐文件(长度为10-20秒)。它可以在我的iPad和模拟器上找到,有或没有耳机。但是在没有耳机的iPhone设备上,大约80%的文件听起来乱码,音量很低,有些几乎不易读。我可以插入耳机,听起来很棒。一旦我拔下耳机,声音再次出现乱码。

由于有些文件总是很好用,我把我使用的所有原始mp3转换为AAC格式,采样率为44100.但相同的文件播放正常,而大多数文件在iphone设备上播放时失真

我为AVAudioSession类别尝试了几种设置:playback,playAndRecord,ambient。我已经尝试将文件存储在核心数据中并使用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];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我最终决定从不同的来源获取声音文件。新的声音文件播放正常。我仍然没有理论为什么旧的声音文件会与耳机一起玩,但没有它们。我真的不喜欢在不了解其工作原理的情况下找到解决方案。