iOS-音频和视频

一、视频

  1. 视频播放器需要添加MediaPlayer.framework.
  2. 视频播放主要提供了两个类,一个MPMoviePlayerController, 另一个是MPMoviePlayerViewController.
  3. 视频资源可以从本地获得,也可以从服务器获得。

MPMoviePlayerController播放本地视频资源(本地资源URL出现汉字不能编码)

NSString *path = [[NSBundle mainBundle]pathForResource:@"root" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
_playcontroller = [[MPMoviePlayerController alloc]initWithContentURL:url];
_playcontroller.view.frame = CGRectMake(0, 100, 320, 300);
[_playcontroller play];
[self.view addSubview:_playcontroller.view];

 MPMoviePlayerController播放网络资源(服务器为自己搭建,地址不可使用,连接服务器的URL,出现汉字需要编码)

str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]//编码方法

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];
_playcontroller.view.frame = CGRectMake(0, 100, 320, 300);
[_playcontroller play]; [self.view addSubview:_playcontroller.view];

 可以加一个通知,在视频播放完毕的时候发出通知,将视图从界面上移除。

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(removeMedia) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
-(void)removeMedia
{
    [_playcontroller.view removeFromSuperview];
}

 MPMoviePlayerViewController播放视频资源

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentViewController:player animated:YES completion:nil];

 效果图:

MPMoviePlayerViewController如何让视频横屏播放。

横屏播放,需要继承MPMoviePlayerViewController这个类。假设myMPMoviePlayerViewController继承这个类。

我们需要在myMPMoviePlayerViewController中实现两个方法。

-(BOOL)shouldAutorotate
{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return  UIInterfaceOrientationMaskLandscape;
}

 然后在实现下面代码:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];
myMPMoviePlayerViewController *player = [[myMPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentViewController:player animated:YES completion:nil];

 效果图为:

二、音频

音频的话,我们需要框架AVFoundation.framework

音频的话,一般需要使用进度条显示音频播放的进度。

@interface ViewController : UIViewController
{
    //音频播放器
    AVAudioPlayer * _audioPalyer;
    IBOutlet UISlider *_slider;
    float currentProgress;
    NSTimer *_timer;
}
- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;

@end
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _slider.value = 0.0;
}

- (IBAction)playButton:(id)sender
{
   _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(moveSlide) userInfo:nil repeats:YES];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp3"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    _audioPalyer = [[AVAudioPlayer alloc]initWithData:data error:nil];
    [_audioPalyer play];
    
    _slider.maximumValue = _audioPalyer.duration;
    [_slider addTarget:self action:@selector(changeSlide) forControlEvents:UIControlEventValueChanged];
}

-(void)changeSlide
{
    _audioPalyer.currentTime = _slider.value;
}

-(void)moveSlide
{
    _slider.value = _audioPalyer.currentTime;
}

- (IBAction)stopButton:(id)sender
{
    UIButton *button = (UIButton *)sender;
   
    if (button.tag == 0)
    {
        [_audioPalyer pause];
        [button setTitle:@"继续播放" forState:UIControlStateNormal];
        button.tag = 1;
        [_timer invalidate];
        _timer = nil;
    }
    else
    {
        [_audioPalyer play];
        [button setTitle:@"暂停" forState:UIControlStateNormal];
        button.tag = 0;
        [_slider addTarget:self action:@selector(changeSlide) forControlEvents:UIControlEventValueChanged];
    }
}

 效果图:

 

 
posted @ 2015-09-21 23:20  WCCW  阅读(897)  评论(0编辑  收藏  举报