While making mobile application using Ionic 2, you wants to make your app more attractive. To make it attractive you need to add some images ,videos that can attract users. So to make application more attractive you can add videos by following the below steps:
Step 1 : Firstly add video folder in app folder by the name videos and save the video in it.
Step 2: For synchronization with www folder you have to add a task in gulp file.
gulp.task('videos', function(){
return gulp.src('app/videos/**/*.+(mp4)')
.pipe(gulp.dest('www/build/video'));
});
here gulp.src(‘app/videos/**/*.+(mp4)’) is the source path from where video will be taken and will be sent to the www folder after performing the gulp task.
Step 3: Now add this new task ‘videos‘ in the ‘watch‘ gulp-watch and ‘build‘ gulp-build tasks:
gulp.task('watch', ['clean'], function(done){
runSequence(
['sass', 'html', 'fonts', 'scripts','images','videos'],
function(){
gulpWatch('app/**/*.scss', function(){
gulp.start('sass');
});
gulpWatch('app/**/*.html', function(){
gulp.start('html');
});
gulpWatch('app/**/*.+(ttc|otf)', function(){ gulp.start('fonts'); });
buildBrowserify({
src: browserifySrc,
watch: true
}).on('end', done);
}
);
});
gulp.task('build', ['clean'], function(done){
runSequence(
['sass', 'html', 'fonts', 'scripts', 'images','videos'],
function(){
buildBrowserify({
src:browserifySrc,
minify: isRelease,
browserifyOptions: {
debug: !isRelease
},
uglifyOptions: {
mangle: false
}
}).on('end', done);
}
);
});
Step 4: For display , Ionic 2 code in .html page :
<video autoplay loop class="video" muted webkit-playsinline requestFullScreen>
<source src="build/video/Muted_bg_video.mp4" type='video/mp4' >
</video>
code of .scss for styling :
.bg-vid {
position: absolute;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: 100%;;
z-index: -100;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
-o-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background-size: cover;
}
Note : For iPhone this above code will show video player and then after stopping it we will get the screen. To resolve this problem we have to add preferences in the config.xml file :
<preference name="AllowInlineMediaPlayback" value="true"/>
and in .html file you have to add the below code which is in bold as iPhone use h.264 encoding scheme:
<video autoplay loop class="video" muted webkit-playsinline requestFullScreen>
<source src="build/video/Muted_bg_video.mp4" type='video/mp4; codecs="h.264"' >
<source src="build/video/Muted_bg_video.mp4" type='video/mp4' >
</video>
By following these steps you are done with adding video in background.