/*** * Excerpted from "Hello, Android", * published by The Pragmatic Bookshelf. * Copyrights apply to this code. It may not be used to create training material, * courses, books, articles, and the like. Contact us if you are in doubt. * We make no guarantees that this code is fit for any purpose. * Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information. ***/ package com.example.audio; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class Audio extends Activity implements OnClickListener { private MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_audio); setVolumeControlStream(AudioManager.STREAM_MUSIC); // Nine numerical buttons findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); findViewById(R.id.button_3).setOnClickListener(this); findViewById(R.id.button_4).setOnClickListener(this); findViewById(R.id.button_5).setOnClickListener(this); findViewById(R.id.button_6).setOnClickListener(this); findViewById(R.id.button_7).setOnClickListener(this); findViewById(R.id.button_8).setOnClickListener(this); findViewById(R.id.button_9).setOnClickListener(this); } public void onClick(View v) { int resId; switch (v.getId()) { case R.id.button_1: resId = R.raw.button_1; break; case R.id.button_2: resId = R.raw.button_2; break; case R.id.button_3: resId = R.raw.button_3; break; case R.id.button_4: resId = R.raw.button_4; break; case R.id.button_5: resId = R.raw.button_5; break; case R.id.button_6: resId = R.raw.button_6; break; case R.id.button_7: resId = R.raw.button_7; break; case R.id.button_8: resId = R.raw.button_8; break; case R.id.button_9: resId = R.raw.button_9; break; default: resId = R.raw.button_1; break; } // Release any resources from previous MediaPlayer if (mp != null) { mp.release(); } // Create a new MediaPlayer to play this sound mp = MediaPlayer.create(this, resId); mp.start(); if (resId == R.raw.button_1) finish(); } }