FileDocCategorySizeDatePackage
SoundEffect.javaAPI DocAndroid 1.5 API3758Wed May 06 22:42:46 BST 2009com.android.phone

SoundEffect

public class SoundEffect extends Object
Helper class used to play DTMF tones in the Phone app.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
private static android.media.MediaPlayer
sDtmfTone
private static String[]
sDtmfToneFilePath
private static char
sLastDtmfPlayed
Constructors Summary
private SoundEffect()
This class is never instantiated.


          
      
    
Methods Summary
private static java.lang.StringgetDtmfFileNameForIndex(int index)

        String retVal;

        if (index < 10) {
            retVal = String.valueOf(index);
        } else if (index == 10) {
            retVal = "_star";
        } else {
            retVal = "_pound";
        }

        return retVal;
    
static voidinit()

        if (DBG) {
            Log.d(LOG_TAG, "[SoundEffect] init");
        }

        String extension = ".mp3";
        String root = Environment.getRootDirectory().toString();
        String prefix = root + "/sounds/dtmf";

        sDtmfToneFilePath = new String[12];

        int length = sDtmfToneFilePath.length;

        for (int i = 0; i < length; i++) {
            String fileName = getDtmfFileNameForIndex(i);
            StringBuilder buf = new StringBuilder();
            buf.append(prefix);
            buf.append(fileName);
            buf.append(extension);

            sDtmfToneFilePath[i] = buf.toString();
        }
    
static voidplayDtmfTone(char c)

        if (!PhoneNumberUtils.is12Key(c)) {
            Log.e(LOG_TAG,
                    "playDtmfTone called with invalid character '" + c + "'");
        } else {
            // Play the tone for the far side
            PhoneApp.getInstance().phone.sendDtmf(c);
            
            // Don't play these locally for now.
            if (false) {
                // Play the tone locally
                int index = c - '0";
    
                if (c >= '0" && c <= '9") {
                    index = c - '0";
                } else if (c == '*") {
                    index = 10;
                } else if (c == '#") {
                    index = 11;
                }
    
                try {
                    if (DBG) {
                        Log.d(LOG_TAG, "[SoundEffect] playDtmfTone for " + c + ", tone index=" + index);
                    }
    
                    if (sDtmfTone == null) {
                        sDtmfTone = new MediaPlayer();
                    }
                    sDtmfTone.reset();
                    sDtmfTone.setDataSource(sDtmfToneFilePath[index]);
                    sDtmfTone.setAudioStreamType(
                            android.media.AudioManager.STREAM_RING);
                    sDtmfTone.prepare();
                    sDtmfTone.seekTo(0);
                    sDtmfTone.start();
                } catch (Exception ex) {
                    if (DBG) Log.e(LOG_TAG,
                            "[SoundEffect] playDtmfTone caught " + ex);
                }
            }
        }