Hi I am working on an music app for Android.In that I want to change playback speed & reverb value for music dynamically.
In my view there will be two SeekBar for both playback speed & reverb value, so on changing SeekBar's value sound must change accordingly.
Currently I am using MediaPlayer but after search I found the MediaPlayer doesn't support functionality for changing playback speed.
Then I tried with SoundPool, with this I am able to changes the speed of track.But with SoundPool I am not able to use the big music files.
I want a way where I can change both speed & reverb value.
I am searching for weeks for solution but still I am not able to find it.
Please please help me...
EDIT
float playbackSpeed = 1.5f;
SoundPool soundPool;
int soundId;
void soundPooler()
{
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
final int soundId = soundPool.load("/storage/sdcard0/Music/departed.mp3", 1);
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
#Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2)
{
soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
}
});
}
This is the code I have tried.
Thanks..
Related
I am using soundpool to play sound but i observe that sound gets cut off. Only half of sound plays. I dont know what is happening.
I am using sound when i stretch the arrow and when it touches any object it plays.I am sharing code
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool sp, int sampleId, int status) {
soundPool.play(sampleId, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
soundPool.load(GameActivity.this, R.raw.btn_click, 1);`
Anyone aware about this.
SoundPool is used to play small length of sounds. It have maximum buffer limit of 1MB. It decompresses audio file to pcm format, which depends on sampling rate and length of song. So your file may not need to be actually of 1MB.
For lengthy sounds you can use android's MediaPlayer.
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
When i click button plays sound on my app, but keep clicking for a while doesn`t play anymore. Maybe it is memory issue? Could you solve me my issue? Here is implemented play sound via SoundPool: when i click button i called play method: and play method works background thread
private void play(int resId) {
soundPool = buildBeforeAPI21();//TODO: refactor with deprecated constructor
soundPool.load(activity, Integer.parseInt(resId), 1);
}
public SoundPool buildBeforeAPI21() {
if (soundPool == null) {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (status == 0) {
soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);
}
}
});
}
return soundPool;
}
Don't create a new SoundPool with each click. That is extremely wasteful. Do this instead:
1) Create the SoundPool just once
2) Load it with all the sounds you want to play, just once
3) Wait for all the sounds to load
4) Just call soundPool.play() with each click.
Maybe this answer cames too late; but here we go. The problem is in the line
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
That '2' number is the int maxStreams, following the documentation "the maximum number of simultaneous streams for this SoundPool object" (you can go deep on this here). Now change it and set it to 10, for example, and try again. And after that, you cand adjust it as your wishes. So:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
Hope this helps (if it is still necessary).
I am having a bit of difficulty with my SoundPool. I have an ArrayList<Integer> of my soundIDs located in my res/raw folder. I'm creating the SoundPool like so in my ViewHolder class for my RecyclerView:
//create the SoundPool
sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
//create the AudioManager for managing volume
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
//store the streamVolume in an int
streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//set the load listener for my soundfiles
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
I am playing the sound on the click of an ImageView like so:
#Override
public void onClick(View v) {
if (v.equals(cardImg)) {
sp.load(context, cardData.getSoundId(), 1);
if (loaded) {
sp.play(cardData.getSoundId(), streamVolume, streamVolume, 1, 0, 1f);
}
}
}
And every time I click on the cardImg it gives me this error:
W/SoundPool﹕ sample 2130968609 not READY
Where the sample int changes depending on which card I click, like it should.
So I know that it's GETTING the proper soundID from my ArrayList (stored in CardData class), and it definitely sees that it's loaded, because it wouldn't try to play otherwise because loaded would never be true. My question is: Why are my sounds never ready?
My sounds are .m4a format which Android says is supported in the documentation. I've tried on an Android 5.0 device and an Android 4.4 device but nothing changes.
I have tried using a MediaPlayer but I am running into memory errors as I have many short clips that need to be able to be spammed and I read that a SoundPool was a much better way to manage this (?).
Any help would be appreciated! I am super vexxed here.
You're using the wrong id. SoundPool.load returns an id that is suppose to be passed in to SoundPool.play.
I have a math game with multiple choice questions. I have a bell sound for a right answer and an oink sound for a wrong answer. There is a timed play mode which encourages the player to enter answers rapidly. The sounds play fine if answer buttons are pressed slowly. But if in quick succession, a sound fails to play.
The two sounds are each a fraction of a second long.
In the method that handles correct answers I have ting.start();, and in the method for wrong answers I have oink.start();
One thing I tried was to forcibly stop both sounds and then start only the one I want (see below) but then the sounds don't play at all! Any suggestions?
private void handleWrongAnswer(int chosenID)
{
// Toast.makeText(getApplicationContext(), "Oops, try again.", Toast.LENGTH_SHORT).show();
Button checked_button = (Button) findViewById(chosenID);
checked_button.setEnabled(false);
checked_button.setClickable(false);
checked_button.setBackgroundColor(Color.rgb(255,100,100));
score = score - 1.0/3.0;
score_field.setText(String.format("%.2f", score));
ting.stop();
oink.stop();
oink.start();
}
Well I suppose that you are using Mediaplayer to play that sounds. If you want to play small audio clips and repeat them in small place of time you should use soundPool.
You should implement it this way:
private SoundPool soundPool;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);
}
And then when you want to use it:
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
}
Anyway you have a nice tutorial about this here.
By default, on my phone, clicking on apps, widgets, home button, etc makes a click noise. How do I override that click noise in a widget? I have a widget that plays a noise on click but right now it plays both my noise and the click noise. In the widget, I use soundpool to play the noise. This is the only code I use to access the noise.
public class MakeState { ...
SoundPool snd =new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
int soundID;
...
public void OnClick() {
soundID = snd.load(getcontext(), R.raw.noise, 1);
snd.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool sound pool, int sampleid, int status) {
soundPool.play(soundID, .6f, .6f, 1, 0, 1f);
}
});
}
Try to disable the standard onClick noise using setSoundEffectsEnabled(false);