Question

Hello my app generates a list of music player and it plays the music player when user clicks on it,but when i swipe the screen to play the next song,it just won't play the song...here is the code for it.the music stops(because i have set it to stop first) but then nothing happens

public class NowPlaying extends Activity implements Serializable {
     MediaPlayer mp =new MediaPlayer();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.now_playing);
        Intent i = getIntent();
        final int position=i.getIntExtra("Data2", 0);
        final ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1"); 
        SongDetails songDetails = songs.get(position) ;
        Button bfake=(Button) findViewById(R.id.bFake);
        LinearLayout LL=(LinearLayout) findViewById(R.id.LL);
        Button Pause=(Button)findViewById(R.id.bPlayPause);
         Playservice(songs,position,0  );

         bfake.setOnTouchListener(new OnSwipeTouchListener()

         {
                public void onSwipeTop() {
                    mp.stop();
                    mp.release();
                }
                public void onSwipeRight() {
                    //mp.stop();
                    //mp.release();
                    Playservice(songs,position,-1  );
                }
                public void onSwipeLeft() {
                    //mp.stop();
                    //mp.release();
                    Playservice(songs,position,1  );

                }
                public void onSwipeBottom() {
                    mp.stop();
                    mp.release();
                }
            });

            LL.setOnTouchListener(new OnSwipeTouchListener() {
                public void onSwipeTop() {
                    mp.stop();
                    mp.release();
                }
                public void onSwipeRight() {
                    //mp.stop();
                    //mp.release();
                    Playservice(songs,position,-1  );
                }
                public void onSwipeLeft() {
                    //mp.stop();
                    //mp.release();
                    Playservice(songs,position,1  );
                }
                public void onSwipeBottom() {
                    mp.stop();
                    mp.release();
                }
            }); 

            Pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) 
            {
                mp.stop();
                mp.release();
            }
        });
    }
         private void Playservice( ArrayList<SongDetails> songs, int position, int i    ) {
             try {
                 String ab=songs.get(position+i).getPath2().toString();
                    if(mp.isPlaying())
                    {   mp.stop();
                        mp.release();
                    }

                    mp.reset();
                    mp.setDataSource(ab) ;
                    mp.prepare();
                    mp.start();

                } catch (IllegalArgumentException e) {

                    e.printStackTrace();
                } catch (SecurityException e) {

                    e.printStackTrace();
                } catch (IllegalStateException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();

                }

    }

        }
Was it helpful?

Solution

The problem is that you are trying to reuse a MediaPlayer object after it has been released.

so remove mp.release()

if(mp.isPlaying())
                {   mp.stop();
                //    mp.release();
                }

and release the media player when yoyr activity finishes using onDestroy callback:

@Override
public void onDestroy(){
    super.onDestroy();
   if(mp !=null){
      if (mp.isPlaying()){
           mp.stop();
       }
      mp.release();
  }
}

OTHER TIPS

Don't release media player while changing the song- try with-

String ab=songs.get(position+i).getPath2().toString();
                if(mp.isPlaying())
                {   
                     mp.stop();

                }

Try mp.reset() method When you stop music.I hope this will solve your problem. mp is MediaPlayer object.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top