0

I am using tab activity where there are multiple playlists of songs..So whenever I am playing song from one playlists and moves to another one,both the songs starts playing simultaneously.Now what I want is to stop music from my previous activity whenever I select any song from my new activity using audio manager only without using any services.So can somebody help me or give me some directions in this?

Code:-

public class AllSongs extends ListActivity implements AudioManager.OnAudioFocusChangeListener{

 private Cursor musiccursor;
 int music_column_index;
 int songPosition;
 int ResumePosition=0;
 MediaPlayer mMediaPlayer;
 String filen,filename;
 ImageView imageView;
 ListAdapter adapter;
 int count,index,top;
 ListView lv;
 Parcelable state;
 View v;
 AudioManager am; 
 int result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    am =   (AudioManager) getSystemService(AUDIO_SERVICE);
    result=am.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
  //Creating list of songs using MediaStore 
mMediaPlayer=new MediaPlayer();
    }

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

    if(result!=AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
        Toast.makeText(getApplicationContext(), "Request not Granted", Toast.LENGTH_LONG).show();
    }
    else{
           mMediaPlayer.reset();
           musiccursor.moveToPosition(position);
           filename = musiccursor.getString(music_column_index);
           mMediaPlayer.setDataSource(filename);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        }


@Override
public void onAudioFocusChange(int focusChange) {
    // TODO Auto-generated method stub
    Log.d("In AudioManager", "adajdjdasdjsd");
    if(focusChange==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
    mMediaPlayer.stop();
    mMediaPlayer.release();
    }
    else{
        mMediaPlayer.stop();

    }
}

Another Activity

public class Artists extends ListActivity implements OnAudioFocusChangeListener{

  setContentView(R.layout.artists);


    manager =   (AudioManager) getSystemService(AUDIO_SERVICE);
    result=manager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);


 @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
f(result!=AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
        Toast.makeText(getApplicationContext(), "Request not Granted", Toast.LENGTH_LONG).show();
    }
    else{
           mMediaPlayer.reset();
           musiccursor.moveToPosition(position);
           filename = musiccursor.getString(music_column_index);
           mMediaPlayer.setDataSource(filename);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        }
      }


 @Override
public void onAudioFocusChange(int focusChange) {
    // TODO Auto-generated method stub
    if(focusChange==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
        mMediaPlayer.stop();
        mMediaPlayer.reset();
        mMediaPlayer.release();
    }



}
dasa
  • 179
  • 4
  • 16

1 Answers1

2

Look into this: http://developer.android.com/training/managing-audio/audio-focus.html Basically you would request audio focus and then the other activity would listen for when it loses audio focus. This is the proper solution because it also means that if the user opens up a music player, your app will stop playing.

Ryan S
  • 4,549
  • 2
  • 21
  • 33
  • I tried it,see my updated question but I am still getting multiple songs being played. – dasa Jul 06 '13 at 20:31
  • have you implemented an onAudioFocusChangeListener? – Ryan S Jul 06 '13 at 20:44
  • Yes I have implemented it. – dasa Jul 06 '13 at 21:07
  • are you using the same activity twice? or is there another activity that also starts playing music? – Ryan S Jul 07 '13 at 00:11
  • I am using another activity,there is no problem in this activity with pause and play but when I am using another activity also only problem arises. – dasa Jul 07 '13 at 00:16
  • In your other activity, you need to call requestAudioFocus, please post code for that activity also – Ryan S Jul 07 '13 at 00:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33023/discussion-between-himanshu-verma-and-rsenapps) – dasa Jul 07 '13 at 11:04