搜尋此網誌

2012年10月10日 星期三

MPMoviePlayerViewController VS MPMoviePlayerController


老實說這篇文章轉貼過來後發現在
MPMoviePlayerViewController 這邊有問題
我還是得用addsubview才呈現的出來(也許是個人問題)

原文如下:

MPMoviePlayerViewController VS MPMoviePlayerController


MPMoviePlayerViewController
MPMoviePlayerController
版本支持
Available in iOS 3.2 and later.
Available in iOS 2.0 and later.(多数属性支持3.2)
大小
只支持全屏播放  如果addsubview 不支持横竖屏
可全屏也可自己设置frame
调用

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
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
常用方法:
AudioServicesCreateSystemSoundID(CFURLRef                    inFileURL,                                 SystemSoundID    *outSystemSoundID)

AudioServicesPlaySystemSound(SystemSoundIDinSystemSoundID)
- (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);

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

3 MPMusicPlayerController


player = [MPMusicPlayerController applicationMusicPlayer];

MPMediaItemCollection *_mediaCollection = [[MPMediaItemCollectionalloc]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;
        }
    }
}

沒有留言:

張貼留言