Monday, May 12, 2014

Android SoundPool example

This example show how to use SoundPool to load ogg audio file, in /res/raw/piano117.ogg, implement android.media.SoundPool.OnLoadCompleteListener and play specified sound within SoundPool.


The sample ogg file, piano117.ogg, I download from https://archive.org/details/SynthesizedPianoNotes. Download to your /res/raw folder in your project. Remember to rename it as all in low-case character.

MainActivity.java
package com.example.androidsoundpool;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 Button btnPlay;
 SoundPool soundPool;
 AudioManager audioManager;
 int soundId;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  btnPlay = (Button)findViewById(R.id.play);
  btnPlay.setEnabled(false);
  btnPlay.setOnClickListener(btnPlayOnClickListener);
  
  audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

  //the maximum number of simultaneous streams for this SoundPool object
  int maxStreams = 4;
  //the audio stream type as described in AudioManager
  int streamType = AudioManager.STREAM_MUSIC;
  //the sample-rate converter quality. Currently has no effect. Use 0 for the default.
  int srcQuality = 0;
  
  soundPool = new SoundPool(maxStreams, streamType, srcQuality);
  soundPool.setOnLoadCompleteListener(soundPoolOnLoadCompleteListener);
  soundId = soundPool.load(this, R.raw.piano117, 1);
  
 }
 
 OnLoadCompleteListener soundPoolOnLoadCompleteListener =
  new OnLoadCompleteListener(){

   @Override
   public void onLoadComplete(SoundPool soundPool,
     int sampleId, int status) {
    if(status==0){
     btnPlay.setEnabled(true);
    }else{
     Toast.makeText(MainActivity.this, 
      "SoundPool.load() fail", 
      Toast.LENGTH_LONG).show(); 
    }
    
   }
 };
 
 OnClickListener btnPlayOnClickListener =
  new OnClickListener(){

   @Override
   public void onClick(View v) {
    float vol = audioManager.getStreamVolume(
      AudioManager.STREAM_MUSIC);
       float maxVol = audioManager.getStreamMaxVolume(
         AudioManager.STREAM_MUSIC);
       float leftVolume = vol/maxVol;
       float rightVolume = vol/maxVol;
       int priority = 1;
       int no_loop = 0;
       float normal_playback_rate = 1f;
       soundPool.play(soundId, 
         leftVolume, 
         rightVolume, 
         priority, 
         no_loop, 
         normal_playback_rate);
   }
 };

}

/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidsoundpool.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <Button
        android:id="@+id/play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - PLAY - " />

</LinearLayout>


No comments: