音频视频

原文地址:音频视频 作者:家明

视频播放 MediaPlayer.framework

MPMoviePlayerViewController VS MPMoviePlayerController

 

 

MPMoviePlayerViewController

MPMoviePlayerController

版本支持

Available in iOS 3.2 and later.

Available in iOS 2.0 and later.(多数属性支持3.2)

大小

只支持全屏播放  如果addsubview 不支持横竖屏

可全屏也可自己设置frame

调用

presentMoviePlayerViewControllerAnimated: 

 

dismissMoviePlayerViewControllerAnimated

addsubview:

属性

moviePlayer

[mMPVC. moviePlayer play];

 

 

BOOL shouldAutoplay

NSTimInterval initialPlaybackTime

 

NSTimeInterval duration

MPMovieControlStyle controlStyle

函数

 

initWithContentURL

 

shouldAutorotateToInterfaceOrientation

 

initWithContentURL

 

requestThumbnailImagesAtTimes:timeOption

 

thumbnailImageAtTime:timeOption

 

timedMetadata (4.0)

notification

MPMoviePlayerPlaybackDidFinishNotification

播放完成

 

MPMovieMediaTypesAvailableNotification

视频开始播放 (载入完成)

 

MPMoviePlayerNowPlayingMovieDidChangeNotification

视频开播 (开始载入)

 

MPMoviePlayerPlaybackStateDidChangeNotification

播放状态变化

判断 mediaPlayer.playbackState

 

MPMoviePlayerDidEnterFullscreenNotification

全屏 相关

 

 

 

另外 UIWebview播放方式 方便 但是对一些视频不支持 经测试有的流媒体的 使用 MPMoviePlayerController 可以播放  UIWebview不支持.

 

 MPMoviePlayerController 为单例4.0之后 可使用 AVPlayerLayer 的播放方式 addSubLayer实现多个视频同时播放

player1 = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]];

     player1.actionAtItemEnd = AVPlayerActionAtItemEndNone;

     [[NSNotificationCenter defaultCenter] addObserver:self

                                                          selector:@selector(playerItemDidReachEnd:)

                                                               name:AVPlayerItemDidPlayToEndTimeNotification

                                                            object:[player1 currentItem]];

     [player1 play];

 

playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:player1];

     playerLayer1.frame = self.bounds;       

          [self.layer addSublayer:playerLayer1];

 

 

参考:

MPMoviePlayerViewController

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/mpmovieplayerviewcontroller_class/Reference/Reference.html

MPMoviePlayerController

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

 

 

音频播放

AVFoundation.framework

 

 

System Sound Services

AVAudioPlayer 

MPMusicPlayerController

特点

播放短音效

播放任意长度音频

播放本地ipod音乐

版本

ios 2.0 or later

ios 2.2 or later

 

ios 3.0 later

属性

 

playing,duration,currentTime,

repeatMode,currentPlaybackTime,

numberOfLoops

常用方法:

AudioServicesCreateSy

stemSoundID(CFURLR inFileURL,

SystemSoundID *outSystemSoundID)

 

AudioServicesPlay

SystemSound(SystemSoundID inSystemSoundID)

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError*)outError;

 

-  (id)initWithData:(NSData *)dataerror:(NSError *)outError;

 

-  (BOOL)play;

-  (void)pause;

- (void)stop;

applicationMusicPlayer;

 

-  (void)setQueueWithQuery:(MPMediaQuery *)query;

 

-(void)play;

-(void)pause;

-(void)stop;

 

 各个播放器初始化方法:

1 System Sound Services

// 创建路径

NSString*dropMusicPath = [[NSBundle mainBundle] pathForResource:@"bird drop" ofType:@"wav"];

CFURLRefdropURL = (CFURLRef)[NSURL fileURLWithPath:dropMusicPath];

 

//创建系统声音

AudioServicesCreateSystemSoundID(dropURL, &birdDropID);

 

//播放音效

AudioServicesPlaySystemSound(birdDropID);

 

AVAudioPlayer 

 

// 设置音乐文件路径  

    path = [[NSBundle mainBundle] pathForResource:@"InTheMood" ofType:@"mp3"]; 

   

 

  // 设置 player    url为本地音频文件路径

 player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];

 

在线播放用data初始化

player = [[AVAudioPlayer alloc] initWithData:receiveData error:&err];

 

 [player play];

 

MPMusicPlayerController

 

 

player = [MPMusicPlayerController applicationMusicPlayer];

 

MPMediaItemCollection *_mediaCollection = [[MPMediaItemCollection alloc]initWithItems:SongList];

self.mediaCollection = _mediaCollection;

[_mediaCollection release];

 

[player setQueueWithItemCollection:mediaCollection];

[player setRepeatMode:MPMusicRepeatModeAll];

 

[player play];

 

 

二 音频后台播放:

 

(1) 设置 AVAudioSession 属性支持

NSError * err;

AVAudioSession*audioSession; 

audioSession = [AVAudioSession sharedInstance]; 

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 

[audioSession setActive:YES error:nil];

 

(2)  设置工程文件plist属性

三 系统后台控制音频播放

 

 

(1)  重写方法 canBecomeFirstResponder 返回YES

 

- (BOOL)canBecomeFirstResponder

{

    return YES;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self canBecomeFirstResponder];

}

 

(2) 实现接收RemoteControlEvents方法

 

- (void)viewDidAppear:(BOOL)animated {

 

    [super viewDidAppear:animated];

    [self becomeFirstResponder];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

 

}

 

- (void)viewWillDisappear:(BOOL)animated {

   

    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    [self resignFirstResponder];

   

}

 

(3)  在回调方法做相应处理

 

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

   

    if (receivedEvent.type == UIEventTypeRemoteControl)

    {

        switch (receivedEvent.subtype)

     {

            case UIEventSubtypeRemoteControlTogglePlayPause:

                                        

                break;

     case UIEventSubtypeRemoteControlPlay:

          break;

     case UIEventSubtypeRemoteControlPause:

          break;

           case UIEventSubtypeRemoteControlPreviousTrack:

                     break;

           case UIEventSubtypeRemoteControlNextTrack:

                break;

            default:

                break;

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值