#import"ViewController.h"
#import
#import
@interfaceViewController()
@end
@implementationViewController
{
AVAudioRecorder* audioRecorder;
AVAudioPlayer* player;
}
- (void)viewDidLoad {
[superviewDidLoad];
//? ? MPMediaPickerController* pickerController=[[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeMusic];
//? ? pickerController.prompt=@"請選擇音樂";
//? ? pickerController.allowsPickingMultipleItems=YES;
//? ? pickerController.delegate=self;
//? ? [self presentViewController:pickerController animated:YES completion:^{
//? ? }];
//? ? NSString* filePath=[[NSBundle mainBundle] pathForResource:@"include" ofType:@"mp4"];
//? ? NSURL* sourceNoviewURL=[NSURL fileURLWithPath:filePath];
//? ? AVAsset* moviewAsset=[AVURLAsset URLAssetWithURL:sourceNoviewURL options:nil];
//? ? AVPlayerItem* playItem=[AVPlayerItem playerItemWithAsset:moviewAsset];
//? ? AVPlayer* player=[AVPlayer playerWithPlayerItem:playItem];
//? ? AVPlayerLayer* playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
//? ? playerLayer.frame=self.view.layer.bounds;
//? ? playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;
//? ? [self.view.layer addSublayer:playerLayer];
//? ? [player play];
//獲取當(dāng)前應(yīng)用的音頻會話
AVAudioSession* audioSession=[AVAudioSessionsharedInstance];
//設(shè)置音頻類別导披,playAndRecored即可播放剿另,也可錄制
[audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[audioSessionsetActive:YESerror:nil];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSString*)documentsPath{
if(!_documentsPath) {
NSArray* searchPaths=
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
_documentsPath=[searchPathsobjectAtIndex:0];
}
return_documentsPath;
}
//22050采樣率
//8 16 24樣點(diǎn)
//雙聲道2
- (IBAction)player:(id)sender {
}
- (IBAction)recorder:(id)sender {
if(audioRecorder!=nil&&audioRecorder.isRecording) {
[audioRecorderpause];
}
else{
NSString* destinationString=[[selfdocumentsPath]
stringByAppendingPathComponent:@"sound.wav"];
NSURL* destinationURL=[NSURLfileURLWithPath:destinationString];
NSMutableDictionary*recordSettings=[[NSMutableDictionaryalloc]init];
//音頻格式
[recordSettingssetObject:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
//采樣率
[recordSettingssetObject:[NSNumbernumberWithFloat:22050]forKey:AVSampleRateKey];
//通道數(shù)
[recordSettingssetObject:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];
//樣點(diǎn)位數(shù)
[recordSettingssetObject:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];
//采用高位優(yōu)先的記錄格式
[recordSettingssetObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];
//采樣信號采用浮點(diǎn)數(shù)
[recordSettingssetObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsFloatKey];
//初始化
audioRecorder=[[AVAudioRecorderalloc]initWithURL:destinationURLsettings:recordSettingserror:nil];
audioRecorder.delegate=self;
[audioRecorderrecord];
}
}
//-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{
//? ? MPMusicPlayerController* player=[[MPMusicPlayerController alloc]init];
//? ? [player setQueueWithItemCollection:mediaItemCollection];
//? ? [player play];
//}
//-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{
//
//}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end