diff --git a/package.json b/package.json index 1483f5b..0f6000d 100644 --- a/package.json +++ b/package.json @@ -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.", diff --git a/video-source/video-source.ios.ts b/video-source/video-source.ios.ts index 881075a..6f98a60 100644 --- a/video-source/video-source.ios.ts +++ b/video-source/video-source.ios.ts @@ -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; } @@ -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; } diff --git a/videoplayer.ios.ts b/videoplayer.ios.ts index 693f187..ffea400 100644 --- a/videoplayer.ios.ts +++ b/videoplayer.ios.ts @@ -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) { @@ -21,7 +20,6 @@ function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeD // register the setNativeValue callback (common.Video.videoSourceProperty.metadata).onSetNativeValue = onVideoSourcePropertyChanged; - export class Video extends common.Video { private _player: any; /// AVPlayer private _playerController: any; /// AVPlayerViewController @@ -32,12 +30,9 @@ 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 @@ -45,7 +40,6 @@ export class Video extends common.Video { this._ios = this._playerController.view; this._observer = PlayerObserverClass.alloc(); this._observer["_owner"] = this; - } get ios(): any { @@ -54,8 +48,19 @@ 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(); + } } } @@ -63,7 +68,6 @@ export class Video extends common.Video { this._src = nativePlayerSrc; let url: string = NSURL.URLWithString(this._src); this._player = new AVPlayer(url); - this._init(); } @@ -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; @@ -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 @@ -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 {