57667

Mediaplayer plays twice

I have a media player but when another file is selected it continues to play the old file and new one so it is playing two files at once here is my onCreate method

private MediaPlayer mediaplayer = new MediaPlayer(); private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.songplaying); // Getting Our Extras From Intent Bundle names = getIntent().getExtras(); // Getting Specific Data path = names.getString("pathkeyword"); //Start Player try { playAudio(path); } catch (Exception e) { e.printStackTrace();

}

and this is the method that plays the audio

private void playAudio(String url) throws Exception{ mediaplayer.release(); mediaplayer.setDataSource(url); mediaplayer.prepare(); mediaplayer.start();

Answer1:

When you are start to play the song ,check it is playing or not and stop it if it is currently playing.

if(player.isPlaying()) { mediaplayer.stop(); } mediaplayer.reset(); mediaplayer.setDataSource(url); mediaplayer.prepare(); mediaplayer.start();

no need to release the player.player.release() used only when player no longer needed .

And you have to use stop() and release() methods whenever activity destroys.Otherwise so many players are running in background.

Answer2:

Try to add this to oncreate method so you will be able to prevent the new creation of audio

Mediaplayer M = Mediaplayer.create(this,R.row.audio file)

and make a new function like

void my function { // call it here m.start(); }

Recommend

  • Android:Got NumberFormatExxception while getting data from EditText field
  • Clear Cache App: Why it doesn't work?
  • SSRS Expression-Sum two fields
  • How to convert 0s to NaNs in certain columns of dataframe in R?
  • Mysql get closest numeric result comparing two columns (lat and long) at same time
  • Sparql query from Dbpedia and another graph returns less results than expected
  • Listbox generates secondary Listbox based on selection
  • Calculating rounded percentage in Shell Script without using “bc”
  • R tbats Model Seasonal Customer Flag No Results
  • Changing co-ordinate system
  • Grep number extraction
  • NotificationListenerService works intermittently for Oreo
  • Why can I not initialize an array by passing a pointer to a function?
  • Infer constraints for both if and else of type equality
  • How might I grab all windows with a certain word in their titles?
  • How to replace a contour (rectangle) in an image with a new image using Python?
  • TabLayout cannot be resolved to a type- Eclipse
  • Android Notification Without R.java
  • Why do I have to use LD_PRELOAD for libcl.2 to use XML::LibXML::Common on HP/UX?
  • Why must we Forward Declare a class and include the corresponding header file in a header file
  • Listview - list item selection and initial setting (custom made items)
  • how can I key rotate for google cloud storage service account?
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • Unable to resolve Static method?
  • Fragment gives me an error while inflating 1
  • Correct implementation of List Iterator methods
  • How to get listview position?
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Jackson Parser: ignore deserializing for type mismatch
  • Change multiple background-images with jQuery
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • Importing jscolor library in angular 2
  • align graphs with different xlab
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize