Android: How to play Tone sound

    /* Tone play sample code */
    
    private void playTone() {
        ToneGenerator tg = mToneGenerator;
        if (tg != null) {
            tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
        }   	
   }
    
    @Override
    public void onResume() {
    	initializeTone();
    }       
    
    @Override
    public void onPause() {
    	releaseTone();
    }   

    
    private void initializeTone() {
        // Initialize tone generator.
        try {
            mToneGenerator = new ToneGenerator(
                    AudioManager.STREAM_SYSTEM, 50);
        } catch (Throwable ex) {
            Log.w(TAG, "Exception caught while creating tone generator: ", ex);
            mToneGenerator = null;
        }
    }     	
	
	private void releaseTone() {
	    if (mToneGenerator != null) {
            mToneGenerator.release();
            mToneGenerator = null;
        }
	}

你可能感兴趣的:(android,exception,null,System,generator)