ios錄音、音頻播放功能

來源:互聯網
上載者:User

標籤:

#import <AVFoundation/AVFoundation.h>

{

    NSInteger   _timeCount;
    NSTimer     *_timer;

}

@property(nonatomic,retain)AVAudioPlayer  *audioPlayer;
@property(nonatomic,retain)AVAudioRecorder *recorder;
@property(nonatomic,assign)NSInteger      playTag;

 

/**
 *  建立錄音機
 */
-(AVAudioRecorder *)recorder{
    if (!_recorder) {
        NSURL *url = [self filePath];
        NSDictionary *setting = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey,[NSNumber numberWithInt:16],AVEncoderBitRateKey,[NSNumber numberWithInt:2],AVNumberOfChannelsKey,[NSNumber numberWithFloat:44100.0],AVSampleRateKey,nil];
        NSError *error=nil;
        _recorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
        _recorder.delegate=self;
        _recorder.meteringEnabled=YES;
        if (error) {
            NSLog(@"錄音錯誤:%@",error.localizedDescription);
        }
        [_recorder prepareToRecord];
    }
    return _recorder;
}


//返迴文件的路徑
-(NSURL *)filePath
{
    NSString *strPath=[NSTemporaryDirectory() stringByAppendingPathComponent:@"aaa.caf"];
     NSLog(@"%@-----------",strPath);
    NSURL *url=[NSURL fileURLWithPath:strPath];
    return url;
}

//彈出錄音頁面

-(void)addAudioView{
    self.navigationItem.rightBarButtonItem.enabled = NO;
    _backView = [[ UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _backView.alpha = 0.5;
    _backView.backgroundColor = [ UIColor blackColor];
    [self.view addSubview:_backView];
    
    _audioView  = (AudioRecorderAlterView *)[[[NSBundle mainBundle]loadNibNamed:@"AudioRecorderAlterView" owner:nil options:nil]lastObject];
    _audioView.frame = CGRectMake(0, 64, 200, 200);
    _audioView.center = CGPointMake(self.view.center.x, 200);
    [_audioView.EndBtn addTarget:self action:@selector(pulishVedio) forControlEvents:UIControlEventTouchUpInside];
    [_audioView.CannelBtn addTarget:self action:@selector(cancleBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_audioView];
}

//點擊錄音按鈕

- (IBAction)vedioBtnClick:(id)sender {
    if ([_audioPlayer isPlaying]) {
        [_audioPlayer stop];
        UIImageView *animationImageView_last = (UIImageView  *)[self.view viewWithTag:_playTag  + 10000];
        [animationImageView_last stopAnimating];
    }

    if ([self canRecord]) {
        [self addAudioView];
        if (![self.recorder isRecording]) {
            [self.recorder record];
        }
        [self addTimer];
    }
}

ios錄音、音頻播放功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.