Android Sound Demonstration

This demonstration is based on Java Android code on the EDUmobile website. You need to add a folder named "raw" to the "res" folder of the solution. Highlight the "res" folder in the Solution Explorer, right click then select menu item Add > New Folder. The Solution Explorer now appears as follows.

Solution Explorer

Solution Explorer

The demo needs a file named chimes.wav in the "raw" folder. You can copy chimes.wav from the C:\Windows\Media folder. Note that you do not use its extension in the code:
soundID := soundPool.load(self, R.raw.chimes, 1);
The playing of the sound has an adjustable rate, which increases by 0.5 each button press. The pitch of the sound increases for the first few button presses and then it is not played at all when the rate is too high.

The following screenshot shows the app in action on a Nexus emulator. A pop-up message (Toast) shows the rate.

Screenshot

Screenshot

The code of MainActivity.pas

namespace org.me.sound_demo;

interface

uses
  java.util,  android.app,  android.content, android.os, android.util, android.view, android.widget, android.media.*;
type
  MainActivity = public class(Activity)
  private
    soundPool  : SoundPool ;
    soundID : Integer;
    class var loaded : Boolean  := false;
    class var rateMultiplier : Single := 0.5;
  public
    method onCreate(savedInstanceState: Bundle); override;
    method onResume; override;
    method ButtonOnClick(v : View);
    class method onLoadComplete(soundPool : SoundPool; sampleId, status : Integer);
  end;                 

implementation

method MainActivity.onCreate(savedInstanceState: Bundle);
begin
  inherited;
  // Set our view from the "main" layout resource
  ContentView := R.layout.main;
  // Get our button from the layout resource,
  // and attach an event to it
  var myButton: Button := Button(findViewById(R.id.MyButton));
  myButton.OnClickListener := new interface View.OnClickListener(onClick := @ButtonOnClick);
  // Set the hardware buttons to control the music
  self.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  // Load the sound
  soundPool := new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
  soundPool.setOnLoadCompleteListener(@onLoadComplete);           
  soundID := soundPool.load(self, R.raw.chimes, 1);
end;

method MainActivity.onResume;
begin
  inherited;
  rateMultiplier := 0.5;
end;

class method  MainActivity.onLoadComplete(soundPool: SoundPool; sampleId, status : Integer);
begin
  loaded := true;
end;

method MainActivity.ButtonOnClick(v : View);
begin
  var audioManager := AudioManager(getSystemService(AUDIO_SERVICE));
  var actualVolume := Single(audioManager.getStreamVolume(audioManager.STREAM_MUSIC));                  
  var maxVolume := Single(audioManager.getStreamMaxVolume(audioManager.STREAM_MUSIC));
  var volume := actualVolume / maxVolume;
  if loaded then
    begin
      soundPool.play(soundID, volume, volume, 1, 0, rateMultiplier);
      Toast.makeText(self, 'Rate multiplier: ' + rateMultiplier.toString, Toast.LENGTH_SHORT).show;
      rateMultiplier := rateMultiplier  + 0.5;
    end;
end;

end.

Layout File

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical|center_horizontal">
    <Button
      android:id="@+id/MyButton"
      android:text="@string/my_button_text"
      android:textSize="40sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
  </LinearLayout>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">Sound Demo</string>
  <string name="my_button_text">Play</string>
</resources>
Programming - a skill for life!

How to use EditText, Toast, DatePicker, ListView, RadioGroup, ScrollView and ImageView widgets, sound and files in Oxygene for Java Android apps