2013-08-28 69 views
0

我在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); 
     } 
    } 

} 
} 

回答

1

您需要在創建MediaPlayer對象創建方法

0

檢查AudioManager.isMusicActive();然後不要發出另一首歌。或嘗試

mediaplayer.reset(); then mediplayer.setDatasource 

還要檢查這個relevant question

相關問題