美文网首页
播放音乐和视频及录音

播放音乐和视频及录音

作者: nalis风 | 来源:发表于2016-07-20 21:44 被阅读52次

    #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

    相关文章

      网友评论

          本文标题:播放音乐和视频及录音

          本文链接:https://www.haomeiwen.com/subject/hwzvjttx.html