#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];
}
}
音频播放\录音
最新推荐文章于 2025-08-08 20:09:48 发布