Tuesday, December 2, 2014

android.media.ToneGenerator simple example

android.media.ToneGenerator provides methods to play DTMF tones (ITU-T Recommendation Q.23), call supervisory tones (3GPP TS 22.001, CEPT) and proprietary tones (3GPP TS 31.111). Depending on call state and routing options, tones are mixed to the downlink audio or output to the speaker phone or headset. This API is not for generating tones over the uplink audio path.


MainActivity.java
package com.example.androidtonegenerator;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {
 
 Button btnGenTone0;
 ToggleButton tgbtnGenTone1;
 
 ToneGenerator toneGenerator1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnGenTone0 = (Button)findViewById(R.id.gentone0);
        tgbtnGenTone1 = (ToggleButton)findViewById(R.id.gentone1);
        
        btnGenTone0.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    int streamType = AudioManager.STREAM_MUSIC;
    int volume = 50;
    ToneGenerator toneGenerator = new ToneGenerator(streamType, volume);
    int toneType = ToneGenerator.TONE_DTMF_0;
    int durationMs = 500;
    toneGenerator.startTone(toneType, durationMs);
   }});
        
        toneGenerator1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 50);
        
        tgbtnGenTone1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    if(isChecked){
     toneGenerator1.startTone(ToneGenerator.TONE_DTMF_1);
    }else{
     toneGenerator1.stopTone();
    }
   }});
    }

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidtonegenerator.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/gentone0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Generate Tone: TONE_DTMF_0" />
    
    <ToggleButton
        android:id="@+id/gentone1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textOn="Stop Tone: TONE_DTMF_1"
        android:textOff="Start Tone: TONE_DTMF_1" />
</LinearLayout>

Next:
- DTMF Piano using ToneGenerator
Sound samples generated by android.media.ToneGenerator

No comments: