65908

How to play sound by onTouch and stop when fingures up

Question:

When I touch on Image, the media player starts but on removing fingure from that image, media playes does'nt stop.Please help me guys.

iv.setOnTouchListener(this); mp1 = MediaPlayer.create(getApplicationContext(), R.raw.air);

@Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mp1 = MediaPlayer.create(getApplicationContext(), R.raw.airhorn_1); mp1.start(); mp1.setLooping(true); mp1.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp1) { // TODO Auto-generated method stub mp1.release(); } }); Log.i("Action", "Down"); break; case MotionEvent.ACTION_MOVE: Log.i("Action", "Move"); Log.i("On Long", "On Long"); break; case MotionEvent.ACTION_CANCEL: mp1.pause(); Log.i("Action", "Cancel"); break; case MotionEvent.ACTION_UP: mp1.pause(); //mp1.stop(); Log.i("Action", "up"); break; default: break; } return true; }

Answer1:

public void onTouch(View v) { if (mp.isPlaying()) { mp.stop(); } else { mp.start(); }

This will check when you Touch the ImageView if the Media Player is playing, if it is it will stop it, if not it will start it. With this you shouldn't need to use the switch case.

Be sure to set up your media player before this.

Recommend

  • Android Click button behind another button
  • Getting All x and y coordinates from a motion event in android
  • How to get CPU usage in flash?
  • Dart : Isolate not working when using html import
  • Android Inter-app relations
  • How to prevent keyboard to show in EditText onTouch?
  • Using the Menu, Back, Home buttons with AIR for Android, Flash CS6
  • URL detection adobe air desktop widget
  • How do I increase font size in RStudio Help pane?
  • Laravel lmutator $this->attributes return 'Undefined index: id'
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • Count from each distinct date, fill in missing dates with zero
  • Using Generics on right hand side in Java 6?
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Play WS (2.2.1): post/put large request
  • Get data from AJAX - How to
  • NHibernate Validation Localization with S#arp Architecture
  • Initializer list vs. initialization method
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • Spring security and special characters
  • JFileChooser in front of fullscreen Swing application
  • PHP - How to update data to MySQL when click a radio button
  • Trying to switch camera back to front but getting exception
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Getting Messege Twice Using IMvxMessenger
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • Programmatically clearing map cache
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?