MainActivity.java
package com.example.androiddtmfpiano;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
GridView gridView;
static final String[] numbers = new String[] {
"1", "2", "3",
"4", "5", "6",
"7", "8", "9",
"*", "0", "#"};
static final int[] toneTypes = new int[]{
ToneGenerator.TONE_DTMF_1,
ToneGenerator.TONE_DTMF_2,
ToneGenerator.TONE_DTMF_3,
ToneGenerator.TONE_DTMF_4,
ToneGenerator.TONE_DTMF_5,
ToneGenerator.TONE_DTMF_6,
ToneGenerator.TONE_DTMF_7,
ToneGenerator.TONE_DTMF_8,
ToneGenerator.TONE_DTMF_9,
ToneGenerator.TONE_DTMF_S, //*
ToneGenerator.TONE_DTMF_0,
ToneGenerator.TONE_DTMF_P //#
};
static int streamType = AudioManager.STREAM_MUSIC;
static int volume = 50;
static int durationMs = 100;
static final ToneGenerator toneGenerator =
new ToneGenerator(streamType, volume);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
toneGenerator.startTone(toneTypes[position], durationMs);
}});
}
}
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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androiddtmfpiano.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" />
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
Next example:
- Visible DTMF Piano: Visualizer + ToneGenerator
No comments:
Post a Comment