Media
Table of Contents:
- Camcorder
- Camera
- FilePicker
- ImagePicker
- Player
- Sound
- SoundRecorder
- SpeechRecognizer
- TextToSpeech
- Translator
- VideoPlayer
Camcorder
A component to record a video using the device’s camcorder. After the video is recorded, the
name of the file on the phone containing the clip is available as an argument to the
AfterRecording
event. The file name can be used, for example, to set the source
property of a VideoPlayer
component.
Properties
None
Events
- AfterRecording(clip)
- Indicates that a video was recorded with the camera and provides the path to the stored video.
Methods
- RecordVideo()
- Records a video, then raises the
AfterRecording
event.
Camera
Use a camera component to take a picture on the phone.
Camera
is a non-visible component that takes a picture using the device’s camera. After the
picture is taken, the path to the file on the phone containing the picture is available as an
argument to the AfterPicture
event. The path can be used, for example, as the
Picture
property of an Image
component.
Properties
None
Events
- AfterPicture(image)
- Called after the picture is taken. The text argument
image
is the path that can be used to locate the image on the phone.
Methods
- TakePicture()
- Takes a picture, then raises the
AfterPicture
event.
FilePicker
The FilePicker
component is a button-like component that when clicked
by the user will prompt them to select a file from the system. The picker
can also be programmatically opened by calling its
Open method.
Using the FilePicker requires Android 4.4 or higher or iOS 11 or higher.
Properties
- Action
- Sets the desired action for the FilePicker. One of:
- Pick Existing File: Open an existing file
- Pick Directory: Open an existing directory
- Pick New File: Create a new file for saving
- BackgroundColor
- Specifies the
FilePicker
’s background color as an alpha-red-green-blue integer. If anImage
has been set, the color change will not be visible until theImage
is removed. - Enabled
- Specifies whether the
FilePicker
should be active and clickable. - FontBold
- Specifies whether the text of the
FilePicker
should be bold. Some fonts do not support bold. - FontItalic
- Specifies whether the text of the
FilePicker
should be italic. Some fonts do not support italic. - FontSize
- Specifies the text font size of the
FilePicker
, measured in sp(scale-independent pixels). - FontTypeface
- Specifies the text font face of the
FilePicker
as default, serif, sans serif, monospace, or custom font typeface. To add a custom typeface, upload a .ttf file to the project’s media. - Height
- Specifies the
FilePicker
’s vertical height, measured in pixels. - HeightPercent
- Specifies the
FilePicker
’s vertical height as a percentage of theScreen
’sHeight
. - Image
- Specifies the path of the
FilePicker
’s image. If there is both anImage
and aBackgroundColor
specified, only theImage
will be visible. - MimeType
- Sets the desired MIME type for picking a file.
- Selection
- Returns the selected file, possibly as a content URI.
- Shape
- Specifies the shape of the
FilePicker
. The valid values for this property are0
(default),1
(rounded),2
(rectangle), and3
(oval). TheShape
will not be visible if anImage
is used. - ShowFeedback
- Specifies if a visual feedback should be shown when a
FilePicker
with an assignedImage
is pressed. - Text
- Specifies the text displayed by the
FilePicker
. - TextAlignment
- Specifies the alignment of the
FilePicker
’s text. Valid values are:0
(normal; e.g., left-justified if text is written left to right),1
(center), or2
(opposite; e.g., right-justified if text is written left to right). - TextColor
- Specifies the text color of the
FilePicker
as an alpha-red-green-blue integer. - Visible
- Specifies whether the
FilePicker
should be visible on the screen. Value istrue
if theFilePicker
is showing andfalse
if hidden. - Width
- Specifies the horizontal width of the
FilePicker
, measured in pixels. - WidthPercent
- Specifies the horizontal width of the
FilePicker
as a percentage of theScreen
’sWidth
.
Events
- AfterPicking()
- Event to be raised after the
FilePicker
activity returns its result and the properties have been filled in. - BeforePicking()
- Event to raise when the
FilePicker
is clicked or the picker is shown using theOpen
method. This event occurs before the picker is displayed, and can be used to prepare the picker before it is shown. - GotFocus()
- Indicates the cursor moved over the
FilePicker
so it is now possible to click it. - LostFocus()
- Indicates the cursor moved away from the
FilePicker
so it is now no longer possible to click it. - TouchDown()
- Indicates that the
FilePicker
was pressed down. - TouchUp()
- Indicates that the
FilePicker
has been released.
Methods
- Open()
- Opens the
FilePicker
, as though the user clicked on it.
ImagePicker
A special-purpose button. When the user taps an ImagePicker
, the device’s image gallery
appears, and the user can choose an image. After an image is picked, it is saved, and the
Selection
property will be the name of the file where the image is stored. In order
to not fill up storage, a maximum of 10 images will be stored. Picking more images will delete
previous images, in order from oldest to newest.
Properties
- BackgroundColor
- Specifies the
ImagePicker
’s background color as an alpha-red-green-blue integer. If anImage
has been set, the color change will not be visible until theImage
is removed. - Enabled
- Specifies whether the
ImagePicker
should be active and clickable. - FontBold
- Specifies whether the text of the
ImagePicker
should be bold. Some fonts do not support bold. - FontItalic
- Specifies whether the text of the
ImagePicker
should be italic. Some fonts do not support italic. - FontSize
- Specifies the text font size of the
ImagePicker
, measured in sp(scale-independent pixels). - FontTypeface
- Specifies the text font face of the
ImagePicker
as default, serif, sans serif, monospace, or custom font typeface. To add a custom typeface, upload a .ttf file to the project’s media. - Height
- Specifies the
ImagePicker
’s vertical height, measured in pixels. - HeightPercent
- Specifies the
ImagePicker
’s vertical height as a percentage of theScreen
’sHeight
. - Image
- Specifies the path of the
ImagePicker
’s image. If there is both anImage
and aBackgroundColor
specified, only theImage
will be visible. - Selection
- Path to the file containing the image that was selected.
- Shape
- Specifies the shape of the
ImagePicker
. The valid values for this property are0
(default),1
(rounded),2
(rectangle), and3
(oval). TheShape
will not be visible if anImage
is used. - ShowFeedback
- Specifies if a visual feedback should be shown when a
ImagePicker
with an assignedImage
is pressed. - Text
- Specifies the text displayed by the
ImagePicker
. - TextAlignment
- Specifies the alignment of the
ImagePicker
’s text. Valid values are:0
(normal; e.g., left-justified if text is written left to right),1
(center), or2
(opposite; e.g., right-justified if text is written left to right). - TextColor
- Specifies the text color of the
ImagePicker
as an alpha-red-green-blue integer. - Visible
- Specifies whether the
ImagePicker
should be visible on the screen. Value istrue
if theImagePicker
is showing andfalse
if hidden. - Width
- Specifies the horizontal width of the
ImagePicker
, measured in pixels. - WidthPercent
- Specifies the horizontal width of the
ImagePicker
as a percentage of theScreen
’sWidth
.
Events
- AfterPicking()
- Event to be raised after the
ImagePicker
activity returns its result and the properties have been filled in. - BeforePicking()
- Event to raise when the
ImagePicker
is clicked or the picker is shown using theOpen
method. This event occurs before the picker is displayed, and can be used to prepare the picker before it is shown. - GotFocus()
- Indicates the cursor moved over the
ImagePicker
so it is now possible to click it. - LostFocus()
- Indicates the cursor moved away from the
ImagePicker
so it is now no longer possible to click it. - TouchDown()
- Indicates that the
ImagePicker
was pressed down. - TouchUp()
- Indicates that the
ImagePicker
has been released.
Methods
- Open()
- Opens the
ImagePicker
, as though the user clicked on it.
Player
Multimedia component that plays audio and controls phone vibration. The name of a multimedia
file is specified in the Source
property, which can be set in the Designer or
in the Blocks Editor. The length of time for a vibration is specified in the Blocks Editor in
milliseconds (thousandths of a second).
For supported audio formats, see Android Supported Media Formats.
This component is best for long sound files, such as songs, while the Sound
component is
more efficient for short files, such as sound effects.
Properties
- IsPlaying
- Reports whether the media is playing.
- Loop
- If true, the
Player
will loop when it plays. SettingLoop
while the player is playing will affect the current playing. - PlayOnlyInForeground
- If true, the
Player
will pause playing when leaving the current screen; if false (default option), thePlayer
continues playing whenever the current screen is displaying or not. - Source
- Sets the audio source.
- Volume
- Sets the volume property to a number between 0 and 100.
Events
- Completed()
- Indicates that the media has reached the end
- OtherPlayerStarted()
- This event is signaled when another player has started (and the current player is playing or paused, but not stopped).
Methods
- Pause()
- Suspends playing the media if it is playing.
- Start()
- Plays the media. If it was previously paused, the playing is resumed. If it was previously stopped, it starts from the beginning.
- Stop()
- Stops playing the media and seeks to the beginning of the song.
- Vibrate(milliseconds)
- Vibrates for specified number of milliseconds.
Sound
A multimedia component that plays sound files and optionally vibrates for the number of milliseconds (thousandths of a second) specified in the Blocks Editor. The name of the sound file to play can be specified either in the Designer or in the Blocks Editor.
For supported sound file formats, see Android Supported Media Formats.
This Sound
component is best for short sound files, such as sound effects, while the
Player
component is more efficient for longer sounds, such as songs.
Properties
- MinimumInterval
- Specifies the minimum interval required between calls to
Play
, in milliseconds. Once the sound starts playing, all furtherPlay
calls will be ignored until the interval has elapsed. - Source
- The name of the sound file. Only certain formats are supported. See http://developer.android.com/guide/appendix/media-formats.html.
Events
None
Methods
- Pause()
- Pauses playing the sound if it is being played.
- Play()
- Plays the sound.
- Resume()
- Resumes playing the sound after a pause.
- Stop()
- Stops playing the sound if it is being played.
- Vibrate(millisecs)
- Vibrates for the specified number of milliseconds.
SoundRecorder
Multimedia component that records audio.
Properties
- SavedRecording
- Specifies the path to the file where the recording should be stored. If this property is the empty string, then starting a recording will create a file in an appropriate location. If the property is not the empty string, it should specify a complete path to a file in an existing directory, including a file name with the extension .3gp.
Events
- AfterSoundRecorded(sound)
- Provides the location of the newly created sound.
- StartedRecording()
- Indicates that the recorder has started, and can be stopped.
- StoppedRecording()
- Indicates that the recorder has stopped, and can be started again.
Methods
- Start()
- Starts recording.
- Stop()
- Stops recording.
SpeechRecognizer
Use a SpeechRecognizer
component to listen to the user speaking and convert the spoken sound
into text using the device’s speech recognition feature.
Properties
- Language
- Suggests the language to use for recognizing speech. An empty string (the default) will
use the system’s default language.
Language is specified using a language tag with an optional region suffix, such as en or es-MX. The set of supported languages will vary by device.
- Result
- Returns the last text produced by the recognizer.
- UseLegacy
- If true, a separate dialog is used to recognize speech (the default). If false, speech is
recognized in the background and updates are received as it recognizes words.
AfterGettingText
may get several calls withpartial
set totrue
. Once sufficient time has elapsed since the last utterance, orStopListening
is called, the last string will be returned withpartial
set tofalse
to indicate that it is the final recognized string and no more data will be provided until recognition is again started. SeeAfterGettingText
for more details on partial speech recognition.
Events
- AfterGettingText(result,partial)
- Simple event to raise after the SpeechRecognizer has recognized speech. If
UseLegacy
istrue
, then this event will only happen once at the very end of the recognition. IfUseLegacy
isfalse
, then this event will run multiple times as theSpeechRecognizer
incrementally recognizes speech. In this case,partial
will betrue
until the recognized speech has been finalized (e.g., the user has stopped speaking), in which casepartial
will befalse
. - BeforeGettingText()
- Simple event to raise when the
SpeechRecognizer
is invoked but before its activity is started.
Methods
- GetText()
- Asks the user to speak, and converts the speech to text. Signals the
AfterGettingText
event when the result is available. - Stop()
- Function used to forcefully stop listening speech in cases where
SpeechRecognizer cannot stop automatically.
This function works only when the
UseLegacy
property is set tofalse
.
TextToSpeech
The TextToSpeech
component speaks a given text aloud. You can set the pitch
and the rate of speech.
You can also set a language by supplying a language code. This changes the pronunciation of
words, not the actual language spoken. For example, setting the Language
to French
and speaking English text will sound like someone speaking English (en) with a French accent.
You can also specify a country by supplying a Country
code. This can affect the
pronunciation. For example, British English (GBR) will sound different from US English (USA).
Not every country code will affect every language.
The languages and countries available depend on the particular device, and can be listed with
the AvailableLanguages
and AvailableCountries
properties.
Properties
- AvailableCountries
- List of the country codes available on this device for use with TextToSpeech. Check the Android developer documentation under supported languages to find the meanings of these abbreviations.
- AvailableLanguages
- List of the languages available on this device for use with TextToSpeech. Check the Android developer documentation under supported languages to find the meanings of these abbreviations.
- Country
- Country code to use for speech generation. This can affect the pronunciation. For example, British English (GBR) will sound different from US English (USA). Not every country code will affect every language.
- Language
- Sets the language for TextToSpeech. This changes the way that words are pronounced, not the actual language that is spoken. For example, setting the language to French and speaking English text will sound like someone speaking English with a French accent.
- Pitch
- Sets the speech pitch for the TextToSpeech.
The values should be between 0 and 2 where lower values lower the tone of synthesized voice and greater values raise it.
The default value is 1.0 for normal pitch.
- Result
- Returns
true
if the text was successfully converted to speech, otherwisefalse
. - SpeechRate
- Sets the SpeechRate for TextToSpeech.
The values should be between 0 and 2 where lower values slow down the pitch and greater values accelerate it.
The default value is 1.0 for normal speech rate.
Events
- AfterSpeaking(result)
- Event to raise after the message is spoken. The
result
will betrue
if the message is spoken successfully, otherwise it will befalse
. - BeforeSpeaking()
- Event to raise when Speak is invoked, before the message is spoken.
Methods
- Speak(message)
- Speaks the given message.
- Stop()
- Stops any current speech.
Translator
Use this component to translate words and sentences between different languages. This component needs Internet access, as it will request translations from a server at MIT (which in turn will request translations from a commercial translation service). Specify the source and target language in the form source-target using two letter language codes. So “en-es” will translate from English to Spanish while “es-ru” will translate from Spanish to Russian. If you leave out the source language, the service will attempt to detect the source language. So providing just “es” will attempt to detect the source language and translate it to Spanish.
Note: Translation happens asynchronously in the background. When the translation is complete,
the GotTranslation
event is triggered.
Properties
- ApiKey
- The API Key to use. MIT App Inventor will automatically fill this value in. You should not need to change it.
Events
- GotTranslation(responseCode,translation)
- Event indicating that a request has finished and has returned data (translation).
Methods
- RequestTranslation(languageToTranslateTo,textToTranslate)
- By providing a target language to translate to (for instance, ‘es’ for Spanish, ‘en’ for
English, or ‘ru’ for Russian), and a word or sentence to translate, this method will request
a translation. Once the text is translated by the external
service, the event
GotTranslation
will be executed.Note: Translator will attempt to detect the source language. You can also specify prepending it to the language translation, e.g., es-ru will specify Spanish to Russian translation.
VideoPlayer
A multimedia component capable of playing videos. When the application is run, the VideoPlayer
will be displayed as a rectangle on-screen. If the user touches the rectangle, controls will
appear to play/pause, skip ahead, and skip backward within the video. The application can also
control behavior by calling the Start
, Pause
, and SeekTo
methods.
Video files should be in 3GPP (.3gp) or MPEG-4 (.mp4) formats. For more details about legal formats, see Android Supported Media Formats.
App Inventor only permits video files under 1 MB and limits the total size of an application to 5 MB, not all of which is available for media (video, audio, and sound) files. If your media files are too large, you may get errors when packaging or installing your application, in which case you should reduce the number of media files or their sizes. Most video editing software, such as Windows Movie Maker and Apple iMovie, can help you decrease the size of videos by shortening them or re-encoding the video into a more compact format.
You can also set the media source to a URL that points to a streaming video, but the URL must point to the video file itself, not to a program that plays the video.
Properties
- FullScreen
- Sets whether the video should be shown in fullscreen or not.
- Height
- Specifies the component’s vertical height, measured in pixels.
- HeightPercent
- Specifies the
VideoPlayer
’s vertical height as a percentage of theScreen
’sHeight
. - Source
- Sets the “path” to the video. Usually, this will be the name of the video file, which should be added in the Designer.
- Visible
- Specifies whether the
VideoPlayer
should be visible on the screen. Value istrue
if theVideoPlayer
is showing andfalse
if hidden. - Volume
- Sets the volume property to a number between 0 and 100. Values less than 0 will be treated as 0, and values greater than 100 will be treated as 100.
- Width
- Specifies the component’s horizontal width, measured in pixels.
- WidthPercent
- Specifies the horizontal width of the
VideoPlayer
as a percentage of theScreen
’sWidth
.
Events
- Completed()
- Indicates that the video has reached the end
Methods
- GetDuration()
- Returns duration of the video in milliseconds.
- Pause()
- Pauses playback of the video. Playback can be resumed at the same location by calling the
Start
method. - SeekTo(ms)
- Seeks to the requested time (specified in milliseconds) in the video. If the video is paused, the frame shown will not be updated by the seek. The player can jump only to key frames in the video, so seeking to times that differ by short intervals may not actually move to different frames.
- Start()
- Plays the media specified by the
Source
. - Stop()
- Resets to start of video and pauses it if video was playing.