我有几个片段,其中一个片段显示了我使用此代码获得的所有专辑的列表:
private void getAlbums() {
final Uri songUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor myCursor = getActivity().getContentResolver().query(songUri, null, null, null, null);
if (myCursor != null && myCursor.moveToFirst()) {
int id_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID); //Get column ALBUM ID
int album_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM); //Get column ALBUM NAME
int artist_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ARTIST); //Get column ALBUM ARTIST
int tracks_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS); //Get column NUMBER OF SONGS IN ALBUM
String temp = myCursor.getString(album_Column);
do {
Long albumId = myCursor.getLong(id_Column);
String album = myCursor.getString(album_Column);
String albumArtist = myCursor.getString(artist_Column);
int tracks = myCursor.getInt(tracks_Column);
if (!temp.equals(album)) {
QueryAlbums queryAlbums = new QueryAlbums(albumId, album, albumArtist, tracks);
albumList.add(queryAlbums);
}
temp = album;
} while (myCursor.moveToNext());
myCursor.close();
}
当用户点击相册时,我会通过用户点击其他活动的位置。 这是用户点击相册时的代码:
recyclerViewAlbums.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onItemClick(View view, int position) {
albumIndex = position;
Toast.makeText(getContext(), "You Clicked position: " + albumIndex + " " + albumList.get(albumIndex).getAlbumName() + " "
+ albumList.get(position).getAlbumid(), Toast.LENGTH_SHORT).show();
Intent selectedAlbum = new Intent(getContext(), AlbumActivity.class);
selectedAlbum.putExtra("albumIndex", albumIndex);
startActivity(selectedAlbum);
}
}));
这是当用户点击显示该专辑中歌曲的专辑时显示的活动。
AlbumActivity.java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_album);
recyclerViewAlbum = findViewById(R.id.recyclerViewAlbum);
//AlbumAdapter
songAdapter = new SongAdapter(getApplicationContext(), songList);
songAdapter.notifyDataSetChanged();
if (recyclerViewAlbum != null){
recyclerViewAlbum.setHasFixedSize(true);
}
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewAlbum.setAdapter(songAdapter);
recyclerViewAlbum.setLayoutManager(linearLayoutManager);
// Load album list and songs inside albums
getAlbums();
getSongsFromAlbum();
/* //Sort songs NOT WORKING!
Collections.sort(songList, new Comparator<QuerySongs>() {
public int compare(QuerySongs a, QuerySongs b) {
return a.getTitle().compareTo(b.getTitle());
}
}); */
albumIvBottom = findViewById(R.id.albumIvBottom);
tvCurrSongTitle = findViewById(R.id.tvCurrSongTitle);
tvCurrSongArtist = findViewById(R.id.tvCurrSongArtist);
recyclerViewAlbum.addOnItemTouchListener(new OnItemClickListeners(getApplicationContext(), new OnItemClickListeners.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
songIndex = position;
playAudio(songIndex);
Toast.makeText(getApplicationContext(), "You Clicked position: " + songIndex + " " + songList.get(songIndex).getData(), Toast.LENGTH_SHORT).show();
//Fetch current song details
tvCurrSongTitle.setText(songList.get(songIndex).getTitle());
tvCurrSongArtist.setText(songList.get(songIndex).getArtist());
//Fetch album art activeSong
loadAlbumArtBottom();
}
}));
}
private void playAudio(int songIndex) {
//Check if service is active
if (!serviceBound) {
StorageUtil storageUtil = new StorageUtil(getApplicationContext());
storageUtil.storeSong(songList);
storageUtil.storeSongIndex(songIndex);
Intent playerIntent = new Intent(getApplicationContext(), MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Store new songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getApplicationContext());
storageUtil.storeSongIndex(songIndex);
//Service is active
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(broadCast_PlAY_NEW_SONG);
sendBroadcast(broadCastReceiverIntent);
}
}
private void loadAlbumArtBottom(){
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, songList.get(songIndex).getAlbumId());
Picasso.with(getApplicationContext())
.load(albumArtUri)
.placeholder(R.drawable.no_album)
.error(R.drawable.no_album)
.resize(220, 220)
.centerCrop()
.into(albumIvBottom);
}
//Store album info in ArrayList
@TargetApi(Build.VERSION_CODES.O)
private void getAlbums() {
final Uri songUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor myCursor = getContentResolver().query(songUri, null, null, null, null);
if (myCursor != null && myCursor.moveToFirst()) {
int id_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID); //Get column ALBUM ID
int album_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM); //Get column ALBUM NAME
int artist_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ARTIST); //Get column ALBUM ARTIST
int tracks_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS); //Get column NUMBER OF SONGS IN ALBUM
String temp = myCursor.getString(album_Column);
do {
Long albumId = myCursor.getLong(id_Column);
String album = myCursor.getString(album_Column);
String albumArtist = myCursor.getString(artist_Column);
int tracks = myCursor.getInt(tracks_Column);
if (!temp.equals(album)) {
QueryAlbums queryAlbums = new QueryAlbums(albumId, album, albumArtist, tracks);
albumList.add(queryAlbums);
}
temp = album;
} while (myCursor.moveToNext());
myCursor.close();
}
}
private void getSongsFromAlbum() {
final Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
bundle = getIntent().getExtras();
int albumIndex = bundle.getInt("albumIndex");
String selection = MediaStore.Audio.Media.ALBUM + "=?";
String songsInAlbum[] = { albumList.get(albumIndex).getAlbumName() };
Cursor myCursor = getContentResolver().query(songUri, null, selection, songsInAlbum, null);
if (myCursor != null && myCursor.moveToFirst()) {
int id_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int albumId_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
int album_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
int data_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
int title_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int artist_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
do {
Long songId = myCursor.getLong(id_Column);
Long albumId = myCursor.getLong(albumId_Column);
String album = myCursor.getString(album_Column);
String songData = myCursor.getString(data_Column);
String songName = myCursor.getString(title_Column);
String songArtist = myCursor.getString(artist_Column);
QuerySongs querySongs = new QuerySongs(songId, albumId, album, songData, songName, songArtist);
songList.add(querySongs);
} while (myCursor.moveToNext());
myCursor.close();
}
}
所以问题是,当我播放一首歌然后点击后退按钮返回我的专辑页面(片段标签)并点击另一张专辑并播放该歌曲时,它仍然播放我之前选择的歌曲。 / p>
知道我造成了什么问题吗?当按下后退按钮时,我尝试清除arraylists,但这不起作用。
提前致谢,
文斯
答案 0 :(得分:0)