3

虽然这个Phonegap 插件示例可以很好地使用 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

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

谢谢!

4

0 回答 0