0

我正在使用标签活动,其中有多个歌曲播放列表。所以每当我从一个播放列表播放歌曲并移动到另一个播放列表时,两首歌曲都会同时开始播放。现在我想要的是在我之前的活动中停止音乐仅使用音频管理器从我的新活动中选择任何歌曲而不使用任何服务。那么有人可以帮助我或给我一些指导吗?

代码:-

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();

    }
}

另一个活动

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();
    }



}
4

1 回答 1

2

看看这个: http: //developer.android.com/training/managing-audio/audio-focus.html基本上你会请求音频焦点,然后其他活动会在失去音频焦点时监听。这是正确的解决方案,因为这也意味着如果用户打开音乐播放器,您的应用将停止播放。

于 2013-07-06T20:05:58.420 回答