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,088
Last edited:

aaronk

Well-Known Member
Licensed User
Longtime User
I noticed that you say it will allow you to stream videos (VideoView.LoadVideoUrl)

Does this mean this library will let you stream a MJEPG stream (and H.264 streams) and display it on the screen within the app?

What about if you have a IP camera that when you view the URL like: http://ip_address_here/snapshot.jpg it will display a snapshot of the camera.
If you send that same URL it will give you another updated snapshot of the camera.

Will this library let you then load that snapshot from the camera and automatically keep polling for the snapshot or would you say I need to download the snapshot another way and then load it into a imageview ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Quoting Apple documentation:

This class plays any movie or audio file supported in iOS. This includes both streamed content and fixed-length files. For movie files, this typically means files with the extensions .mov, .mp4, .mpv, and .3gp and using one of the following compression standards:

  • H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. (The Baseline profile does not support B frames.)

  • MPEG-4 Part 2 video (Simple Profile)

If you use this class to play audio files, it displays a white screen with a QuickTime logo while the audio plays. For audio files, this class supports AAC-LC audio at up to 48 kHz, and MP3 (MPEG-1 Audio Layer 3) up to 48 kHz, stereo audio.


For an IP camera you can use HttpUtils2 to download the images.
 

ilan

Expert
Licensed User
Longtime User
really great erel, b4i is awsome (and its only BETA), can i ask you one question, in this tutorial i see that you are using a simulator of iphone 5 on your PC (windows) what is it for a simulator?

where can i download it please? thank you...
 

inakigarm

Well-Known Member
Licensed User
Longtime User
really great erel, b4i is awsome (and its only BETA), can i ask you one question, in this tutorial i see that you are using a simulator of iphone 5 on your PC (windows) what is it for a simulator?

where can i download it please? thank you...


I think is reflector; you can display the ios device on computer screen. You have to download the Pc soft and choose AirPlay on ios device

http://www.airsquirrels.com/reflector/
 

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody, I'm porting our B4A APP to Iphone. The Android App starts a sequence of photo grabbing, as fast as the device can and trasfers to a remote host. A limit is imposed by Apple(this drives me crazy, since in 41 years I never had "limits" in what can be done and cannot), they don't allow to start a sequence of foto programmatically, without user's button press. I don't think it's viable in B4I. Am I right?

Best regards
Mauro
 

tigrot

Well-Known Member
Licensed User
Longtime User
Well, I'm trying to extract the frame from a streaming video, convert to a Jpeg and send to the server. After developing with B4A, using Xcode(and objective-c) gives me the sensation to be in a Jail! Now I understand what "JailBroken" stands for... There are so many restrictions, which are reducing feasibility of my projects almost to zero chances. I have done the impossible with any kind of device, from micro to very large systems, but this time I'm very upset by the limits imposed by Apple...
Thank you Erel, you are the best!
 

Mark Turney

Active Member
Licensed User
Longtime User
Having an issue, which is likely related to my newness with B4i, and complete lack of knowledge with iOS :confused:. For some reason, I cannot get a picture taken with my iPAD camera to fill an imageview I created with designer, called lastImage. Here is my code ... any suggestions would be appreciated. I've looked up and down the iMedia documentation and Camera example....
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Public cam1 As Camera
    Private Page1 As Page
    Private Page2 As Page
    Private Page3 As Page
    Private Page4 As Page
    Private lastImage As ImageView
    Private snapPic As Button
    Private tabcontroller As TabBarController
    Private resetBtn As Button
    Private sql1 As SQL
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    NavControl.ToolBarVisible = True
    Page1.Initialize("Page1")
    Page2.Initialize("Page2")
    Page3.Initialize("Page3")
    Page4.Initialize("Page4")
    Page1.Title = "Face Flashcards"
    Page1.RootPanel.Color = Colors.Gray
    Page1.RootPanel.LoadLayout("Home")
    NavControl.ShowPage(Page1)
    tabcontroller.Initialize("tc")   
    cam1.Initialize("cam1", Page1)
    sql1.Initialize(File.DirDocuments, "faceflashcards", True)
End Sub

'Took out page change code "here", as it is working fine...

Private Sub Application_Background
   
End Sub

Sub snapPic_Click
    cam1.TakePicture
End Sub

Sub cam1_Complete (Success As Boolean, Image As Bitmap)
    If Success Then
        If Image.IsInitialized Then
            lastImage.Bitmap = Image
        End If
    End If
End Sub

Thanks in advance for the assistance!

Mark
 

Mark Turney

Active Member
Licensed User
Longtime User
Good idea ... because, nothing happens after I snap the pic with the iPad camera. Once I click "Use Photo", the dialog closes and nothing populates the imageview.

Will put in a break point ... thanks!

Mark
 

Mark Turney

Active Member
Licensed User
Longtime User
Added some msgboxes, as follows...
B4X:
Sub cam1_Complete (Success As Boolean, Image As Bitmap)
    If Success Then
        Msgbox("Success is true", "cam1_complete")
        If Image.IsInitialized Then
            lastImage.Bitmap = Image
        End If
    Else
        Msgbox("Success is false", "cam1_complete")
    End If
End Sub
But, I get no response at all, as if the "complete" event is not being called at all.

Thanks,
Mark
 

Mark Turney

Active Member
Licensed User
Longtime User
I changed to Log("Success") or Log("Failure"). Please, excuse my denseness here, but I don't see anything recorded related to these logs. However, this is what is in the log...

Application_Start
Application_Active
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Application_Inactive
Application_Background

Thanks again Erel,
Mark
 
Top