Monday, May 24, 2021

Display Images and Video Player using Ionic

 

Lately I explained how to take photos and how to capture videos and store them within your apps directory. Due to a wish of you, this time I will show you how to present images and videos properly in your ionic app. Therefore, we will create a simple media popover using ionic modal overlay and the ionic slide box to slide through images. I will just load some local files as it’s a bit annoying to always take a photo/video first to see how it works, but you could just change the URLs I use for local files to your URLs to your captured media files.

For a even more detailed guide also check out my Complete Guide To Images With Ionic

I recently updated this post and expanded the functionality to also allow image zooming like in the Facebook app, so make sure to take a look at How To Create An Advanced Ionic Gallery with Image Zooming.

SETUP THE BASE

We start with a simple, blank ionic app:

You could as well add other platforms, but I like to go with iOS as I am developing on a Mac. When your finished, go to your index.html and replace the body with this:

Nothing special in here. We got a small list of images, which will take images from a scope array and a click event which calls showImages with the index of the image in the array. You will see why we pass this parameter soon. Below the image scroll list we got a simple button which shall start our video. Obviously there could be your thumbnail images from your captured movies, but for the simplicity I will just open a local file.

ADDING THE IMAGE POPOVER

The HTML is fine, let’s go to your app.js and add the controller we assigned to our ion-content tag. Inside our controller we need the image array with the source to each image, and the showImages function, so add:

If you want the same images and video file I used, you can grab it from the github repo for this tutorial.
Our showImages sets the scope var activeSlide and calls the showModal function with a template for the modal which will be shown. The showModal function finally calls the ionicModal (which is why we included $ionicModal as a dependency in our controller), sets the scope of the modal and the animation for the appearance, and shows the modal when it is created. The close modal will be used later. So for now, what’s missing? The template! So go ahead and create a file at templates/image-popover.html (you might need to create the folder if your use the blank ionic app), and fill it with:

Here in our image-popover template file we got everything surrounded in a div, which will call closeModal() on click, and a ion-slide-box to display all images as pages with a tiny pager at the bottom. Furthermore we set the active-slide through our scope variable. This is why we passed the index of the image to our showImages() function; we can now open the slide box with exactly the clicked image. Otherwise, the box would always start at the first picture even if we had clicked the third! Be friendly to your users.

For more information on the slide-box check the ionic documentation for ion-slide-box.

The ng-repeat is the same as in our index, only the class has changed. You might have already missed some css if you have run your app before, so let’s change that and open up the style.css file from the project and insert:

Here we have the styling for our tiny image thumbnails in the list, and the styling for the modal box. It’s nothing special, most of the time we just have to call our elements to use the complete width and height of the view. You can play around a bit with the styling elements to see what brakes your app when you remove it. If you have any tweaks or better styling ideas, please leave me a comment below!

That’s all we need for our images! Now run your app and it should look like this:

mediapopover-appmediapopover-imageslidebox

ADDING THE VIDEOPLAYER

The image popover is working, now we need a way to present our videos. First of all we need the source for our clipfile, then we need the action for our image button, so add this to our MediaCtrl:

Quite short, hm? Good thing we created our showModal() function, which now just get’s the link to our video popover. Nothing more to do here! You might know what’s coming now….yes, create the templates/video-popover.html file and insert this:

Our div has again the closeModal() action, and now we have just a simple HTML5 video tag in here. THAT’S ALL? Yes, that is all we need to display our clips! There are some new CSS attributes, so for the last time open the style.css and add the missing stuff:

The centerme is to display the preview of the image in the center of the screen, otherwise it would be at (0,0). The player gets some standard styling to use all the space, again no magic inside.
If you use your own movie file, or wonder why you have no preview image: The video tag loads a clippreview, but sometimes it’s just not fast enough and the result is just a black box. In that case, you can assign a custom image like this:

If you run your app and start the video, it should look somehow like this (depending on your movie):

mediapopover-video
The video starts automatically when you press the button as we set autoplay on the video element, and we got some standard controls for the media player in here.

CONCLUSION

In this tutorial I showed you how it’s very easy to display images and videos in fullscreen inside your ionic app. The complete tutorial can be done without a device, as we use no cordova here. If anyone of you has some better ideas for styling or displaying media, please leave me a comment!

If this tutorial was helpful, follow me on twitter @schlimmson (or your preferred platform) and leave a comment/ tweet it!
Don’t forget to subscribe to my newsletter to receive new posts and infos via mail!

No comments:

Post a Comment

Technology’s generational moment with generative AI: A CIO and CTO guide

 Ref:  A CIO and CTO technology guide to generative AI | McKinsey 1. Determine the company’s posture for the adoption of generative AI As us...