首页 > 移动平台 > 详细

iOS-音频和视频

时间:2015-09-21 23:58:36      阅读:451      评论:0      收藏:0      [点我收藏+]

一、视频

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

MPMoviePlayerController播放本地视频资源

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播放网络资源(服务器为自己搭建,地址不可使用)

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

 效果图:技术分享

 

iOS-音频和视频

原文:http://www.cnblogs.com/wangyaoguo/p/4826675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!