Я пытаюсь использовать библиотеку StandOutWindow, в которой я пытался создать MusicPlayer из этого Это руководство Мой ListView показывает песни, но когда я нажимаю на песни, происходит сбой приложения. Что мне не хватает? Спасибо.
Основное действие
public void createAndAttachView(int id, FrameLayout frame) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.musicplayer, frame, true);
songView = (ListView) view.findViewById(R.id.lvMusicPlayer_songList);
songList = new ArrayList<Song>();
getSonglist();
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdapter = new SongAdapter(this, songList);
songView.setAdapter(songAdapter);
songView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
musicService.playSong();
}
});
}
private void getSonglist() {
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null,
null);
if (musicCursor != null && musicCursor.moveToFirst()) {
int titleColumn = musicCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songList.add(new Song(thisId, thisTitle, thisArtist));
} while (musicCursor.moveToNext());
}
}
Музыкальная служба
public void onCreate() {
super.onCreate();
songPosition = 0;
random = new Random();
mp = new MediaPlayer();
initMusicPlayer();
}
private void initMusicPlayer() {
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(this);
mp.setOnErrorListener(this);
}
public void setList(ArrayList<Song> theSong) {
songs = theSong;
}
public class MusicBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return musicBind;
}
@Override
public boolean onUnbind(Intent intent) {
mp.stop();
mp.release();
return false;
}
public void playSong() {
mp.reset();
Song playSong = songs.get(songPosition);
songTitle = playSong.getTitle();
long currSong = playSong.getID();
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
try {
mp.setDataSource(getApplicationContext(), trackUri);
} catch (Exception e) {
Log.e("MUSIC SERVICR", "Error setting data source", e);
}
mp.prepareAsync();
}
public void setSong(int songIndex) {
songPosition = songIndex;
}
@Override
public void onCompletion(MediaPlayer mp) {
if (mp.getCurrentPosition() > 0) {
mp.reset();
playNext();
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("MUSIC PLAYER", " Playback Error");
mp.reset();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
public int getPosition() {
return mp.getCurrentPosition();
}
public int getDur() {
return mp.getDuration();
}
public boolean isPng() {
return mp.isPlaying();
}
public void pausePlayer() {
mp.pause();
}
public void seek(int position) {
mp.seekTo(position);
}
public void go() {
mp.start();
}
public void playPrev() {
songPosition--;
if (songPosition < 0)
songPosition = songs.size() - 1;
playSong();
}
public void playNext() {
if (shuffle) {
int newSong = songPosition;
while (newSong == songPosition) {
newSong = random.nextInt(songs.size());
}
songPosition = newSong;
} else {
songPosition++;
if (songPosition >= songs.size())
songPosition = 0;
}
playSong();
}
public void onDestroy() {
stopForeground(true);
;
}
public void setShuffle() {
if (shuffle)
shuffle = false;
else
shuffle = true;
}
ЛОГКАТ
Вам нужно добавить публичный метод songPicked() в свою активность.