iOS Tutorial iMedia library - Camera and VideoView

VideoView is an object that makes it quite simple to play local or remote videos.

The most important thing to remember about VideoView is that the object itself is not a View (unlike in B4A). The view is exposed through its View property.

In order to play a video you need to:
1. Initialize VideoView.
2. Add VideoView.View to the layout.
3. Load the video with VideoView.LoadVideo or VideoView.LoadVideoUrl (for video streams).

The Ready event is raised when the video is ready for playback.
The Complete event is raised when the playback reaches the end.

Camera object allows you to take pictures, capture videos and also to select exiting pictures and videos from the device albums. Note that it only supports portrait orientation.

The main methods are:
Camera.TakePicture - Opens the Camera form and allows the user to take a picture.
Camera.TakeVideo - Opens the form and allows the user to capture video.
Camera.SelectFromSavedPhotos / SelectFromPhotoLibrary - Open the form and allows the user to select a picture or video from the device camera roll album or device photo library.

On iPads the existing media selection form is anchored to a view.

The complete event will be raised when the user closes the form:
B4X:
Sub Cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
   If Success Then
     If Image.IsInitialized Then
       vv.View.Visible = False
       ImageView1.Bitmap = Image
     Else
       vv.View.Visible = True
       vv.LoadVideo(VideoPath, "")
     End If
   End If
End Sub
If Success is true then either Image will be initialized and will hold the selected / taken photo or VideoPath will hold the path to the selected / captured video.


Starting from iOS 10 you need to add explanation about the camera usage:
B4X:
#PlistExtra:<key>NSPhotoLibraryUsageDescription</key><string>Select a photo.</string>
#PlistExtra:<key>NSCameraUsageDescription</key><string>Taking a photo.</string>
#PlistExtra:<key>NSMicrophoneUsageDescription</key><string>Record video.</string>

iOS 11+ also requires:
B4X:
#PlistExtra:<key>NSPhotoLibraryAddUsageDescription</key><string>Save photo in albums.</string>
 

Attachments

  • CameraExample.zip
    3 KB · Views: 1,097
Last edited:

Mark Turney

Active Member
Licensed User
Longtime User
Thanks for pointing me in the right direction Erel. What I was doing when editing the "camera" example was taking out the "video" components, as I don't need them for my app. However, what I failed to see that I needed to leave the event signature in its correct syntax. Here is the correct code ... the picture now popuates lastImage (my imageview) immediately after clicking "Use Photo".
B4X:
Sub cam1_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)            'I had omitted the Video Path As String, which needs to remain even if not using video
    If Success Then
        If Image.IsInitialized Then
            lastImage.Bitmap = Image
        End If
    End If
End Sub
Thanks again Erel.

Mark
 

Giuliano Cucchiarini

Member
Licensed User
Longtime User
Hi
I am doing some experiments with the Camera in iMedia.
Unlike a similar app that I made in Android (Using B4a) I see that is not possible to take a series of photo one after another and have them automatically saved.
What I see is that I need to perform too many steps:

1. Press Take a Picture
2. Take a Picture
3. Press Use Photo
4. Press again Take a Picture...etc..

Is is possible to improve this scenario?
Thank you

Giuliano C.
 

susu

Well-Known Member
Licensed User
Longtime User
Is there a way to know when the VideoView can not play the source, like onError or something. Now only Complete event fires so I don't know it actually complete or error cause.
 

ilan

Expert
Licensed User
Longtime User
Is there a way to know when the VideoView can not play the source, like onError or something. Now only Complete event fires so I don't know it actually complete or error cause.

you can use a timer.
set it to 1 sec and put a variable in sub vv_complete and change it there if it was successfully loaded

so by loading the video loadok = 0 and in success u change it to loadok = 1
now in timer u ask if loadok = 1 else video didnot load successfully...

(just a thought, for sure not the sexiest way... :D)

edit: again if i think about it you should get in the complete sub success = false if it could not load that video, dont you? my way will only tells you if the video was not loaded and the complete sub was not fired...
 

ilan

Expert
Licensed User
Longtime User
You don't need a timer. You can store the current time when you loaded the video and compare it to the time that Complete event fired (assuming that it fired when there was an error).

i think thats his problem, he want to check if the complete event was fired or not...
he need to find a way to catch an error that was not fired from the complete event and with a timer it will be possible..
 

susu

Well-Known Member
Licensed User
Longtime User
@Erel: Complete event fires whenever the source is error or actually complete. I can't know which is the case to handle.
@ilan: Thank you for your suggestion but I can't trust the timer because some source will take more time to load than others.

How about check the value of vv.Position? I think it will always > 0 if it can play the source, right?
 

ilan

Expert
Licensed User
Longtime User
but I can't trust the timer because some source will take more time to load than others

should not take to much to load a video... set it to 5 sec if you think its not enough and you can put a loading dialog so the user wont get a filling of freezing app...

I can't know which is the case to handle

why not if complete then success = true else if error then success = false ..
 
Last edited:

CryoGenID

Active Member
Licensed User
Longtime User
Hello,

one quick question:
Is it also possible to let the user choose 1..n pictures and work with them (e.g. upload them via FTP)?

Thanks a lot and best regards,

Chris
 

qsrtech

Active Member
Licensed User
Longtime User
VideoView is an object that makes it quite simple to play local or remote videos.

The most important thing to remember about VideoView is that the object itself is not a View (unlike in B4A). The view is exposed through its View property.

In order to play a video you need to:
1. Initialize VideoView.
2. Add VideoView.View to the layout.
3. Load the video with VideoView.LoadVideo or VideoView.LoadVideoUrl (for video streams).

The Ready event is raised when the video is ready for playback.
The Complete event is raised when the playback reaches the end.

is there any way to have a video played within a specific area of the videoview? I have a video clip that is lets say 50x200 but when I play the video it won't show unless my "panel" is almost the full screen height cause it plays the video at the bottom. b4a videoview works perfect in this instance. thanks
 

qsrtech

Active Member
Licensed User
Longtime User
Set the VideoView left and top properties to negative values to make the important area visible.
Sure but is there a way to know what size of "video player" we are dealing with so we can adjust properly?
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
how can i show the permission option again? when i try run your example i selected no allow use camera :(
now i cant use the camera, dont have permission and dont know how to allow or show msg again.

i found on forum this LLCamera.AuthorizationDenied but is not for this lib/example
how can i show permission msgbox again if permission is denied? how to check permission when user press a button?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Yvon Steinthal

Active Member
Licensed User
Longtime User
Hello,

I was wondering if it was possible to stream the content of the video before taking a picture into an imageview...

In other words, i have a circle with the picture of someone's face and i'd like to take a picture using that circle frame... is it possible?
 

Yvon Steinthal

Active Member
Licensed User
Longtime User
Actually let me rephrase, i want the camera feed to go through an object, like an image view (not full screen). This is for a profile picture. The person would see the camera feed through that circle and click to take a picture...

Basically cropping down the feed...since the circle is obviously smaller than the feed...
 
Top