なぜこれが機能しないのですか?activityClass( PlayListActivity.java
) と serviceClass( AudioService.java
) があります。をクリックするListView
と、曲の再生が始まるはずですが、そうではありません。をクリックするListView
と、エラー( などforced close
)なしでアプリが閉じます。
LogCat で次のエラーが発生しました。
アクティビティ com.example.serviceaudio.PlayListActivity は、元々ここにバインドされていた ServiceConnection com.example.serviceaudio.PlayListActivity$AudioPlayerServiceConnection@41b1c448 を漏らしました
致命的な例外: manin java.lang.RunTimeException: ブロードキャスト インテントの受信中にエラーが発生しました { act=com.example.serviceaudio.AudioService$AudioPlayerBroadCastResiever@41bdf88
ここに私のコードがあります:
PlayListActivity.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);
}
}
AudioService.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);
}
}
}
}