How do I stop the sound from ringing until the touch finger is lifted?

0

how is it going? Could someone tell me what I would have to do to make the sound stop ringing until I left the button? Try the motion.event actiondown and actionup but the full sound sounds, but I just want it to play until the user releases the touch button? .. I do not know if he has to see the sound (sample) that is in .ogg format ... and I understand that with the media player method it is for long sounds and soundpool for short films ...

 public class MainActivity extends AppCompatActivity {
ImageButton imTecla29,imTecla30;

private SoundPool soundPool;

private int sonTecla29, sonTecla30;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //ORIENTACION

   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    imTecla29=(ImageButton)findViewById(R.id.tecla1);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool =new SoundPool.Builder().setMaxStreams(5).build();
    }else {
        soundPool= new SoundPool(5, AudioManager.STREAM_MUSIC,0);

    }

    sonTecla29=soundPool.load(this, R.raw.acor29,1);

     imTecla29.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                soundPool.play(sonTecla29,1,1,0,0,0);

            }else if(motionEvent.getAction()== MotionEvent.ACTION_UP)
                soundPool.stop();
            return false;
        }
    });
    
asked by luis 03.03.2018 в 19:35
source

2 answers

0

My suggestion is that the playback is handled with AudioManager so you can manipulate using the events according to your interest.

AudioManager.OnAudioFocusChangeListener

If several reproductions happen and they happen partially or permanently, you must also work with the focus to redirect the reproduction and pause those that are not of interest until the event ends.

private Handler mHandler = new Handler();
  AudioManager.OnAudioFocusChangeListener afChangeListener =
  new AudioManager.OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
  if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
    // Permanent loss of audio focus
    // Pause playback immediately
    mediaController.getTransportControls().pause();
    // Wait 30 seconds before stopping playback
    mHandler.postDelayed(mDelayedStopRunnable,
      TimeUnit.SECONDS.toMillis(30));
  }
  else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
    // Pause playback
  } else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
    // Lower the volume, keep playing
  } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
    // Your app has been granted audio focus again
    // Raise volume to normal, restart playback if necessary
  }
}
};

check out this android developer link: AndroidDeveloper. Focus

    
answered by 04.03.2018 в 17:24
-1

The problem that keeps sounding is that you are defining the reproduction of 5 Streams at the same time:

 soundPool= new SoundPool(5, AudioManager.STREAM_MUSIC,0);

If you define only a Stream it should work without problem:

 soundPool= new SoundPool(1, AudioManager.STREAM_MUSIC,0);
    
answered by 07.03.2018 в 18:41