the_qwertyas
the_qwertyas

Reputation: 49

Need Button to play .mp3 sound multiple times in succession

1)This is my main activity

package com.art.drumdrum;

import java.util.Timer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Timer time;
MainActivity p;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.kick);

    Button kick = (Button) findViewById(R.id.kick);
    kick.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mpButtonClick.start();
        }
    });

}

}

2) this is my xml manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.art.drumdrum"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.art.drumdrum.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

3)this is my main layout xml ( mainly look at button id "kick")

<RelativeLayout 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:background="#000000"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button6"
    android:layout_alignBottom="@+id/button6"
    android:layout_marginLeft="39dp"
    android:layout_toRightOf="@+id/button6"
    android:text="Button" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button8"
    android:layout_marginTop="15dp"
    android:layout_toRightOf="@+id/button8"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button7"
    android:layout_marginTop="48dp"
    android:layout_toRightOf="@+id/button6"
    android:text="Button" />

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_below="@+id/button5"
    android:layout_marginTop="18dp"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_below="@+id/button1"
    android:layout_marginLeft="14dp"
    android:text="Button" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button6"
    android:layout_marginRight="22dp"
    android:layout_marginTop="22dp"
    android:text="Button" />

<Button
    android:id="@+id/button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button6"
    android:layout_marginTop="22dp"
    android:layout_toLeftOf="@+id/button7"
    android:text="Button" />

<Button
    android:id="@+id/kick"
    style="@style/style"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignBaseline="@+id/button3"
    android:layout_alignBottom="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="25dp"
    android:text="kick" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button6"
    android:layout_alignTop="@+id/button2"
    android:layout_marginTop="33dp"
    android:text="Button" />

</RelativeLayout>

4)Just want each button to have a sound and then be able to keep hitting them and the sounds overlap, right now it waits for the sound to end before it plays the next, even if im just hitting the same key

Upvotes: 1

Views: 454

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

It seems like you only have one MediaPlayer object. If that is true, then it is one reason why you can only play one sample at time. Try using multiple MediaPlayer objects Perhaps one for each button?

I am still not certain if that will work though, it might be a limitation of the MediaPlayer class itself. In this case you'll want to use SoundPool instead, and make sure to set the max streams to something > 1.

It would look something like this:

sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int streamID = sp.load(this, R.raw.sound, 1);
//....
sp.play(streamID, 1, 1, 1, 0, 1);

the 5 is this example is the max streams value, so if you need more than 5 streams at once change it.

Upvotes: 1

Related Questions