Building Apps: How to Create a Method and Play a Sound
Andrew Griesmer September 18, 2015
If you are in the process of learning how to build simulation apps, the video shown here is a fantastic introduction to writing methods. With a fully functioning app already built, we create a method with the click of a button and then proceed to play a sound, include user inputs, and add an if-else statement. All of this is done using Language Elements, which are included in the Method Editor to help make creating methods a breeze.
The Method Editor and Language Elements
When using the COMSOL Multiphysics Application Builder, writing methods is necessary for building any sufficiently complex simulation app. Methods enable an entire tier of actions including, but not limited to, conditional statements (If, If-Else); loops (For, While); and user interfacing (questions, alerts). Since the Java® programming language is used to write methods, you can include all of the Java® functionality. Additionally, the Application Builder comes with a built-in library of Language Elements, blocks of template code that can be inserted directly into your method with the (double-)click of a button.
Beginning with the simplest way to create a method, the video below starts out by showing you how to convert a command sequence to a method and then how to play a sound. We focus on the Compute button for the video, so the commands are “Compute Study 1″ and “Plot Temperature (ht)”. After converting this to a method, we use the Language Element playSound to play the “success_5.wav” sound after the simulation finishes.
The second half of the video continues to add Language Elements to show the true advantages of using methods. We add a user input function, confirm, to ask the user if they’d like to play a sound or not. Then we add an If-Else statement to play the sound if they choose “Yes”, and not play it if they choose “No”.
Video Tutorial: How to Create Methods to Improve Your Simulation Apps
Through three previous videos in this series, we have built a working app using the Form Editor and form objects. Now, we will venture into the land of methods, learning how to use the functionality that can take your app to the next level. This example will show how to create a method from a command sequence and how to play a sound using Language Elements.
For this video, we will be taking a look at the compute button in our app. In the Choose Commands to Run section, we have two commands: Compute the study and then plot the temperature in the graphics1 window. If I want to add a sound to play after this happens, I can do this one of two ways. The first is through the Form Editor, by going to Libraries>Sounds, and then adding either the success, fail, or neutral sound, and clicking run. However, I am going to do this using the Method Editor.
First, I need to convert my command sequence to a new method. So here, my two commands have disappeared and I just have method1 that is run. I also have method1 over here in the methods node in the Application Builder window. Now, I can click on the Go to Method button, and my method1 will open. Here, you can see the two commands that are run to compute the study and then plot the result in the graphics window.
Now, I need to add my sound. I am going to add my sound using the Language Elements feature. There are many different types, including Block Statements, such as If statements, For loops, While loops; there are Conversions, from booleans to strings, strings to doubles, doubles to strings; and then there are User Interface interactions. So what I am going to do is use the Search feature and type in: play. And here, up comes my playSound options. One is to play a signal with a given frequency and a number of seconds to play. And the second is to play it from a .wav file, which is what I’m going to do. So double-clicking adds it to my method. Now, I’m going to get rid of this place holder, and then, in my Application Builder window, go into Libraries, and double-click Sounds. Here, my Sounds library is open, and I have three options: success, fail, and neutral. However, I can also use this plus button to add a file to the library. A number of pre-loaded files have been added to the sounds folder, in the data folder, in the Multiphysics folder of your COMSOL installation. And you can, of course, add any other .wav files that you want, and have permission for, to this folder and to your library. I am going to select “success_5.wav”. Now, I will double-click inside this, and then copy it, and then add my two quotation marks to make it a string, and then paste success_5.wav here. So, now this will play my success sound after the model has been run and plotted in the graphics1 window.
Now, as I mentioned before, this can be done in the Form Editor, so now I will show some functionality that can only be done within the Method Editor. Let’s say we want to give our user the option of whether they want a sound to play or not. Let’s add that functionality now. I am going to go under User Interface and select one of the confirm options, “confirm(question)”, in which I can ask the user a question. So I will ask, “Do you want to play a sound after the simulation is finished?”, and the answer “Yes” or “No” is stored in the string “answer”. Now, I need to add my If statement, so I will go under Block Statements and double-click If-Else, and this gives me my basic structure for my If-Else statement. I need to enter my condition, which will be “if (answer == “Yes”), I want to run the study, plot the graphics, and then play the sound. However, if they click “No”, all I want to do is run the study and plot the graphics.
Now, let’s test the application to see this in action. First, I will edit my input parameters and then update my geometry, and now, click the Compute button. A dialog box appears that asks me if I want to play a sound after the simulation is finished and I am going to click yes. So there is our sound after the simulation finishes. Now, I will click Compute again, except this time I will click no. And there you have it, no sound is played afterwards. So, this is a basic example of how to use Language Elements and the Method Editor to include functionality that you simply cannot do alone with the Form Editor.