0

我正在 Android 中创建音乐播放器。播放歌曲时出现问题:不知道是我的逻辑问题还是我的Service Class的实现问题。

问题是当我单击 ListView 项目时,歌曲会播放,但当我再次单击它时,歌曲会再次播放。我按多少次ListViewItem就会同时播放多少首歌曲。

这是我的代码:

播放列表活动.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;
    }
}}

音乐服务.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);
        } 
    }

}
}
4

2 回答 2

1

您需要在 On Create 方法中创建 MediaPlayer 对象

于 2014-03-18T06:37:17.873 回答
0

检查AudioManager.isMusicActive();然后不要发布其他歌曲。或尝试

mediaplayer.reset(); then mediplayer.setDatasource 

还要检查这个相关问题

于 2015-02-25T11:30:03.720 回答