Android.Media.Tonegenerator Uncomplicated Example

Posted on

android.media.ToneGenerator provides methods to play DTMF tones (ITU-T Recommendation Q.23), telephone telephone supervisory tones (3GPP TS 22.001, CEPT) as well as proprietary tones (3GPP TS 31.111). Depending on telephone telephone solid set down as well as routing options, tones are mixed to the downlink well or output to the speaker telephone or headset. This API is non for generating tones over the uplink well 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;   world bird 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    world void onClick(View v) {     int streamType = AudioManager.STREAM_MUSIC;     int book = 50;     ToneGenerator toneGenerator = novel ToneGenerator(streamType, volume);     int toneType = ToneGenerator.TONE_DTMF_0;     int durationMs = 500;     toneGenerator.startTone(toneType, durationMs);    }});                  toneGenerator1 = novel ToneGenerator(AudioManager.STREAM_MUSIC, 50);                  tgbtnGenTone1.setOnCheckedChangeListener(new OnCheckedChangeListener(){     @Override    world 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="https://rootandroid.org//"         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 past times android.media.ToneGenerator