iOS 学习总结----音频/视频处理

来源:互联网 发布:防网络诈骗班会总结 编辑:程序博客网 时间:2024/04/29 21:06

音频:
1.AVAudioPlayer:使用简单,但是只能播放本地
//获取本地mp3链接
NSString*musicPath = [[NSBundlemainBundle]pathForResource:@"music.mp3"ofType:nil];
//NSURL *url = [NSURL URLWithString:musicPath];转换网络连接
   
//转换本地链接
NSURL*url = [NSURLfileURLWithPath:musicPath];
   
_audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];
_audioPlayer.delegate= self;
[_audioPlayerprepareToPlay];

2.AVPlayer 既可以播放流媒体,也可以播放本地
 NSString*str = @"http://www.soge8.com/1424215157/e4eaa401acb097ad2745efe7f8213352.mp3";
   _player= [[AVPlayeralloc]initWithURL:[NSURLURLWithString:str]];


后台播放音频:
1.在后台播放音乐(按下home键或锁屏情况下):
//后台任务: 音频、定位(耗电量大)、VOIP(网络电话)
//1、在plist文件中设置Required background modes  Voide
   //2、将音频设置为支持后台
    [[
AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:nil];
   
   
NSString *str = [[NSBundlemainBundle]pathForResource:@"music.mp3"ofType:nil];
   
NSURL *url = [NSURLfileURLWithPath:str];
   
_player = [[AVPlayeralloc]initWithURL:url];
    [_playerplay];

在视图将要出现时,设置第一响应者和远程控制:
//成为第一响应者
    [selfbecomeFirstResponder];
   
   
//让应用程序接收远程控制器事件
    [[
UIApplicationsharedApplication]beginReceivingRemoteControlEvents];
   
视图消失时取消第一响应者,关闭远程控制:
 //取消第一响应者
    [
selfresignFirstResponder];
   
//关闭远程控制
    [[
UIApplicationsharedApplication]endReceivingRemoteControlEvents];
   
锁屏时显示的信息:
 //设置专辑图
   
MPMediaItemArtwork *artWork = [[MPMediaItemArtworkalloc]initWithImage:[UIImageimageNamed:@“专辑图片名"]];
   
   
NSDictionary *dic =@{
                         
MPMediaItemPropertyAlbumTitle:@歌曲名",
                         
MPMediaItemPropertyArtist :@歌手名",
                         
MPMediaItemPropertyArtwork:artWork
                         
};
   
//锁屏时显示信息
    [[
MPNowPlayingInfoCenterdefaultCenter]setNowPlayingInfo:dic];
   

//选择线控事件:锁屏时可点击暂停/开始 /下一首(按钮)
- (void)remoteControlReceivedWithEvent:(UIEvent*)event{ 
if(event.type== UIEventTypeRemoteControl) {
       
switch (event.subtype) {
           
case UIEventSubtypeRemoteControlPlay:
               [
_player play];
               
break;
           
case UIEventSubtypeRemoteControlPause:
                [
_player pause];
               
break;
           
case UIEventSubtypeRemoteControlNextTrack:
               
NSLog(@"下一首");
               
break;
           
case UIEventSubtypeRemoteControlPreviousTrack:
               
NSLog(@"上一首");
               
break;
               
           
default:
               
break;
        }
    }

 } 

视频:
 1.使用MPMoviePlayerController创建视频播放器,设置网络视频连接
NSURL *url = [NSURLURLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];
   
//创建播放器
   
_moviePlayer = [[MPMoviePlayerControlleralloc]initWithContentURL:url];
   _moviePlayer.view.frame =CGRectMake(20,50, 320,300);

 [self.viewaddSubview:_moviePlayer.view];
 
2.利用通知来获取播放器的播放/暂停状态,
    [[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(playStateChange:)name:MPMoviePlayerPlaybackStateDidChangeNotificationobject:nil];

通知调用的方法:
- (void)playStateChange:(NSNotification *)notification{    
 MPMoviePlayerController *playerCtrl = notification.object;
   
if (playerCtrl.playbackState == MPMoviePlaybackStatePlaying) {
       
NSLog(@"播放");
    }
elseif(playerCtrl.playbackState == MPMoviePlaybackStatePaused){
       
NSLog(@"暂停");
    }
}


3. 定义一个BOO类型的变量_isPLay,通过取非改变改变播放状态
if (!_isPlay) {
        [
_moviePlayerplay];
    }
else{
        [
_moviePlayerpause];
    }
   
   _isPlay = !_isPlay;
如果不设置全屏,效果图如下:


通过模态视图弹出一个播放器,这个播放器是自动全屏的,不需要设置样式:
 NSURL *url = [NSURLURLWithString:@"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4"];
   
MPMoviePlayerViewController *playerViewCtrl = [[MPMoviePlayerViewControlleralloc]initWithContentURL:url];
   
    [
selfpresentViewController:playerViewCtrlanimated:YEScompletion:nil];
}
效果图如下:



0 0
原创粉丝点击