音频播放\录音

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
- (IBAction)playLocation:(UIButton *)sender;
- (IBAction)playOnline:(UIButton *)sender;
- (IBAction)recordVoice:(UIButton *)sender;
- (IBAction)playRecordVoice:(UIButton *)sender;
@property (nonatomic,strong) MPMoviePlayerController * player;
@property (nonatomic,strong) MPMoviePlayerViewController * mpMoviePlayer;
@property (nonatomic,strong) AVAudioRecorder * recorder;
@property (nonatomic,strong) NSString * recordVoiceFilePath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.recordVoiceFilePath = [NSTemporaryDirectory() stringByAppendingString:@"TmpFile.aiff"];
    NSLog(@"=======%@",self.recordVoiceFilePath);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playLocation:(UIButton *)sender {
    // 本地
    
    [self.player stop];
    NSString * path = [[NSBundle mainBundle] pathForResource:@"春天里" ofType:@"mp3"];
    NSURL * url = [NSURL fileURLWithPath:path];
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.player play];



    
}

- (IBAction)playOnline:(UIButton *)sender {
    // 在线
    [self.player stop];
    NSString * path = [NSString stringWithFormat:@"http://202.204.208.83/gangqin/download/music/02/03/02/Track08.mp3"];
    NSURL * url = [NSURL URLWithString:path];
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.player play];


}

- (IBAction)recordVoice:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        // 录音
        [sender setTitle:@"暂停" forState:UIControlStateNormal];
        self.recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:self.recordVoiceFilePath] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:nil];
        [self.recorder prepareToRecord];
        [self.recorder record];
    } else {
        [sender setTitle:@"录音" forState:UIControlStateNormal];
        [self.recorder stop];

    }
    
    
}

- (IBAction)playRecordVoice:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        // 播放录音
        [sender setTitle:@"暂停" forState:UIControlStateNormal];
        
        [self.player stop];
        NSURL * url = [NSURL fileURLWithPath:self.recordVoiceFilePath];
        self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [self.player play];

        
    } else {
        [sender setTitle:@"播放录音" forState:UIControlStateNormal];
        [self.player stop];

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值