How to Build a Video Recorder With CameraX in Android

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new camera a video as I promised in the last video in this video you will learn how you can record videos directly inside of your app with your very own camera feet so in particular this will be a simple setup here where we have a video button and if we click this video button then start the the recording will start we can do something here whatever you want if you click it again then you can see video capture succeeded and the video will be saved in your internal storage on the device I will also show you where you can find this and because displaying that here in our bottom sheet as well would be a bit complex and also not be related to camera EGS anymore this is again something I showed in my storage playlist playlist how you can really save something persistently on the device to also display it but here this video really focuses more on yeah recording a video and then saving it or at least being able to save it somewhere so if that sounds good to you please make sure to check out the initial code below if you haven't followed the last video I highly recommend you to follow the last last video where we covered switching the camera here where we covered opening this gallery and saving photos there and actually taking photos so if we oops we switch the camera for example take a photo and you can see it will appear here that is what we've done in the last video and in this video we already have the foundation with that so you can also learn how we can record videos with camera X so we are going to add this video call Button which we did not do in the previous video yet by jumping inside of this code make sure to check it out on GitHub or just open your previous project and here for this last icon button for taking a photo want to copy that and paste it and in this case we want to call this record video and the icon would be video cam and in order to record a video what we need is recording object that comes from camera X so private bar recording which is a recording so that is basically an active recording an active video that is going to be recorded and contains information about that so initially that's null but as soon as we start recording we of course want to assign something to that so we now scroll down below this take photo function we can have an additional function to record a video that again will take our controller and here we want to first of all check if the recording is not equal to null so that means we hit record while recording was already in progress in that case we want to stop the recording so recording that stop we set it to null again and we return all of this function so that means we just want to stop the recording and now save the video then if we don't have all the record permissions like uh camera and recording audio we also want to return outo of this function and we should actually also take this and add this as a little safety measure here for our take photo function I forgot that in the last video but if we now go to our record video function we can use our controller do start recording and there are a bunch of these overloads you can see each of these returns this recording object so want to say start recording and assign the return value to our record in object and what do we now want to specify here well first of all output options because for videos there is not an inmemory data type as of my knowledge like we have for photos so here we have bit Maps which you can perfectly store in memory but a video does not have a data type for that out of the box at least so we can only record a video by directly saving it on the file system which is also good since videos are typically quite larger than photos and the first First Option here file output options is if we just want to store everything in a file that is what we're going to use since we want to store something in internal storage it's always the easiest form of storage since it doesn't need any permissions but if you really want to um add your photo or add your video rather to the real Gallery so that you can also see it in the yeah next to your real photos and your real photo gallery then you would need to use media store which allows you to just insert something um like a video a photo into your real Gallery with all its data that belongs to it so we're going to use file output options and we're going to use its Builder and the builder takes in the file we want to save the video ad and we can create this here output file is a new file where do we want to save this well we're going to use a replication context dot actually we don't need the context we're instead of an activity we can just use the files directory which is our internal storage and then the child so the f file name would be my recording. mp4 this will now get overridden every time we make a new recording which I think is what we want since we don't want to pollute our file system with unnecessary videos but of course you could also set it up in a comparable way like we did in our view model just with a persistent storage that we that you have a list of videos in a real setting you would of course want to keep the recordings if you want to display these somewhere so we're going to pass our output file here call. build and the next parameter is our audio config um this will be audio config do either audio disabled if we don't if you want to record without audio or we say. create enable audio is true and then we can get to the last parameter um actually not the last one uh there's one more the executor we had that in the previous video already which you can get with context compat get main executor and we pass replication context and last but not least the listener so that is what gets triggered when the recording is finished we get these video record events there and we can just create a new listener I think we can also just make that a Lambda like this you can see we get video record events in here and then we can check these in the when expression um can we see which events there are new but there's actually only one event or two events we want to be notified about here on the one hand video record event. finalize which is called when the video is going to be finished and it must be is I think yes we also get an error here um call request permission yes um because Android Studio does not recognize that we already check for permissions in here so we can just add The annotation to ignore this so Alt Enter suppress this permission check and then we get rid of the warning or the error getting back into this when expression we now want to check if the event has any errors we want to close the recording so if something just went wrong we want to set the recording back to null and we might just want to show toast toast. make text application context video capture failed for example and toast. length long and we call that show and you could also get the actual cost of the throwable here so if you want to add a log statement feel free to do that to actually find out what the real error is but if there is no error which there hopefully isn't we want to jump into this L's block and just also show a toast video capture succeeded this time and if we go up here to our record video icon button here we of course don't want to call take photo but rather record video with our controller then this should already be everything we need to do of course if you want to update your UI based on the recording state or so you would need to update that here in the record video function so your view model State maybe to show a little uh red circle like you know it from recording video they would need to update it here or rather hide it in this case and here you would need to show it so basically just a Boolean State and then you check okay if the state is true then you show the little red bubble and otherwise not I would say we try this out definitely make sure you have this video capture enabled here uh this was very hard to find I think this is not very good usability for the developers here that you need to explicitly enable video capture and especially with this function because uh for me it doesn't sound like this is a function which enables video I would rather expect something like um is is video capture enabled and we set that to true but that's a Val so we can't do that or I would expect something like enable video capture but uh it took me a while to find the set enabled use cases function here which you need to use to enable this so let's go in here in visor and switch the camera let's take a look switch the camera I want to record a video I hit record and nothing crashes that seems cool and if we now hit record again we should hopefully get a successful toast video capture succeeded that is looking good how do we now get access to the video that was saved in our internal storage we can do this with a device file explorer so on the right side for me um there is not the device Explorer so on the left side for you probably here are under this more tool Windows click this and open the device Explorer this is basically your devices file system make sure your emulator or your real device is selected and we can find the internal storage files under data um I think it's app no it was Data data and here we now need to search for our package name so in my case that is camera a guide there it is we want to open this and under files is our internal storage for that app so as you can see there's our recording and if you right click on that and click save as you can save that on your machine so I will save that somewhere what I don't want to show you on my desktop save and then if we open this then let's see what we recorded there we go here's our video as you can see everything seems to be fine I also hear audio I don't know if you hear that as well but our recording works you just recorded a video with camera X but this is by far not everything you can do with camera X as I've promised in the next video we're going to build a real AI app with which you can recognize popular landmarks in your era so definitely don't miss that one in case you are an Android developer who is looking for becoming an industry ready Android developer definitely also don't miss my more advanced Android premium courses down below which are made to prepare you for the industry and other than that thanks so much for watching I will see you back in the next video have an amazing rest of your week [Music] bye-bye
Info
Channel: Philipp Lackner
Views: 9,130
Rating: undefined out of 5
Keywords:
Id: eYGQEQnLpnI
Channel Id: undefined
Length: 11min 37sec (697 seconds)
Published: Sun Oct 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.