创建Phonegap / Cordova IOS插件以使用AVPlayer启动视频

时间:2013-06-24 19:01:10

标签: ios video cordova phonegap-plugins avplayer

虽然Phonegap plugin的这个示例适用于使用MPMoviePlayerController播放视频,但我正在寻找类似的方法来使用AVPlayer启动视频(仍然使用Phonegap插件)?

我需要这样的东西,但我无法弄清楚如何调用播放器并获得插件启动的播放器视图。

AVPlayerViewPlay是CDVPlugin调用:

//  AVPlayerViewPlay.h
//
#import <Cordova/CDV.h>
#import <UIKit/UIKit.h>

#import "AVPlayerView.h"
#import "AVPlayerViewController.h"

@class AVPlayerViewController;

@interface AVPlayerViewPlay : CDVPlugin 

- (void)play:(CDVInvokedUrlCommand*)command;

@end

//
//  AVPlayerViewPlay.m
//

#import <Cordova/CDV.h>
#import <AVFoundation/AVFoundation.h>

#import "AVPlayerViewPlay.h"
#import "AVPlayerViewController.h"

@implementation AVPlayerViewPlay

- (void)play:(CDVInvokedUrlCommand*)command {
  CDVPluginResult* pluginResult = nil;
  NSString* javaScript = nil;


    //!!  WHAT DO I NEED TO DO HERE TO START the AVPlayerView and play the 2 videos AS A PHONEGAP PLUGIN??
    //self.playbackViewController = [[AVPlayerViewController alloc] init];
    //[self.playbackViewController setURL:URLWithString:movie];


  pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"PLUGIN WORKS "];
  javaScript = [pluginResult toSuccessCallbackString:command.callbackId];
  [self writeJavascript:javaScript];
}

- (void)dealloc {
  [super dealloc];
}
@end

AVPlayerView是AVPlayer视图:

//
//  AVPlayerView.h
//

#import <UIKit/UIKit.h>

@class AVPlayer;

@interface AVPlayerView : UIView

@property (nonatomic, retain) AVPlayer* player;

- (void)setPlayer:(AVPlayer*)player;
- (void)setVideoFillMode:(NSString *)fillMode;

@end

//
//  AVPlayerView.m
//

#import "AVPlayerView.h"
#import <AVFoundation/AVFoundation.h>



@implementation AVPlayerView

+ (Class)layerClass
{
    return [AVPlayerLayer class];
}

- (AVPlayer*)player
{
    return [(AVPlayerLayer*)[self layer] player];
}

- (void)setPlayer:(AVPlayer*)player
{
    [(AVPlayerLayer*)[self layer] setPlayer:player];
}

- (void)setVideoFillMode:(NSString *)fillMode
{
    AVPlayerLayer *playerLayer = (AVPlayerLayer*)[self layer];
    playerLayer.videoGravity = fillMode;
}

@end

AVPlayerViewController是AVPlayer控制器:

//
//  AVPlayerViewController.h
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@class AVPlayerViewController;

@interface AVPlayerViewController : UIViewController {
  AVQueuePlayer *queuePlayer;
  IBOutlet AVPlayerViewController  *mPlaybackView;
}
@property (readwrite, retain) AVQueuePlayer *queuePlayer;
@property (nonatomic, retain) IBOutlet AVPlayerViewController *mPlaybackView;

- (void)observeValueForKeyPath:(NSString*) path ofObject:(id)object change:(NSDictionary*)change context:(void*)context;
@end

//
//  AVPlayerViewController.m
//

#import "AVPlayerViewController.h"
#import "AVPlayerView.h"

static void *AVPlayerDemoPlaybackViewControllerStatusObservationContext = &AVPlayerDemoPlaybackViewControllerStatusObservationContext;

@implementation AVPlayerViewController
@synthesize mPlaybackView, queuePlayer;

- (void)dealloc
{
  [queuePlayer release];
  [mPlaybackView release];
  [super dealloc];
}

- (void)viewDidLoad
{
  [super viewDidLoad];

  NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"video1" ofType:@"mp4"];
    NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mov"];


    AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
    AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];

    self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];

// I get a WARNING here that AVPlayerViewController may not respong to 'setPlayer'
    [self.mPlaybackView setPlayer:self.queuePlayer];
    [self.queuePlayer play];

}
@end

我到处寻找,我找不到任何这样的插件示例。上面的代码基本上受到启发from here(IOS Native)。我主要使用Javascript工作,所以我认为我的问题是弄清楚如何通过Phonegap插件调用替换Interface Builder IBoutlet播放按钮?

谢谢!

0 个答案:

没有答案