Solved

Looping multiple sounds with SoundPool

Posted on 2014-03-01
4
966 Views
Last Modified: 2014-03-18
I am aiming to play and loop multiple sounds at the same time using the soundpool class in java.  I am new at this and it appears I am not able to loop more than one sound at at time.  I can play multiple sounds at the same time but when i set the loop parameter to anything other than zero I am limited to only being able to play one sound at a time.  

Here is the code.

package com.example.poolsoundsample;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	Button button1;
	SoundPool sp;
	
	Button button2;
	SoundPool sp2;

	int pianoa = 0;
	int pianob = 0;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sp = new SoundPool(99, AudioManager.STREAM_MUSIC,0);
		pianoa = sp.load(this, R.raw.bassloop2, 1);
		
		 button1 = (Button) findViewById(R.id.button1);
	        button1.setOnClickListener(ButtonClickListener);
		
	        sp2 = new SoundPool(99, AudioManager.STREAM_MUSIC,0);
			pianob = sp2.load(this, R.raw.bassloop3, 1);
			
			 button2 = (Button) findViewById(R.id.button2);
		        button2.setOnClickListener(ButtonClickListener);
	}

	
	private OnClickListener ButtonClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//if (sound1 != 0)
			//	sp.play(sound1, 1, 1, 0, 0, 1);	
			switch(v.getId()) {
////////////////////////////////Start button 1//////////////////////////
			        case R.id.button1:
				        	if (pianoa != 0){
				        		sp2.play(pianob, 1, 1, 0, -1, 1);}
			        		break;
			        case R.id.button2:
			        	if (pianob != 0){
			        		sp2.play(pianob, 1, 1, 0, -1, 1);}
		        		break;
							}
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	}

Open in new window

0
Comment
Question by:ForLoop5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Author Comment

by:ForLoop5
ID: 39898865
Do you think I have to do them in different threads?  Is it possible to loop multiple sound pools at once?
0
 
LVL 12

Accepted Solution

by:
satsumo earned 500 total points
ID: 39912353
Hello ForLoop5. You only require one sound pool to do this. You have created a pool with 99 streams, so it could potentially play 99 sounds simultaneously. You can load both sounds into the same pool and play them looping or otherwise in the same pool.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tomcat 9 + java 8 error while trying to deploy a war file 2 856
Java Inheritance super keyword use 8 72
phone area codes 3 43
Firefox and firebub 5 27
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question