视频(音频)播放 MoviePlayer

来源:互联网 发布:排水系统模拟软件 编辑:程序博客网 时间:2024/04/29 16:04

MoviePlayer  

首先,添加三个系统库AVFoundation.framework、MediaToolbox.framework、MediaPlayer.framework


#import "MainViewController.h"

#import <MediaPlayer/MediaPlayer.h>

#import <AVFoundation/AVFoundation.h>

@interfaceMainViewController ()

{

    MPMoviePlayerViewController *_pvc;

    MPMoviePlayerController *_mpc;

   AVAudioPlayer *_player;  //播放

   AVAudioRecorder *_recorder;  //录音

}

@end


@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    NSString *path = [[NSBundlemainBundle] pathForResource:@"test1"ofType:@"mp4"];

    _pvc = [[MPMoviePlayerViewControlleralloc] initWithContentURL:[NSURLfileURLWithPath:path]];

    [_pvc.moviePlayersetShouldAutoplay:YES];

    //[_pvc.moviePlayer play];

    

    //_mpc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];

    

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btn.frame =CGRectMake(100,100, 100, 40);

    [btn setTitle:@"button"forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

    

    UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btn2.frame =CGRectMake(100,140, 100, 40);

    [btn2 setTitle:@"点击录音" forState:UIControlStateNormal];

    [btn2 addTarget:selfaction:@selector(startRecord:)forControlEvents:UIControlEventTouchDown];

    [btn2 addTarget:selfaction:@selector(stop:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:btn2];

    

    

    UIButton *btn3 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btn3.frame =CGRectMake(100,180, 100, 40);

    [btn3 setTitle:@"播放"forState:UIControlStateNormal];

    [btn3 addTarget:selfaction:@selector(play)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn3];

    

   //录音

    //录音设置

    NSMutableDictionary *recordSetting = [[NSMutableDictionaryalloc]init];

    //设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM

    [recordSetting setValue:[NSNumbernumberWithInt:kAudioFormatMPEG4AAC]forKey:AVFormatIDKey];

    //设置录音采样率(Hz)如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

    [recordSetting setValue:[NSNumbernumberWithFloat:44100]forKey:AVSampleRateKey];

    //录音通道数  1 2

    [recordSettingsetValue:[NSNumbernumberWithInt:1]forKey:AVNumberOfChannelsKey];

    //线性采样位数  8162432

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

    //录音的质量

    [recordSetting setValue:[NSNumbernumberWithInt:AVAudioQualityHigh]forKey:AVEncoderAudioQualityKey];

    

    path = [NSHomeDirectory()stringByAppendingPathComponent:@"tmp/aaa"];

    _recorder = [[AVAudioRecorderalloc] initWithURL:[NSURLfileURLWithPath:path] settings:recordSettingerror:nil];

    //准备录音

    [_recorderprepareToRecord];

    

    // Do any additional setup after loading the view.

}

//开始录音

-(void)startRecord:(UIButton *)btn

{

    [_recorderrecord];

    [btn setTitle:@"录音中"forState:UIControlStateNormal];

}

//停止录音

-(void)stop:(UIButton *)btn

{

    [_recorderstop];

    [btn setTitle:@"点击录音" forState:UIControlStateNormal];

}

//播放

-(void)play

{

    NSString *path = [NSHomeDirectory()stringByAppendingPathComponent:@"tmp/aaa"];

   NSLog(@"%@",path);

    _player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLfileURLWithPath:path] error:nil];

    [_playerprepareToPlay];

    [_playerplay];

}


-(void)btnClick

{

    [selfpresentMoviePlayerViewControllerAnimated:_pvc];

    [_pvc.moviePlayerplay];

   // [_mpc play];

}





0 0
原创粉丝点击