Is there a way to play video in NativeScript?
I was reading the documentation https://docs.nativescript.org/modules but I could only find the image widget. All suggestions are welcome. Thanks in advance!
source to share
New answer (edited Nov 10, 2016)
A cross-platform video player module is currently available. Just download and run: https://github.com/bradmartin/nativescript-videoplayer
eg.
<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
There is currently no built-in cross-platform module (as image one) for video playback. There's an open issue on this issue. However, since this is NativeScript, you can make calls to native APIs (which is what makes NativeScript stand out).
Here's an example of how to make calls in 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;
For a complete example, see https://www.nativescript.org/blog/calcunator-the-nativescript-calculator
What you want to do is look at the API of each platform and make calls.
Media players documentation:
- Android: http://developer.android.com/guide/topics/media/mediaplayer.html
- iOS: https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html and https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/ AVAudioPlayerClassReference /
It's also good to read the NativeScripts Documentation on how to call native APIs from NativeScript .
source to share