Is there a way to play a video in NativeScript?

3.4k views Asked by At

I've been reading documentation https://docs.nativescript.org/modules but I only could find a image widget. All suggestions are welcome.Thanks in advance!

1

There are 1 answers

0
Emil Oberg On

New answer (edited 10 november 2016)

There's now a cross platform video player module available. Just download and run: https://github.com/bradmartin/nativescript-videoplayer

E.g.

<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:VideoPlayer="videoplayer">
        <StackLayout>
            <VideoPlayer:Video
                loaded="videoplayerLoaded" 
                finished="videoFinished" 
                autoplay="true" 
                height="300" 
                src="~/videos/small.mp4"
            />
        </StackLayout>
</Page>

Old answer

Currently, there's no built in cross platform module (as the image one) to play video. There's an open issue on the subject. However, as this is NativeScript you can make calls to the native APIs (this is what makes NativeScript stand out).

Here's an example of how to make calls to the iOS AVAudioPlayer:

var Clicker = function(resource) {
    var soundPath = NSBundle.mainBundle().pathForResourceOfType("app/"+resource, "mp3");
    var soundUrl = NSURL.fileURLWithPath(soundPath);
    var player = AVAudioPlayer.alloc().initWithContentsOfURLError(soundUrl, null);
    player.prepareToPlay();

    this.click = function() {
        player.currentTime = 0.0;
        player.play();
    };
};
module.exports.Clicker = Clicker;

Full example, please see https://www.nativescript.org/blog/calcunator-the-nativescript-calculator

What you want to do is look at each platforms' APIs and make the calls.

Documentation of media players:

Also good read is NativeScripts documentation on how to call the native APIs from NativeScript.