bedesten1618 bedesten1618 - 2 months ago 8
Android Question

When I pressed quickly play button, sound play has failure

I'm working on my first Android project. So i litle novice.

I have 3 buttons for playing sounds. At the same time this sounds has looping.. When pressed play/pause button (iv) everything is normal but when i pressed quickly to play/puse button the sounds named soundID, soundID2, soundID3 not pause, going to play.. Sometime all of the voices are playing at the same time.. My codes are below. How can i set this codes clearly and stable perform.

package com.bebekuyutucu;

import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {

private SoundPool soundPool, soundPool2, soundPool3;
private int soundID, soundID2, soundID3;
boolean plays, loaded, loaded2, loaded3 = false;
float actVolume, maxVolume, volume;
AudioManager audioManager;
int counter,counter2, counter3;
String voice = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv, anne, muzik, supurge, fon, ninni;
final SeekBar seekBar;
final TextView tv = (TextView) findViewById(R.id.textView);


// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actVolume / maxVolume;

//Hardware buttons setting to adjust the media sound
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

// the counter will help us recognize the stream id of the sound played now
counter = 0;
counter2 = 0;
counter3 = 0;

rekamYukle ();

// Load the sounds
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.fon_sesi,1);

soundPool2 = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
soundPool2.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded2 = true;
}
});
soundID2 = soundPool2.load(this, R.raw.annekarni,1);

soundPool3 = new SoundPool(5, AudioManager.STREAM_MUSIC, 1);
soundPool3.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded3 = true;
}
});
soundID3 = soundPool3.load(this, R.raw.annekarni,1);

Typeface cocoFont = Typeface.createFromAsset(getAssets(), "fonts/Cocogoose_trial.otf");
tv.setTypeface(cocoFont);
seekBar = (SeekBar) findViewById(R.id.seekBar);
iv = (ImageView) findViewById(R.id.imageView3);
ninni = (ImageView) findViewById(R.id.imageView4);
anne = (ImageView) findViewById(R.id.imageView5);
supurge = (ImageView) findViewById(R.id.imageView6);
fon = (ImageView) findViewById(R.id.imageView7);
muzik = (ImageView) findViewById(R.id.imageView8);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv.setText(progress + " dk");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

anne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
anne.setImageResource(R.mipmap.annex);
fon.setImageResource(R.mipmap.fon);
muzik.setImageResource(R.mipmap.muzik);
supurge.setImageResource(R.mipmap.supurge);
ninni.setImageResource(R.mipmap.ninni);
voice = "annekarni";

if (voice != "annekarni") {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
iv.setImageResource(R.mipmap.butonbaslat);
}

}
});

muzik.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
anne.setImageResource(R.mipmap.anne);
fon.setImageResource(R.mipmap.fon);
muzik.setImageResource(R.mipmap.muzikx);
supurge.setImageResource(R.mipmap.supurge);
ninni.setImageResource(R.mipmap.ninni);
voice = "muzikkutusu";

if (voice != "muzikkutusu" ) {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
iv.setImageResource(R.mipmap.butonbaslat);
}
}
});

supurge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
muzik.setImageResource(R.mipmap.muzik);
fon.setImageResource(R.mipmap.fon);
anne.setImageResource(R.mipmap.anne);
supurge.setImageResource(R.mipmap.supurgex);
ninni.setImageResource(R.mipmap.ninni);
voice = "supurgesesi";

if (voice != "supurgesesi") {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
iv.setImageResource(R.mipmap.butonbaslat);
}
}
});
fon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
anne.setImageResource(R.mipmap.anne);
fon.setImageResource(R.mipmap.fonx);
muzik.setImageResource(R.mipmap.muzik);
supurge.setImageResource(R.mipmap.supurge);
ninni.setImageResource(R.mipmap.ninni);
voice = "fonsesi";

if (voice != "fonsesi") {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
iv.setImageResource(R.mipmap.butonbaslat);
};
}
});
ninni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
muzik.setImageResource(R.mipmap.muzik);
fon.setImageResource(R.mipmap.fon);
anne.setImageResource(R.mipmap.anne);
supurge.setImageResource(R.mipmap.supurge);
ninni.setImageResource(R.mipmap.ninnix);
voice = "ninnisesi";

if (voice != "ninnisesi") {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
iv.setImageResource(R.mipmap.butonbaslat);
}
}
});

iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tv.getText().toString().equals("0 dk")) {
Toast.makeText(MainActivity.this, "Lütfen sesi seçip, süreyi ayarlayınız.", Toast.LENGTH_SHORT).show();
} else if (voice == null) {
Toast.makeText(MainActivity.this, "Lütfen sesi seçiniz.", Toast.LENGTH_SHORT).show();
}else {
try {
if (voice.equals("fonsesi")){
if (loaded && !plays) {
soundPool.play(soundID, volume, volume, 1, seekBar.getProgress() * 12, 1f);
counter = counter++;
plays = true;
iv.setImageResource(R.mipmap.butondurdur);
}else if (plays) {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
soundPool.autoPause();
soundPool2.autoPause();
soundPool3.autoPause();

soundID = soundPool.load(getApplicationContext(), R.raw.fon_sesi, counter);
plays = false;
iv.setImageResource(R.mipmap.butonbaslat);
}

}else if (voice.equals("annekarni")) {
if (loaded2 && !plays) {

soundPool2.play(soundID2, volume, volume, 1, seekBar.getProgress() * 12, 1f);
counter2 = counter2++;
plays = true;
iv.setImageResource(R.mipmap.butondurdur);
} else if (plays) {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
soundID2 = soundPool2.load(getApplicationContext(), R.raw.annekarni, counter2);
plays = false;
iv.setImageResource(R.mipmap.butonbaslat);
}
}else if (voice.equals("supurgesesi")) {
if (loaded3 && !plays) {
soundPool3.play(soundID3, volume, volume, 1, seekBar.getProgress() * 12, 1f);
counter3 = counter3++;
plays = true;
iv.setImageResource(R.mipmap.butondurdur);
} else if (plays) {
soundPool.stop(soundID);
soundPool2.stop(soundID2);
soundPool3.stop(soundID3);
soundID3 = soundPool3.load(getApplicationContext(), R.raw.supurge, counter3);
plays = false;
iv.setImageResource(R.mipmap.butonbaslat);
}
}

}catch (Exception hata){
Toast.makeText(MainActivity.this, "Hata :"+hata, Toast.LENGTH_SHORT).show();
}
}
}
});
}

private void rekamYukle() {
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
}
}

Answer

The solution: I used soundPool.autoPause(); instead of soundpool.stop(soundID)