2014-01-17 6 views
0

하고 싶습니다 파형을 그리십시오오디오를 재생 ??오디오 iOS를 재생하는 동안 파형을 그립니까?

나는 아래의 링크 또한 EZAudio으로 확인하고 audiograph 체크

Drawing waveform of audio on iOS without using AVAudioPlayer

유사한 방법으로 어떤 튜토리얼 또는 샘플 프로젝트가 있습니까 나에게 도움이되지 않습니다. [AVAssetReader 파형과 도면]의

+0

가능한 중복 (http://stackoverflow.com/questions/5032775/drawing-waveform-with-avassetreader) – pasawaya

+0

이 AVAssetReader 사용에 관한 질문 속는 아니다. 이 질문은 애셋이 재생되는 동안 실시간으로 파형 데이터를 읽는 것과 관련이있는 것으로 보입니다. 재생하지 않고 전체 파형을 표시하는 것이 아닙니다. – davidethell

답변

1
- (void)startForFilePath:(NSString *)filePath 
{ 
    [self setNeedsDisplay]; 
    _playTime = 0.0f; 
    _audioFilePath = filePath; 

    NSURL *url = [NSURL fileURLWithPath:_audioFilePath]; 
    NSError *error = nil; 
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
    if (_player == nil) 
    { 
     NSLog(@"player: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]); 
     UIAlertView *alert = 
     [[UIAlertView alloc] initWithTitle: @"" 
            message: [error localizedDescription] 
            delegate: nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 
    _player.numberOfLoops = 0; 
    [_player prepareToPlay]; 
    _player.meteringEnabled = YES; 

    NSTimeInterval duration = _player.duration; 
    _waveUpdateFrequency = duration/(SOUND_METER_COUNT/2); 
    [_player play]; 

    _timer = [NSTimer scheduledTimerWithTimeInterval:_waveUpdateFrequency target:self selector:@selector(updateMeters) userInfo:nil repeats:YES]; 
} 

- (void)updateMeters 
{ 
    [_player updateMeters]; 
    if ((![_player isPlaying]) || (_playTime >= 60.0f)) { //60s 
     [self stop]; 
     return; 
    } 
    _playTime = _player.currentTime; 

    [self addSoundMeterItem:[_player averagePowerForChannel:0]]; 
} 

#pragma mark - Sound meter operations 
- (void)shiftSoundMeterLeft 
{ 
    for(int i = 0; i < SOUND_METER_COUNT - 1; i++) { 
     _soundMeters[i] = _soundMeters[i+1]; 
    } 
} 

- (void)shiftSoundMeterRight 
{ 
    for(int i = SOUND_METER_COUNT - 1; i >= 0 ; i--) { 
     _soundMeters[i] = _soundMeters[i-1]; 
    } 
} 

- (void)addSoundMeterItem:(int)lastValue 
{ 
    if (_isMeterRight) { 
     [self shiftSoundMeterRight]; 
     [self shiftSoundMeterRight]; 
     _soundMeters[0] = lastValue; 
     _soundMeters[1] = lastValue; 
    } 
    else { 
     [self shiftSoundMeterLeft]; 
     [self shiftSoundMeterLeft]; 
     _soundMeters[SOUND_METER_COUNT - 1] = lastValue; 
     _soundMeters[SOUND_METER_COUNT - 2] = lastValue; 
    } 

    [self setNeedsDisplay]; 
} 

#pragma mark - Drawing operations 
- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    // Draw sound meter wave 
    [_meterWaveColor set]; 

    CGContextSetLineWidth(context, 1.0); 
    CGContextSetLineJoin(context, kCGLineJoinRound); 

    int baseLine = _frameRect.size.height/2; 
    int multiplier = 1; 
    int maxValueOfMeter = _frameRect.size.height/2 - 5; 
    for(CGFloat x = SOUND_METER_COUNT - 1; x >= 0; x--) 
    { 
     multiplier = ((int)x % 2) == 0 ? 1 : -1; 

     CGFloat y = baseLine + ((maxValueOfMeter * (MAX_LENGTH_OF_WAVE - abs(_soundMeters[(int)x])))/MAX_LENGTH_OF_WAVE) * multiplier; 

     if(x == SOUND_METER_COUNT - 1) { 
      CGContextMoveToPoint(context, x * (_frameRect.size.width/SOUND_METER_COUNT), y); 
      //CGContextAddLineToPoint(context, x * (_frameRect.size.width/SOUND_METER_COUNT) + 1, y); 
     } 
     else { 
      CGContextAddLineToPoint(context, x * (_frameRect.size.width/SOUND_METER_COUNT), y); 
      //CGContextAddLineToPoint(context, x * (_frameRect.size.width/SOUND_METER_COUNT) + 1, y); 
     } 
    } 

    CGContextStrokePath(context); 
} 
+0

헤더 파일을 놓친 것 같습니다. – Elsammak