正文  多媒体 > 音频/Audio >

Android: How to play Tone sound 播放音乐代码

/* Tone play sample code */ private void playTone() { ToneGenerator tg = mToneGenerator; if (tg != null) { tg.sta......

/* 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;
        }
	}