0

Soundpool stop function not working. I have many images which image is in the view that time i need to play that image related sound, I tried using soundpool object music is playing nice.but my problem is in scrolling time if i speedly move 1 image to 2 image that bith 1,2 image relted sound are played. but i need to set when the 2 image is view position that time automatically i need to stop 1 image relted sound,i need to play only 2 image relted sound.

Thanks,

package com.example.pictures;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Pictures extends Activity implements OnPageChangeListener{
SoundManager snd; 
int sound1,sound2,sound3;
View view=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picturespage);
 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(0); 
 myPager.setOnPageChangeListener(this);
snd = new SoundManager(this);
 sound1 = snd.load(R.raw.sound1);
 sound2 = snd.load(R.raw.sound2);
 sound3 = snd.load(R.raw.sound3);

}
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void onPageSelected(int position) {
    // TODO Auto-generated method stub

    switch (position) {
    case 0:                
            snd.play(sound1);
            lastPlayed = sound1;
        break;
    case 1:
            snd.stop(lastPlayed);
        snd.play(sound2);
            lastPlayed = sound2;
        break;
    case 2:
            snd.stop(lastPlayed);
            snd.play(sound3);
            lastPlayed = sound3;
        break;
    case 3:
        Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
        break;
    case 4:
       Toast.makeText(this, "test2", Toast.LENGTH_SHORT).show();
        break;
    case 5:
        Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();
        break;
    }
}
};

Soundmanager class;

public class SoundManager {
//....
//....
//....

    public SoundManager(Context appContext)
    {
      sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
      pContext = appContext;
    }

    // Load up a sound and return the id
    public int load(int sound_id)
    {
        return sndPool.load(pContext, sound_id, 1);
    }

    // Play a sound
    public int play(int sound_id)
    {
         return sndPool.play(sound_id, leftVolume, rightVolume, 1, 0,rate);     

    }

    public void stop(int sound_id) {
           sndPool.stop(sound_id);           
    }
}
Erdem Azaklı
  • 255
  • 1
  • 8
  • 27

1 Answers1

2

I can't understand much of what you're saying, but just looking at your code you are using the stop function incorrectly.

The stop function is supposed to receive a stream id, not a sound id.

  • A stream id is the value returned from the play function, and identifies a particular instance of a playing track.

  • A sound id is the value returned from a load function, and identifies a loaded resource.

Tim
  • 35,413
  • 11
  • 95
  • 121