Working with multimedia in the 1C mobile platform

Anton Shvyikovsky

11.09.2020 17 min

The current situation with the pandemic has affected the operation of any business around the world. Many companies restructured their activities, organized distributed work of employees, and switched to online sales. On the other hand, there are more and more operations with clients “on the road”: couriers of online stores, sales agents, etc. The results of their activities (for example, new orders issued), they must be able to quickly enter into the central database. And other employees of the company, already from the office or at home, must promptly receive some data (documents, reports) necessary for work. Since mobile devices have different multimedia capabilities, it would be convenient for the courier to take advantage of these capabilities right in the order. For example, he could take photographs of goods that the client did not like, or a video / audio recording with the client's feedback on the order.

In this article, we will discuss how to make photos, videos, and record audios using our 1C:Enterprise mobile Platform. At first, we will learn the basics for Multimedia usage in your configuration. Here we make the basic application to take Photo, Video, and Audio Recording. We also store a temporary picture just to show it. These methods and procedures are the 1C:Enterprise embedded functions. They are available for both Android and IOS (except some noted functions).

At first, we prepare the Form:

 

Here we use two pages. On the first Page, we place three Buttons with the corresponding commands. And on the second Page, we place the Image Field to store our Photo.

Here is the list of the Form commands:

  

We use the unique Procedure for all the commands Command Executor.

We also add the additional attribute to store our Photo:



Here is the Form Item for this attribute:



Now to the code:

&AtClient

//The unique command for all the multimedia
Procedure CommandExecutor(Command)

//Create Result variable with undefined value
Result = Undefined;

//Preprocessor instructions which select the place for executing the following code
#IF MobileAppClient Then

//Choose the command by its name
If Command.Name = "Photo" Then

      //Using MultimediaTools option availible only on mobile appliction client and Mobile client  
      //Contains a lot of methods (check the Syntax assistant).  
      //If the PhotoSupported() method is true we proceed

      If MultimediaTools.PhotoSupported() Then

           //Saving the result of MakePhoto procedure (MultimediaData Type containes FileExtension and MIMEType)
           Result = MultimediaTools.MakePhoto();

           // Use the GetBinaryData() method to save multimedia as binary data to BDResult variable
           BDResult = Result.GetBinaryData();

           // Use the PutToTempStorage() method to save our multimedia to the new UUID temp variable just to show it
           // The value is cleared after we leave the form
           RefToMultimedia = PutToTempStorage(BDResult, New UUID);

      Else Message("Photo is not supported!");
      EndIf; 

ElsIf Command.Name = "Video" Then

      If MultimediaTools.VideoRecordingSupported() Then

           Result = MultimediaTools.MakeVideoRecording();

      Else Message("Video is not supported!");
      EndIf;

ElsIf Command.Name = "Audio" Then

      If MultimediaTools.AudioRecordingSupported() Then

           Result = MultimediaTools.MakeAudioRecording();
      Else Message("Audio is not supported!");
      EndIf;

EndIf;

//Cancel the CommandExecute procedure if the return file has undefined value (Taking photo was canceled)  
If Result = Undefined Then Message("Abort Command");
      Return;
EndIf;

#EndIf

EndProcedure


Let's discuss it:

1.     We use the unique Procedure for all the commands. The command is chosen by its name.

2.     Preprocessor Instruction is needed to compile the code only when it is launched on the Mobile Device. Without this instruction, we won't be able to use the MultimediaTools.

3.     We use MultimediaTools.PhotoSupported() method to check if Camera is available and Photo is supported for our device. (We need to pass the Camera parameter to this method, but we have the next Part for this purpose)

4.     Use the MakePhoto() method to take Photo.

5.     Use GetBinaryData() to obtain the binary data of your Photo.

6.     Use PutToTempStorage() method to save our Photo to UUID and save it to the attribute.

7.     We cancel the CommandExecute Procedure with the corresponding message.

Next, we will increase our abilities by passing the parameters to our methods.

But first of all, we Prepare the Form:


We add Buttons Video and Audio to the Pages 3 and 4, and we also add three additional Form Items corresponding to their Form Attributes.

Here is the list of the Form Attributes we need to store the Data obtained from our application:


For the CameraResolution Form Item, we need to make the ListChoiceMode parameter on:


When you select a value from the drop-down list, the value of the "Text Box" form element becomes equal to the value of the selected list element

If a variable is entered in the "DataPath" property of this Text Box, then after selecting a value from the list, the value of the specified Datapath variable will be the value of the list item.

The variable must be created in the details of the form with the type "Arbitrary" so, when we select the value from the drop-down list, we change the value both for the Form item and the Form Attribute CameraResolution. It becomes a chosen Resolution value that can be transformed into a string

Now to the code.

First of all, we specify the parameters:

&AtClient

//Setup the default PhotoQuality Form Attribute at the time you open the form

Procedure OnOpen(Cancel)

    #IF MobileAppClient Then

        //Default Photo quality
        PhotoQuality = 100;

        //Unavailable for IOS
        //Choose the Camera you want to use (Front, Rear, or Auto).

        Camera = DeviceCameraType.Auto;

        //We set the List of available Camera resolutions for our Camera type and save it to the variable with the GetSupportedCameraResolutions method

        ListOfResolutions = MultimediaTools.GetSupportedCameraResolutions(Camera);                //Unavailible for IOS

        //If our Resolution list is not empty we upload its values to the form item CameraResolution

        //Don't mix up it with the Form attribute!!!
        //Don't forget to turn the ListChoiceMode on for the Form Item!!!

        If NOT ListOfResolutions = Undefined Then

            Items.CameraResolution.ChoiceList.LoadValues(ListOfResolutions);   

        EndIf;

    //Specify the Quality for Video recordings. The options should be predefined, you may use Enumerations for this purpose

    #EndIf

EndProcedure


1.       We set up the default Photo Quality for our Camera. (Unavailable for IOS)

2.       Specify the Camera you want to use (Front, Rear, Auto)

3.       We set the List of available Camera resolutions for our Camera type and save it to the variable with the GetSupportedCameraResolutions method. (Unavailable for IOS)

4.       If our Resolution list is not empty, we upload its values to the form item CameraResolution.

5.       Specify the Quality for Video recordings. The options should be predefined, and you may use Enumerations for this purpose. (Unavailable for IOS. If this does not work, then you may skip this step)

After that, we proceed to all the changes we make in our application:

&AtClient

Procedure CommandExecutor(Command)

    Result = Undefined;

    #IF MobileAppClient Then   

        If Command.Name = "Photo" Then

            //Pass the parameter Camera to check if can take pictures with this type
            If MultimediaTools.PhotoSupported(Camera)Then

//Here we check two things:

//1) We use ValueIsFilled() function to check if String(CameraResolution) is Undefined or Null

//We pass our Array of DeviceCameraResolution type to the String function() to get it String representation

//Function ValueIsFilled() can't accept mutable value (Arbitrary Attribute) so we transform it to the string

//The String() method can't transform the whole Array of the DeviceCameraResolution values, so it returns Undefined

//Thats why we selected ListChoiceMode On in the Form Item CameraResolution properties

//When you select a value from the drop-down list, the value of the "Text Box" form element becomes equal to the value of the selected list element

//If a variable is entered in the "DataPath" property of this Text Box, then after selecting a value from the list, the value of the specified Datapath variable will be the value of the list item.

//The variable must be created in the details of the form with the type "Arbitary"

//so, when we select the value from the drop-down list, we change the value both for the Form item and the Form Attribute CameraResolution. It becomes a chosen Resolution value that can be transformed to a string

//2)We check if there are any availible resolutions

                If NOT ValueIsFilled(String(CameraResolution)) AND Items.CameraResolution.ChoiceList.Count() > 0 Then

                    Message("Specify the photo resolution!");

                    Return;

                EndIf;

                //We pass our Parameteres to the MakePhoto() method

                Result = MultimediaTools.MakePhoto(Camera,CameraResolution,PhotoQuality,BlackAndWhite);
                BDResult = Result.GetBinaryData();
                RefToMultimedia = PutToTempStorage(BDResult, New UUID);

            Else

                Message("Photo is not supported!")

            EndIf;

        ElsIf Command.Name = "Video" Then

            If MultimediaTools.VideoRecordingSupported() Then

                //We pass our Parameteres to the MakeVideoRecording() method
                Result = MultimediaTools.MakeVideoRecording(Camera,VidQuality);

            Else

                Message("Video is not supported!");

            EndIf;

        ElsIf Command.Name = "Audio" Then

            If MultimediaTools.AudioRecordingSupported() Then

                //No additional parameteres for Audio
                Result = MultimediaTools.MakeAudioRecording();

            Else

                Message("Audio is not supported!");

            EndIf;

        EndIf;

    If Result = Undefined Then

        Message("Abort Command");

        Return;

    EndIf;

#EndIf

EndProcedure


1.       Pass the parameter Camera to check if can take pictures with this type

2.       Here we check two things:
We use ValueIsFilled() function to check if String(CameraResolution) is Undefined or Null. We pass our Array of DeviceCameraResolution type to the String function() to get it String representation. Function ValueIsFilled() can't accept mutable value (Arbitrary Attribute), so we transform it into the string. The String() method can't transform the whole Array of the DeviceCameraResolution values, so it returns Undefined. That's why we selected ListChoiceMode On in the Form Item CameraResolution properties.

We check if there are any available resolutions

3.       We pass our Parameters to the MakePhoto() method.

4.       We pass our Parameters to the MakeVideoRecording() method.

5.       No additional parameters for Audio.

Next, we will learn how to store our Multimedia files using Catalog. We also can store these files using the Information Register. You can try this way by yourself.

First of all, we need to create a new Catalog and implement additional attributes for our Catalog:


The FileName attribute is of the String type that is needed to store the Multimedia File Name. And also, the BinaryData attribute is of the ValueStorage type and is necessary to store the Binary Data of our file.

The ListForm for our Catalog is pretty simple:


The only button for our form is ListMakeVideo with Form Command MakeVideo:



Now let's discuss the MakeVideo() Command:

&AtClient

//We create a good old MakeVideoRecording() method to create a video file
Procedure MakeVideo(Command)

#If MobileAppClient Then           

If MultimediaTools.VideoRecordingSupported() Then

VideoData = MultimediaTools.MakeVideoRecording();

//Create a new file to our Catalog using our procedure CreateNewFile if the Video file is not empty

        If VideoData <> Undefined Then

            //Get Video Binary Data
            VideoBinary = VideoData.GetBinaryData();

            //Get Video MIME type Data
            VideopMIME = VideoData.MIMEType;

            //Get Video File Extension Data
            VideoExtension = VideoData.FileExtention; 
            CreateNewFile(VideoBinary, VideopMIME, VideoExtension);

        Else

            Message("MultimediaData is Undefined");

        EndIf;

    Else

        Message("Video is not supported!");

    EndIf;   

#EndIf     

EndProcedure

As you can see, it is the good old method for creating a video file. We just use the Video File Data to pass it to our CreateNewFile() Procedure:

&AtServer

Procedure CreateNewFile(BinaryData, MIMEType, FileExtention)

typeMIMEType = MIMEType;

   //Search for the symbol "/" in the typeMIMEType string using StrFind() Method and return its number

    //The MIMEType returns a name of the multimedia (video) and its extension (mp4) divided by "/"

    Number = StrFind(typeMIMEType, "/");

  //We cut the extension (mp4) and symbol "/" from the string leaving only the word "video'

    If Number > 0 Then

        typeMIMEType = Left(typeMIMEType, Number - 1);

    EndIf;

    //Create the name for our file using StrReplace() method

    FileName = StrReplace(String(CurrentDate()), ":", "_") + "." + FileExtention;
    FileName = StrReplace(FileName, " ", "_");

   //To use the FileName, we need to create a new file and pass our string as a path to the corresponding file

    File = New File(FileName);

    //We create a new Catalog file to store our Video
    NewFile = Catalogs.FilesMultimedia.CreateItem();

   //We save the Description, the Filename
    NewFile.Description = "File: " + typeMIMEType + " " + String(CurrentDate());
    NewFile.FileName    = File.Name;

    //Create a new ValueStorage file from our BinaryData using Deflation() method to compress data
    NewFile.BinaryData = New ValueStorage(BinaryData, New Deflation());

    //Record the created file to the DataBase
    NewFile.Write();

   EndProcedure

 

This Procedure writes the Video file to our Catalog.

Our application is ready! So, we have analyzed how to work with multimedia using the 1C mobile platform, and thereby greatly simplify the work of employees in the fields.

You can download this the Demo Configuration to learn this multimedia properly.

If you have any questions about this article, you can always get answers on our forum: https://1c-dn.com/forum/

Stay with us. There are still many interesting things ahead

Be the first to know tips & trick on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.