Android Projects

Android Development Books

Sunday

Playing Sounds with Android


This tutorial describes the usage of the Android media API for playing sound. It is based on Eclipse 3.7, Java 1.6 and Android 2.3.4 (Gingerbread).

1. Android Sound and Media

1.1. Overview

Android provides two API's for playing sounds. SoundPool and MediaPlayer.
SoundPlayer can be used for small audio clips. It can repeat sounds and play several sounds simultaneously. The sound files played with SoundPool should not exceed 1 MB.
SoundPool does load the file asynchronously. As of Android API8 it is possible to check if the loading is complete via a OnLoadCompleteListener.
Android supports different audio streams for different purposes. The phone volume button can be configured to control a specific audio stream, e.g. during a call the volume button allow increase / decrease the caller volume. To set the button to control the sound media stream set the audio type in your application.

    
context.setVolumeControlStream(AudioManager.STREAM_MUSIC);
   

Mediaplayer is better suited for longer music and movies.

1.2. Prerequisites

The following assumes that you are already familiar with basic Android programming. Please see Android Tutorial for an introduction.

2. Example for SoundPool
We will create an application which will start playing a sound once the finger touches the display. Create an Android project "de.vogella.android.soundpool" with the Activity "PlaySound".
Change the layout "main.xml" to the following.

   
<?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">
 <TextView android:text="Click on the screen to start playing"
  android:id="@+id/textView1" android:layout_width="fill_parent"
  android:layout_height="fill_parent"></TextView>
</LinearLayout>

  

Download a free sound effect from http://hamsterrepublic.com/ohrrpgce/Free_Sound_Effects.html and put it into your "res/raw" folder under the name "sound1.ogg".
Create the following coding for your activity.

   
package de.vogella.android.soundpool;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class PlaySound extends Activity implements OnTouchListener {
 private SoundPool soundPool;
 private int soundID;
 boolean loaded = false;

 
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = findViewById(R.id.textView1); view.setOnTouchListener(this); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.sound1, 1); } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); Log.e("Test", "Played sound"); } } return false; } }  
 

If you press the button your sound should be played. It will use the current volume settings.

More about this articles click here. http://www.vogella.de/articles/AndroidMedia/article.html

and other links with may  help you this articles .
http://developer.android.com/guide/topics/media/index.html
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
http://www.youtube.com/watch?v=KFw765mBcak
http://stackoverflow.com/questions/2220575/how-to-implement-an-audio-player-for-android-using-mediaplayer-and-mediacontroll
http://www.helloandroid.com/tutorials/how-play-video-and-audio-android




Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0