使用EZAudio播放器播放音频时出错

时间:2017-09-19 14:15:43

标签: objective-c ezaudio

我正在使用EZAudio库进行Wave表格,当玩家尝试播放音频时应用程序崩溃并将错误显示为日志

  

错误:无法从文件波形(-50)

中读取音频数据

我的代码已经发出,

.Where("Email=@0", dummyEmail)
//----------------------- Initialization ----------------------
-(void)initializesEZAudio
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error;
    [session setCategory:AVAudioSessionCategoryPlayback error:&error];
    if (error)
    {
        NSLog(@"Error setting up audio session category: %@", error.localizedDescription);
    }
    [session setActive:YES error:&error];
    if (error)
    {
        NSLog(@"Error setting up audio session active: %@", error.localizedDescription);
    }

    // Customizing the audio plot's look
    self.audioPlot.backgroundColor = [UIColor clearColor];
    self.audioPlot.color           = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    self.audioPlot.plotType        = EZPlotTypeBuffer;
    self.audioPlot.shouldFill      = YES;
    self.audioPlot.shouldMirror    = YES;

    NSLog(@"outputs: %@", [EZAudioDevice outputDevices]);


    self.player = [EZAudioPlayer audioPlayerWithDelegate:self];



    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    if (error)
    {
        NSLog(@"Error overriding output to the speaker: %@", error.localizedDescription);
    }

    //
    // Listen for EZAudioPlayer notifications
    //
    [self setupNotificationsForPlay];
    //[self setupNotificationsForRecord];

    /*
     Try opening the sample file
     */

    //[self.microphone startFetchingAudio];


}


- (void)       output:(EZOutput *)output
          playedAudio:(float **)buffer
       withBufferSize:(UInt32)bufferSize
 withNumberOfChannels:(UInt32)numberOfChannels
{
    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update plot, buffer[0] = left channel, buffer[1] = right channel
    });
}



- (void)setupNotificationsForPlay
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(audioPlayerDidChangeAudioFile:)
                                                 name:EZAudioPlayerDidChangeAudioFileNotification
                                               object:self.player];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(audioPlayerDidChangeOutputDevice:)
                                                 name:EZAudioPlayerDidChangeOutputDeviceNotification
                                               object:self.player];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(audioPlayerDidChangePlayState:)
                                                 name:EZAudioPlayerDidChangePlayStateNotification
                                               object:self.player];
}
//------------------- Delegates & Notification ------------------
  

// ----------------播放方法-------------------

- (void)audioPlayerDidChangeAudioFile:(NSNotification *)notification
{
    EZAudioPlayer *player = [notification object];
    NSLog(@"Player changed audio file: %@", [player audioFile]);
}

//------------------------------------------------------------------------------

- (void)audioPlayerDidChangeOutputDevice:(NSNotification *)notification
{
    EZAudioPlayer *player = [notification object];
    NSLog(@"Player changed output device: %@", [player device]);
}



- (void)audioPlayerDidChangePlayState:(NSNotification *)notification
{
    EZAudioPlayer *player = [notification object];
    NSLog(@"Player change play state, isPlaying: %i", [player isPlaying]);
}


- (void)openFileWithFilePathURL:(NSURL *)filePathURL
{

    _audioFile = [EZAudioFile audioFileWithURL:filePathURL];

    //    // Plot the whole waveform
    _audioPlot.plotType = EZPlotTypeBuffer;
    _audioPlot.shouldFill = YES;
    _audioPlot.shouldMirror = YES;
    __weak typeof (self) weakSelf = self;
    [self.audioFile getWaveformDataWithCompletionBlock:^(float **waveformData,
                                                         int length)
     {
         [weakSelf.audioPlot updateBuffer:waveformData[0]
                           withBufferSize:length];
     }];

    // Play the audio file
    [_player setAudioFile:_audioFile];
}


- (void)  audioPlayer:(EZAudioPlayer *)audioPlayer
          playedAudio:(float **)buffer
       withBufferSize:(UInt32)bufferSize
 withNumberOfChannels:(UInt32)numberOfChannels
          inAudioFile:(EZAudioFile *)audioFile
{
    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.audioPlot updateBuffer:buffer[0]
                          withBufferSize:bufferSize];
    });
}

- (void)drawBufferPlot
{
    self.audioPlot.plotType = EZPlotTypeBuffer;
    self.audioPlot.shouldMirror = NO;
    self.audioPlot.shouldFill = NO;
}

//------------------------------------------------------------------------------


- (void)drawRollingPlot
{
    self.audioPlot.plotType = EZPlotTypeRolling;
    self.audioPlot.shouldFill = YES;
    self.audioPlot.shouldMirror = YES;
}

//---------------- Implmentation ------------------

- (IBAction)btn_record_activities:(id)c {

    if (isPlayable) {
        if ([state isEqualToString:@"IDLE"]) {
            state=[NSMutableString stringWithFormat:@"RECORDING"];
            [_btn_record_activities setImage:[UIImage imageNamed:@"btn_recording.png"] forState:UIControlStateNormal];
            [_view_state setBackgroundColor:[UIColor redColor]];
            [_btn_state setImage:[UIImage imageNamed:@"state_recording_btn.png"] forState:UIControlStateNormal];

            [self performSelectorOnMainThread:@selector(setUpAudioRecorder) withObject:nil waitUntilDone:YES];
            recordingTimer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeAudioProgress) userInfo:nil repeats:YES];
            NSLog(@"recordingTimer %@",recordingTimer);

        }
        else if ([state isEqualToString:@"RECORDING"]) {
            _lbl_public.hidden=NO;
            _switch_public.hidden=NO;
            _btn_done.userInteractionEnabled =YES;
            state=[NSMutableString stringWithFormat:@"RECORDED"];
            [_btn_record_activities setImage:[UIImage imageNamed:@"btn_recorded.png"] forState:UIControlStateNormal];
            [_view_state setBackgroundColor:[UIColor whiteColor]];
            [_btn_state setImage:[UIImage imageNamed:@"state_redo_btn.png"] forState:UIControlStateNormal];
            [audioPlayer_ofstate stop];
            [audioRecorder stop];
//            [self playWave];
//            [self.player pause];

            for (int k=0; k<[arr_player_objects count]; k++) {
                [[arr_player_objects objectAtIndex:k] stop];
            }
        }
        else if ([state isEqualToString:@"RECORDED"]) {


            state=[NSMutableString stringWithFormat:@"PLAYING"];
            [_btn_record_activities setImage:[UIImage imageNamed:@"btn_playing.png"] forState:UIControlStateNormal];
            [_view_state setBackgroundColor:[UIColor whiteColor]];
            [_btn_state setImage:[UIImage imageNamed:@"new_state_listning_btn.png"] forState:UIControlStateNormal];

            //----------loading files to player for playing--------//
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sounds.wav", documentsDirectory]];
            //-------------------* Pass URL as Parameter *------------------

            NSLog(@"kAudioFileDefault %@",kAudioFileDefault);

            [self openFileWithFilePathURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sounds.wav", documentsDirectory]]];
            _player.delegate = self;

            NSError *error;
            audioPlayer_ofstate = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
            audioPlayer_ofstate.numberOfLoops = 0;
            audioPlayer_ofstate.delegate=self;
            audioPlayer_ofstate.volume=2.0;
            [audioPlayer_ofstate prepareToPlay];
            NSLog(@"Playing.......");
            seconds=0;
            minutes=0;
            hours=0;
            recordingTimer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeAudioProgress) userInfo:nil repeats:YES];

            [self playWave];
            [audioPlayer_ofstate play];

        }
        else if ([state isEqualToString:@"PLAYING"]) {
            [recordingTimer invalidate];
            seconds=0;
            minutes=0;
            hours=0;
            state=[NSMutableString stringWithFormat:@"RECORDED"];
            [_btn_record_activities setImage:[UIImage imageNamed:@"btn_recorded.png"] forState:UIControlStateNormal];
            [_view_state setBackgroundColor:[UIColor whiteColor]];
            [_btn_state setImage:[UIImage imageNamed:@"state_redo_btn.png"] forState:UIControlStateNormal];
            i=0;
            [audioPlayer_ofstate stop];
        }
        else{
            seconds=0;
            minutes=0;
            hours=0;
            [recordingTimer invalidate];
            //        [self initWaveViewMirror];
            state=[NSMutableString stringWithFormat:@"IDLE"];
            [_btn_record_activities setImage:[UIImage imageNamed:@"btn_ready_to_recording.png"] forState:UIControlStateNormal];
            [_view_state setBackgroundColor:[UIColor whiteColor]];
            [_btn_state setImage:[UIImage imageNamed:@"state_melody_btn.png"] forState:UIControlStateNormal];
        }

    }
    else{

    }
}

请尽可能解决问题。

谢谢&amp;此致

0 个答案:

没有答案