Code example for ToneGenerator

Methods: release, startTone, stopTone

0
 
            // If the mToneGenerator creation fails, just continue without it. It is 
            // a local audio signal, and is not as important. 
            ToneGenerator toneGenerator;
            try { 
                toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, toneVolume);
                // if (DBG) log("- created toneGenerator: " + toneGenerator); 
            } catch (RuntimeException e) {
                Log.w(THIS_FILE, "InCallTonePlayer: Exception caught while creating ToneGenerator: " + e);
                toneGenerator = null;
            } 
 
            // Using the ToneGenerator (with the CALL_WAITING / BUSY / 
            // CONGESTION tones at least), the ToneGenerator itself knows 
            // the right pattern of tones to play; we do NOT need to 
            // manually start/stop each individual tone, or manually 
            // insert the correct delay between tones. (We just start it 
            // and let it run for however long we want the tone pattern to 
            // continue.) 
            // 
            // TODO: When we stop the ToneGenerator in the middle of a 
            // "tone pattern", it sounds bad if we cut if off while the 
            // tone is actually playing. Consider adding API to the 
            // ToneGenerator to say "stop at the next silent part of the 
            // pattern", or simply "play the pattern N times and then 
            // stop." 
 
            if (toneGenerator != null) {
                toneGenerator.startTone(toneType);
                SystemClock.sleep(toneLengthMillis);
                toneGenerator.stopTone();
 
                Log.v(THIS_FILE, "- InCallTonePlayer: done playing.");
                toneGenerator.release();
            } 
        } 
    } 
 
}