Android Programming Press on the image to return to the main documentation page.

Audio

List of types:

AudioRecordApp
AudioStreamer
Beeper
JetPlayer
MediaPlayerStream
SoundPool
VideoRecordApp
VideoView

AudioRecordApp

AudioRecordApp lets you use the default audio recorder application to record audio.
After initializing the object you should call Record to start recording.
The RecordComplete event will be raised when record completes.
Example:
Sub Process_Globals
  Dim audioRecorder As AudioRecordApp
  Dim videoRecorder As VideoRecordApp
End Sub

Sub Globals
  Dim vv As VideoView
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
    audioRecorder.Initialize("audioRecorder")
    videoRecorder.Initialize("videoRecorder")
  End If
  vv.Initialize("vv")
  Activity.AddView(vv, 0, 0, 100%x, 100%y)
  Activity.AddMenuItem("Record Video", "RecordVideo")
  Activity.AddMenuItem("Record Audio", "RecordAudio")
  ToastMessageShow("Press on Menu button...", True)
End Sub

Sub RecordVideo_Click
  videoRecorder.Record(File.DirRootExternal, "1.mp4")
End Sub
Sub RecordAudio_Click
  audioRecorder.Record(File.DirRootExternal, "1.3gpp")
End Sub
Sub videoRecorder_RecordComplete (Success As Boolean)
  Log(Success)
  If Success Then
    vv.LoadVideo(File.DirRootExternal, "1.mp4")
    vv.Play
  End If
End Sub
Sub audioRecorder_RecordComplete (Success As Boolean)
  Log(Success)
  If Success Then
    vv.LoadVideo(File.DirRootExternal, "1.3gpp")
    vv.Play
  End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Permissions:

android.permission.WRITE_EXTERNAL_STORAGE

Events:

RecordComplete (Success As Boolean)

Members:


  Initialize (EventName As String)

  Record (Dir As String, FileName As String)

Members description:

Initialize (EventName As String)
Initializes the object and sets the sub that will handle the event.
Record (Dir As String, FileName As String)
Calls the recording application.
Dir and FileName set the output file location.

AudioStreamer


Permissions:

android.permission.RECORD_AUDIO

Events:

RecordBuffer (Data() As Byte)
PlaybackComplete
Error

Members:


  Initialize (EventName As String, SampleRate As Int, Mono As Boolean, Encoding As Int, VolumeChannel As Int)

  Initialize2 (AudioSource As Int, EventName As String, SampleRate As Int, Mono As Boolean, Encoding As Int, VolumeChannel As Int)

  PlayerBufferSize As Int [read only]

  StartPlaying

  StartRecording

  StopPlaying

  StopRecording

  VOLUME_ALARM As Int

  VOLUME_MUSIC As Int

  VOLUME_NOTIFICATION As Int

  VOLUME_RING As Int

  VOLUME_SYSTEM As Int

  VOLUME_VOICE_CALL As Int

  Write (Data() As Byte) As Boolean

Members description:

Initialize (EventName As String, SampleRate As Int, Mono As Boolean, Encoding As Int, VolumeChannel As Int)
Initializes the object.
EventName - Sets the subs that will handle the events.
SampleRate - Sample rate in Hz. Common values: 44100, 22050 and 11025.
Mono - True for mono false for stereo.
Encoding - 8 for 8 bit or 16 for 16 bit. Only 16 bit is supported for now.
VolumeChannel - The output channel. One of the VOLUME constants.
Initialize2 (AudioSource As Int, EventName As String, SampleRate As Int, Mono As Boolean, Encoding As Int, VolumeChannel As Int)
Similar to Initialize. Allows you to set the audio source.
The values are listed
here.
PlayerBufferSize As Int [read only]
Returns the player buffer size. This is the maximum size that can be written at once.
StartPlaying
Starts playing. You should call Write to write the PCM data while playing is in progress.
StartRecording
Starts recording. The RecordBuffer event will be raised during the record.
StopPlaying
Stops playing.
StopRecording
Stops recording.
VOLUME_ALARM As Int
Alarms channel.
VOLUME_MUSIC As Int
Music channel.
VOLUME_NOTIFICATION As Int
Notifications channel.
VOLUME_RING As Int
Phone ring channel.
VOLUME_SYSTEM As Int
System sounds channel.
VOLUME_VOICE_CALL As Int
Voice calls channel.
Write (Data() As Byte) As Boolean
Writes the data to the player queue. The array size must be smaller than PlayerBufferSize.
Returns False if the internal queue is full. In that case the data was not written.
Writing Null to the queue will stop the player when the message is processed and then raise the PlaybackComplete event.

Beeper

Plays a "beep" sound with the given duration and frequency.
Example:
Dim b As Beeper
b.Initialize(300, 500)
b.Beep

Events:

None

Members:


  Beep

  Initialize (Duration As Int, Frequency As Int)

  Initialize2 (Duration As Int, Frequency As Int, VoiceChannel As Int)

  Release

  VOLUME_ALARM As Int

  VOLUME_MUSIC As Int

  VOLUME_NOTIFICATION As Int

  VOLUME_RING As Int

  VOLUME_SYSTEM As Int

  VOLUME_VOICE_CALL As Int

Members description:

Beep
Plays the sound.
Initialize (Duration As Int, Frequency As Int)
Initializes the object with the given duration measured in milliseconds and the given frequency measured in Hertz.
The music volume channel will be used.
Initialize2 (Duration As Int, Frequency As Int, VoiceChannel As Int)
Similar to Initialize. Allows you to set the volume channel.
Release
Releases the resources used by this beeper.
VOLUME_ALARM As Int
Alarms channel.
VOLUME_MUSIC As Int
Music channel.
VOLUME_NOTIFICATION As Int
Notifications channel.
VOLUME_RING As Int
Phone ring channel.
VOLUME_SYSTEM As Int
System sounds channel.
VOLUME_VOICE_CALL As Int
Voice calls channel.

JetPlayer


Events:

QueuedSegmentsCountChanged (Count As Int)
CurrentUserIdChanged (UserId As Int, RepeatCount As Int)

Members:


  ClearQueue

  CloseFile

  Initialize (EventName As String)

  IsInitialized As Boolean

  LoadFile (Dir As String, File As String)

  MaxTracks As Int [read only]

  Pause

  Play

  QueueSegment (SegmentNum As Int, LibNum As Int, RepeatCount As Int, Transpose As Int, MuteArray() As Boolean, UserId As Byte)

  Release

  SetMute (MuteArray() As Boolean, Sync As Boolean)

  SetTrackMute (Track As Int, Mute As Boolean, Sync As Boolean)

Members description:

ClearQueue
Clears the segments queue.
CloseFile
Closes the resources related to the loaded file.
Initialize (EventName As String)
Initializes the object and sets the Subs that will handle the JetPlayer events.
IsInitialized As Boolean
LoadFile (Dir As String, File As String)
Loads a jet file.
MaxTracks As Int [read only]
Returns the maximum number of simultaneous tracks.
Pause
Pauses playback.
Play
Starts playing the segments queue.
QueueSegment (SegmentNum As Int, LibNum As Int, RepeatCount As Int, Transpose As Int, MuteArray() As Boolean, UserId As Byte)
Adds a segment to the queue. No more than 3 segments are allowed.
SegmentNum - The segment identifier.
LibNum - The index of the sound bank associated with this segment. Pass -1 if there is no sound bank.
RepeatCount - Number of times the segment will be repeated. 0 means that it will be played once. Pass -1 to repeat indefinitely.
Transpose - The pitch transition. Should be between -12 to 12.
MuteArray - An array of booleans that sets the mute value of each track. The array length must be equal to MaxTracks value.
UserId - An id given to this segment. When the current segment changes the CurrentUserIdChanged event is raised with this id
(assuming that the id of the previous segment was different).
Release
Releases all resources allocated for the JetPlayer.
SetMute (MuteArray() As Boolean, Sync As Boolean)
Sets the tracks mute state.
MuteArray - An array of booleans that sets the mute state of each track. The array length must be equal to MaxTracks value.
Sync - If false the change will be applied as soon as possible, otherwise the change will be applied at the start of the next segment or next repeat.
SetTrackMute (Track As Int, Mute As Boolean, Sync As Boolean)
Similar to SetMute but only changes the state of a single track.

MediaPlayerStream

MediaPlayerStream is similar to MediaPlayer. Unlike MediaPlayer which plays local files MediaPlayerStream plays audio streams
which are available online. Another difference between the objects is that in this case the Load method is asynchronous.
Only when the file is ready, the StreamReady event will be fired and you can start playing.
According to the native documentation the online resource must support progressive download.
Example:
Sub Process_Globals
  Dim mp As MediaPlayerStream
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    mp.Initialize("mp")
  End If
  mp.Load("http://www...")
End Sub
Sub mp_StreamReady
  Log("starts playing")
  mp.Play
End Sub
Sub mp_StreamError (ErrorCode As String, ExtraData As Int)
  Log("Error: " & ErrorCode & ", " & ExtraData)
  ToastMessageShow("Error: " & ErrorCode & ", " & ExtraData, True)
End Sub
Sub mp_StreamBuffer(Percentage As Int)
  Log(Percentage)
End Sub

Permissions:

android.permission.INTERNET

Events:

StreamReady
StreamError (ErrorCode As String, ExtraData As Int)
StreamBuffer(Percentage As Int)
Complete

Members:


  Duration As Int [read only]

  Initialize (EventName As String)

  IsInitialized As Boolean

  IsPlaying As Boolean

  Load (URL As String)

  Looping As Boolean

  Pause

  Play

  Release

  SetVolume (Right As Float, Left As Float)

  Stop

Members description:

Duration As Int [read only]
Initialize (EventName As String)
Initializes the object.
EventName - Name of Subs that will handle the events.
IsInitialized As Boolean
IsPlaying As Boolean
Load (URL As String)
Starts loading the resource from the given URL.
StreamReady event will be raised when the stream is ready.
Looping As Boolean
Pause
Play
Release
SetVolume (Right As Float, Left As Float)
Sets the playing volume for each channel. The value should be between 0 to 1.
Stop

SoundPool

SoundPool holds a collection of short sounds which can be played with low latency.
Each sound has two Id values which you should work with. The first is the LoadId which is returned when loading the sound with Load.
The second is the PlayId which is returned when you call Play.
When working with SoundPool it is useful to watch the unfiltered LogCat for messages (for example when the sound is too long).

Events:

None

Members:


  Initialize (MaxStreams As Int)

  IsInitialized As Boolean

  Load (Dir As String, File As String) As Int

  Pause (PlayId As Int)

  Play (LoadId As Int, LeftVolume As Float, RightVolume As Float, Priority As Int, Loop As Int, Rate As Float) As Int

  Release

  Resume (PlayId As Int)

  SetRate (PlayId As Int, Rate As Float)

  SetVolume (PlayId As Int, Left As Float, Right As Float)

  Stop (PlayId As Int)

  Unload (LoadId As Int)

Members description:

Initialize (MaxStreams As Int)
Initializes the SoundPool and sets the maximum number of simultaneous streams.
IsInitialized As Boolean
Load (Dir As String, File As String) As Int
Loads a sound file and returns the sound LoadId.
Example:
Dim LoadId As Int
LoadId = SP.Load(File.DirAssets, "sound.wav")
Pause (PlayId As Int)
Pauses the stream with the given PlayId.
Play (LoadId As Int, LeftVolume As Float, RightVolume As Float, Priority As Int, Loop As Int, Rate As Float) As Int
Plays the sound with the matching LoadId and returns the PlayId. Returns 0 if there was an error.
LoadId - The value returned when loading the file.
LeftVolume / RightVolume - The volume value (0 - 1)
Priority - A priority value which you assign to this sound. The higher the value the higher the priority.
When the number of simultaneous streams is higher than the value set in Initialize the lowest priority stream will be stopped.
Loop - Number of times to repeat. Pass -1 to repeat indefinitely.
Rate - Playback rate (0 - 2).
Release
Releases all resources allocated to this object.
Resume (PlayId As Int)
Resumes the stream with the given PlayId.
SetRate (PlayId As Int, Rate As Float)
Sets the rate of the stream with the given PlayId. Value is between 0 to 2.
SetVolume (PlayId As Int, Left As Float, Right As Float)
Sets the volume of the stream with the given PlayId. Values are between 0 to 1.
Stop (PlayId As Int)
Stops the stream with the given PlayId.
Unload (LoadId As Int)
Unloads the stream with the given LoadId.

VideoRecordApp

VideoRecordApp lets you use the default video recorder application to record video.
After initializing the object you should call Record to start recording.
The RecordComplete event will be raised when record completes.

Events:

RecordComplete (Success As Boolean)

Members:


  Initialize (EventName As String)

  Record (Dir As String, FileName As String)

  Record2 (Dir As String, FileName As String, MaxLengthSeconds As Int)

  Record3 (Dir As String, FileName As String, MaxLengthSeconds As Int, Uri As android.net.Uri)

Members description:

Initialize (EventName As String)
Initializes the object and sets the sub that will handle the event.
Record (Dir As String, FileName As String)
Will not work on Android 7+ devices. Use Record3 instead.
Record2 (Dir As String, FileName As String, MaxLengthSeconds As Int)
Will not work on Android 7+ devices. Use Record3 instead.
Record3 (Dir As String, FileName As String, MaxLengthSeconds As Int, Uri As android.net.Uri)
Calls the recording app. The RecordComplete event will be raised.
Dir / FileName - Video output file.
MaxLengthSeconds - Sets a flag that requests the video duration to be limited. Pass -1 for no limits.
Uri - FileProvider output file URI.

VideoView

VideoView is a view that allows you to play video media inside your application.
The VideoView optionally shows a media controller when the user touches the view.
The Completed event is raised when playback is completed.
Simple example of using VideoView:
Sub Globals
  Dim vv As VideoView
End Sub
Sub Activity_Create(FirstTime As Boolean)
  vv.Initialize("vv")
  Activity.AddView(vv, 10dip, 10dip, 250dip, 250dip)
  vv.LoadVideo(File.DirRootExternal, "somefile.mp4")
  vv.Play
End Sub
Sub vv_Complete
  Log("Playing completed")
End Sub

This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

Complete

Members:


  Background As android.graphics.drawable.Drawable

  BringToFront

  Color As Int [write only]

  Duration As Int [read only]

  Enabled As Boolean

  Height As Int

  Initialize (EventName As String)

  Invalidate

  Invalidate2 (arg0 As android.graphics.Rect)

  Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)

  IsInitialized As Boolean

  IsPlaying As Boolean

  Left As Int

  LoadVideo (Dir As String, FileName As String)

  MediaControllerEnabled As Boolean [write only]

  Padding() As Int

  Parent As Object [read only]

  Pause

  Play

  Position As Int

  RemoveView

  RequestFocus As Boolean

  SendToBack

  SetBackgroundImage (arg0 As android.graphics.Bitmap) As BitmapDrawable

  SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)

  SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)

  SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)

  SetVisibleAnimated (arg0 As Int, arg1 As Boolean)

  Stop

  Tag As Object

  Top As Int

  toString As String

  Visible As Boolean

  Width As Int

Members description:

Background As android.graphics.drawable.Drawable
BringToFront
Color As Int [write only]
Duration As Int [read only]
Gets the video duration in milliseconds.
Enabled As Boolean
Height As Int
Initialize (EventName As String)
Initialize the objects and sets the name of the subs that will handle the events.
Invalidate
Invalidate2 (arg0 As android.graphics.Rect)
Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
IsInitialized As Boolean
IsPlaying As Boolean
Tests whether the video is currently playing.
Left As Int
LoadVideo (Dir As String, FileName As String)
Loads a video file and prepares it for playing.
It is not possible to load files from the assets folder.
Advanced: you can pass "http" to the Dir parameter and then a full URL (including http) to the FileName.
In this case the online video will be streamed. Note that you need to add the INTERNET permission for this to work.
MediaControllerEnabled As Boolean [write only]
Sets whether the media controller is enabled. It is enabled by default.
Note that the media player gets attached to the VideoView parent.
Padding() As Int
Parent As Object [read only]
Pause
Pauses the playback.
Play
Starts or resumes playing.
Position As Int
Gets or sets the playing position (in milliseconds).
RemoveView
RequestFocus As Boolean
SendToBack
SetBackgroundImage (arg0 As android.graphics.Bitmap) As BitmapDrawable
SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
Stop
Stops the playback.
Tag As Object
Top As Int
toString As String
Visible As Boolean
Width As Int

Top