播放音乐和视频及录音

#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];

//获取当前应用的音频会话

AVAudioSession* audioSession=[AVAudioSessionsharedInstance];

//设置音频类别,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样点

//双声道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];

//通道数

[recordSettingssetObject:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];

//样点位数

[recordSettingssetObject:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];

//采用高位优先的记录格式

[recordSettingssetObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];

//采样信号采用浮点数

[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

你可能感兴趣的:(播放音乐和视频及录音)