为什么这不起作用?我有activityClass( PlayListActivity.java
) 和serviceClass( AudioService.java
)。当我点击 时ListView
,歌曲应该开始播放,但这并没有发生。当我单击 时ListView
,它会关闭应用程序而不会出现错误(例如forced close
)。
我在 LogCat 中收到此错误:
Activity com.example.serviceaudio.PlayListActivity 泄露了原来绑定在这里的ServiceConnection com.example.serviceaudio.PlayListActivity$AudioPlayerServiceConnection@41b1c448
致命异常:manin java.lang.RunTimeException:接收广播意图时出错 { act=com.example.serviceaudio.AudioService$AudioPlayerBroadCastResiever@41bdf88
这是我的代码:
播放列表活动.java
public class PlayListActivity extends ListActivity {
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private ServiceConnection serviceConnection = new AudioPlayerServiceConnection();
private AudioService audioPlayer;
private Intent audioPlayerIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
audioPlayerIntent = new Intent(this, AudioService.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) {
playSong(position);
finish();
}
});
}
private final class AudioPlayerServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName className, IBinder baBinder) {
audioPlayer = ((AudioService.AudioPlayerBinder) baBinder).getService();
startService(audioPlayerIntent);
}
public void onServiceDisconnected(ComponentName className) {
audioPlayer = null;
}
}
public void playSong(int songIndex){
Intent intent = new Intent(AudioService.PLAY_SONG);
intent.putExtra("songIndex", songIndex);
this.sendBroadcast(intent);
}
}
音频服务.java
public class AudioService extends Service implements OnCompletionListener{
public static final String PLAY_SONG_BASE = "com.example.serviceaudio.AudioService";
public static final String PLAY_SONG = PLAY_SONG_BASE + ".PLAY_SONG";
private ArrayList<HashMap<String, String >> songList = new ArrayList<HashMap<String, String>>();
private SongManager songManager = new SongManager();
private MediaPlayer mp;
private AudioPlayerBroadcastReceiver broadcastReceiver = new AudioPlayerBroadcastReceiver();
public int currentSongIndex;
public class AudioPlayerBinder extends Binder {
public AudioService getService() {
return AudioService.this;
}
}
private final IBinder audioPlayerBinder = new AudioPlayerBinder();
@Override
public IBinder onBind(Intent intent) {
return audioPlayerBinder;
}
@Override
public void onCreate() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(PLAY_SONG);
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onStart(Intent intent, int startId) {
}
@Override
public void onDestroy() {
//release();
}
@Override
public void onLowMemory() {
stopSelf();
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
private void release() {
if( mp == null) {
return;
}
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
//==== playSong() ========
public void playSong(int songIndex){
// Play song
songList = songManager.getPlayList(this);
try {
mp.reset();
mp.setDataSource(songList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songList.get(songIndex).get("songTitle");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// == End of playSong() ===
private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
currentSongIndex = intent.getExtras().getInt("songIndex");
if( PLAY_SONG.equals(action)) {
playSong(currentSongIndex);
}
}
}
}