Reputation: 179
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();
}
}
Upvotes: 0
Views: 1840
Reputation: 4567
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.
Upvotes: 2