音乐播放器中的绑定服务多次播放歌曲,重叠

时间:2013-08-28 15:41:19

标签: android android-music-player

我正在Android中创建一个音乐播放器。我在播放歌曲时遇到问题:我不知道问题是我的逻辑还是我的服务类的实现。

问题是当我点击ListView项目时,歌曲会播放,但当我再次点击它时,歌曲将再次播放。我多次按下ListViewItem是同时播放的歌曲数量很多。

这是我的代码:

PlayListActivity.java

public class PlayListActivity extends ListActivity {

public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private ServiceConnection serviceConnection = new AudioPlayerServiceConnection();
private MusicService audioPlayer;
private Intent audioPlayerIntent;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    audioPlayerIntent = new Intent(this, MusicService.class);
    bindService(audioPlayerIntent, serviceConnection, Context.BIND_AUTO_CREATE);


    ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    SongManager plm = new SongManager();
    this.songsList = plm.getPlayList(this);

    for (int i = 0; i < songsList.size(); i++) {
        HashMap<String, String> song = songsList.get(i);
        songsListData.add(song);
    }// end loop

    ListAdapter adapter = new SimpleAdapter(this, songsListData,
            R.layout.playlist_item, new String[] { "songTitle","songArtist"}, new int[] {
                    R.id.songTitle, R.id.songArtist });
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            int songIndex = position;

        playSongSong(songIndex);

        //finish();

        }

    });
}

public void playSongSong(int song){
    Intent intent = new Intent(MusicService.PLAY_TRACK);
    intent.putExtra("songIndex", song);
    this.sendBroadcast(intent);

    }


@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    unbindService(serviceConnection);
    super.onDestroy();

}



private final class AudioPlayerServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName className, IBinder baBinder) {
        audioPlayer = ((MusicService.AudioPlayerBinder) baBinder).getService();
        startService(audioPlayerIntent);
    }

    public void onServiceDisconnected(ComponentName className) {
        audioPlayer = null;
    }
}}

MusicService.java

public class MusicService extends Service implements OnCompletionListener {
public static final String INTENT_BASE_NAME =     "com.example.serviceaudio.MusicService";
public static final String PLAY_TRACK = INTENT_BASE_NAME + ".PLAY_TRACK";
private ArrayList<HashMap<String, String >> songList = new ArrayList<HashMap<String, String>>();
private SongManager songManager = new SongManager();
private MediaPlayer mp;
private AudioPlayerBroadcastReceiver broadcastReceiver = new     AudioPlayerBroadcastReceiver();

private final String TAG = "MusicService";




public class AudioPlayerBinder extends Binder {
    public MusicService getService() {
        Log.v(TAG, "AudioPlayerBinder: getService() called");
        return MusicService.this;
    }
}

private final IBinder audioPlayerBinder = new AudioPlayerBinder();

@Override
public IBinder onBind(Intent intent) {
    Log.v(TAG, "AudioPlayer: onBind() called");
    return audioPlayerBinder;
}

@Override
public void onCreate() {
    Log.v(TAG, "AudioPlayer: onCreate() called");
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(PLAY_TRACK);
    registerReceiver(broadcastReceiver, intentFilter);

    songList = songManager.getPlayList(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
    Log.i(TAG, "AudioPlayer: onDestroy() called");

}


@Override
public void onLowMemory() {
    stopSelf();
}

public void onCompletion(MediaPlayer _mediaPlayer) {

}


private void  playSong(int songIndex){

    try {
        mp = new MediaPlayer();
        mp.reset();
        mp.setDataSource(songList.get(songIndex).get("songPath"));
        mp.prepare();
        mp.start();
        // Displaying Song title
        String songTitle = songList.get(songIndex).get("songTitle");        updateProgressBar();            
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) { 
        e.printStackTrace();
    }
}


private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int currentSongIndex = intent.getExtras().getInt("songIndex");
        Log.d(TAG, "Received intent for action " + intent.getAction() + " for id: " + currentSongIndex);

        if( PLAY_TRACK.equals(action)) {
            playSong(currentSongIndex);
        } 
    }

}
}

2 个答案:

答案 0 :(得分:1)

您需要在On Create方法

中创建MediaPlayer对象

答案 1 :(得分:0)

检查AudioManager.isMusicActive();然后不要再发一首歌。或尝试

mediaplayer.reset(); then mediplayer.setDatasource 

还要检查此relevant question