Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a button to repeat a sound each time its clicked?
    primarykey
    data
    text
    <p>When I press a button it plays the sound fine but when I click it again nothing happens. I want it to repeat the sound each time its clicked? Any ideas??</p> <p>Thanks for looking. :-)</p> <p>package com.example.sherlock;</p> <pre><code>import java.io.IOException; import android.app.Activity; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Conent extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content); final MediaPlayer mp = new MediaPlayer(); Button b = (Button) findViewById(R.id.button1); final MediaPlayer mp1 = new MediaPlayer(); Button c = (Button) findViewById(R.id.Button2); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(mp.isPlaying()) { mp.stop(); mp.reset(); } try { AssetFileDescriptor afd; afd = getAssets().openFd("s1.mp3"); mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); mp.prepare(); mp.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); c.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(mp1.isPlaying()) { mp1.stop(); mp1.reset(); } try { AssetFileDescriptor afd; afd = getAssets().openFd("s2.mp3"); mp1.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); mp1.prepare(); mp1.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload