Skip to content

Commit

Permalink
2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
bnussey committed Dec 18, 2016
1 parent 3d0450d commit ada44d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-videoplayer",
"version": "2.1.1",
"version": "2.1.2",
"main": "videoplayer.js",
"typings": "videoplayer.d.ts",
"description": "A NativeScript plugin that uses the native video players to play local and remote videos.",
Expand Down
10 changes: 5 additions & 5 deletions video-source/video-source.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import common = require("./video-source-common");
import enums = require("ui/enums");
import definition = require("./video-source");

declare var android, AVPlayer, NSBundle, NSURL;
declare var android, AVPlayerItem, NSBundle, NSURL;

global.moduleMerge(common, exports);

export class VideoSource implements definition.VideoSource {
public android: any; /// android.widget.VideoView
public ios: any; /// AVPlayer
public ios: any; /// AVPlayerItem
height: any;
width: any;

public loadFromResource(name: string): boolean {
let videoURL = NSBundle.mainBundle().URLForResourceWithExtension(name, null);
let player = new AVPlayer(videoURL);
let player = new AVPlayerItem(videoURL);
this.ios = player;
return this.ios != null;
}
Expand All @@ -29,14 +29,14 @@ export class VideoSource implements definition.VideoSource {
}

let videoURL = NSURL.URLWithString(fileName);
let player = new AVPlayer(videoURL);
let player = new AVPlayerItem(videoURL);
this.ios = player;
return this.ios != null;
}

public loadFromUrl(url: string): boolean {
let videoURL = NSURL.URLWithString(url);
let player = new AVPlayer(videoURL);
let player = new AVPlayerItem(videoURL);
this.ios = player;
return this.ios != null;
}
Expand Down
40 changes: 24 additions & 16 deletions videoplayer.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import * as application from 'application';

declare var NSURL, AVPlayer, AVPlayerViewController, AVPlayerItemDidPlayToEndTimeNotification, UIView, CMTimeMakeWithSeconds, NSNotification, NSNotificationCenter, CMTimeGetSeconds, CMTimeMake, kCMTimeZero, AVPlayerItemStatusReadyToPlay;


global.moduleMerge(common, exports);

function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
Expand All @@ -21,7 +20,6 @@ function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
// register the setNativeValue callback
(<proxy.PropertyMetadata>common.Video.videoSourceProperty.metadata).onSetNativeValue = onVideoSourcePropertyChanged;


export class Video extends common.Video {
private _player: any; /// AVPlayer
private _playerController: any; /// AVPlayerViewController
Expand All @@ -32,20 +30,16 @@ export class Video extends common.Video {
private _observer: NSObject;
private _observerActive: boolean;


constructor() {
super();

this._playerController = new AVPlayerViewController();

this._player = new AVPlayer();
this._playerController.player = this._player;
// showsPlaybackControls must be set to false on init to avoid any potential 'Unable to simultaneously satisfy constraints' errors
this._playerController.showsPlaybackControls = false;
this._ios = this._playerController.view;
this._observer = PlayerObserverClass.alloc();
this._observer["_owner"] = this;

}

get ios(): any {
Expand All @@ -54,16 +48,26 @@ export class Video extends common.Video {

public _setNativeVideo(nativeVideoPlayer: any) {
if (nativeVideoPlayer != null) {
this._player = nativeVideoPlayer;
this._init();
let currentItem = this._player.currentItem;
if (currentItem !== null) {
// Need to set to null so the previous video is not shown while its loading
this._player.replaceCurrentItemWithPlayerItem(null);
this._removeStatusObserver(currentItem);
this._addStatusObserver(nativeVideoPlayer);
this._player.replaceCurrentItemWithPlayerItem(nativeVideoPlayer);
}
else {
this._addStatusObserver(nativeVideoPlayer);
this._player.replaceCurrentItemWithPlayerItem(nativeVideoPlayer);
this._init();
}
}
}

public _setNativePlayerSource(nativePlayerSrc: string) {
this._src = nativePlayerSrc;
let url: string = NSURL.URLWithString(this._src);
this._player = new AVPlayer(url);

this._init();
}

Expand All @@ -85,11 +89,6 @@ export class Video extends common.Video {
this._player.muted = true;
}

if (!this._observerActive) {
this._player.currentItem.addObserverForKeyPathOptionsContext(this._observer, "status", 0, null);
this._observerActive = true;
}

if (!this._didPlayToEndTimeActive) {
this._didPlayToEndTimeObserver = application.ios.addNotificationObserver(AVPlayerItemDidPlayToEndTimeNotification, this.AVPlayerItemDidPlayToEndTimeNotification.bind(this));
this._didPlayToEndTimeActive = true;
Expand Down Expand Up @@ -151,8 +150,7 @@ export class Video extends common.Video {
}

if (this._observerActive = true) {
this._player.currentItem.removeObserverForKeyPath(this._observer, "status");
this._observerActive = false;
this._removeStatusObserver(this._player.currentItem);
}
this.pause();
this._player.replaceCurrentItemWithPlayerItem(null); //de-allocates the AVPlayer
Expand All @@ -163,6 +161,16 @@ export class Video extends common.Video {
this._emit(common.Video.loadingCompleteEvent);
}

private _addStatusObserver(currentItem) {
currentItem.addObserverForKeyPathOptionsContext(this._observer, "status", 0, null);
this._observerActive = true;
}

private _removeStatusObserver(currentItem) {
currentItem.removeObserverForKeyPath(this._observer, "status");
this._observerActive = false;
}

}

class PlayerObserverClass extends NSObject {
Expand Down

0 comments on commit ada44d4

Please sign in to comment.