Skip to main content

Set Sound Effect On Button In Android

--------------------------------------------------------------------------------------------------------------------------

MainActivity.java

public class SoundEffectsInAndroidActivity extends Activity implements OnClickListener {
    private Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // init Effects class
        Effects.getInstance().init(this);
        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Effects.getInstance().playSound(Effects.SOUND_1);
    }
}

----------------------------------------------------------------------------------------------------------------------
Effect.java

public class Effects {

    private static final String TAG = Effects.class.toString();

    private static final Effects INSTANCE = new Effects();

    // Sound ID can't be 0 (zero)
    public static final int SOUND_1 = 1;

    private Effects() {

    }

    public static Effects getInstance() {
        return INSTANCE;
    }

    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;
    int priority = 1;
    int no_loop = 0;
    private int volume;
    float normal_playback_rate = 1f;

    private Context context;

    public void init(Context context) {
        this.context = context;
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(SOUND_1, soundPool.load(context, R.raw.sound_1, 1));
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public void playSound(int soundId) {
        Log.i(TAG, "!!!!!!!!!!!!!! playSound_1 !!!!!!!!!!");
        soundPool.play(soundId, volume, volume, priority, no_loop, normal_playback_rate);
        Toast.makeText(context, "Now you can hear sound effect!", Toast.LENGTH_LONG).show();
    }

}

-----------------------------------------------------------------------------------------------------------------------
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Click Me" />

</LinearLayout>

Note :Add raw Folder In Resource Folder And Put MP3 One Sound File In raw Folder




Download Source CodeClick Here

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"