我制作了带有片段的mediaplayer应用;歌曲,专辑,艺术家,流派,播放列表
这是我其中一个片段中的代码,我将以Artists为例。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab3, container, false);
recyclerViewArtists = rootView.findViewById(R.id.recyclerViewArtists);
initRecyclerView();
return rootView;
}
private void initRecyclerView(){
items = Main.songs.getArtists();
if ((items != null) && (! items.isEmpty())) {
adapter = new Adapter(getContext(), items);
recyclerViewArtists.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewArtists.setHasFixedSize(true);
recyclerViewArtists.setAdapter(adapter);
Log.i(TAG, "Artists found in list!");
recyclerViewArtists.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), "You Clicked position: " + position + " " + items.get(position) , Toast.LENGTH_SHORT).show();
if (! Main.songs.isInitialized()){
return;
}
String selectedArtist = items.get(position);
Main.musicList = Main.songs.getSongsByArtist(selectedArtist);
Intent intent = new Intent(getContext(), ListSongsActivity.class);
intent.putExtra("title", selectedArtist);
startActivity(intent);
}
}));
}else{
Log.i(TAG, "No artists found in list!");
}
}
在我的initRecyclerView()方法中,我有一个名为“ items”的数组列表,该列表保存着歌曲艺术家。 Main.songs.getArtist-> Main是一个类,其中包含一个称为“ songs”的公共静态数组列表,并且该数组列表使用模型类Song来保存setter和getter(getArtists();)
现在,当用户单击艺术家时,另一个活动将启动ListSongsActivity.java
这是类ListSongsActivity.java
中的代码 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_songs_layout);
//Holds all the songs in a scrollable list.
recyclerViewListSongs = findViewById(R.id.recyclerViewListSongs);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
// Connects the song list to an adapter
// (thing that creates several Layouts from the song list)
if ((Main.musicList != null) && (!Main.musicList.isEmpty())) {
allSongsAdapter = new AllSongsAdapter(this, Main.musicList);
recyclerViewListSongs.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerViewListSongs.setHasFixedSize(true);
recyclerViewListSongs.setAdapter(allSongsAdapter);
allSongsAdapter.notifyDataSetChanged();
}
mToolbar = findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
if ((getSupportActionBar() != null) && (bundle != null)) {
getSupportActionBar().setTitle((String) bundle.get("title"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home_up);
}
recyclerViewListSongs.addOnItemTouchListener(new OnItemClickListeners(getApplicationContext(), new OnItemClickListeners.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
playAudio(position);
Toast.makeText(getApplicationContext(), "You Clicked position: " + position + Main.musicList.get(position).getData(), Toast.LENGTH_SHORT).show();
}
}));
}
private void playAudio(int songIndex) {
//Check if service is active
if (!MediaPlayerService.musicBound) {
StorageUtil storageUtil = new StorageUtil(this);
storageUtil.storeSong(Main.musicList);
storageUtil.storeSongIndex(songIndex);
Intent playerIntent = new Intent(this, MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, Main.musicConnection, Context.BIND_AUTO_CREATE);
} else {
//Store new songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(this);
storageUtil.storeSong(Main.musicList);
storageUtil.storeSongIndex(songIndex);
//MediaService is active
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(broadCast_PlAY_NEW_SONG);
sendBroadcast(broadCastReceiverIntent);
}
}
在此类中,我存储songList和songIndex,其中songlist =保存歌曲信息的数组列表,songindex =单击位置
public class StorageUtil {
private final String STORAGE = "com.vince_mp3player.STORAGE";
private SharedPreferences mSharedPreferences;
private Context context;
private SharedPreferences.Editor mEditor;
public StorageUtil(Context context){
this.context = context;
}
public void storeSong(List<Song> list){
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
mEditor.putString("songList", json);
mEditor.apply();
}
public ArrayList<Song> getSongs(){
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = mSharedPreferences.getString("songList", null);
Type type = new TypeToken<List<Song>>(){
}.getType();
return gson.fromJson(json, type);
}
public void storeSongIndex(int index) {
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
mEditor.putInt("songIndex", index);
mEditor.apply();
}
public int loadSongIndex() {
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
return mSharedPreferences.getInt("songIndex", -1);//return -1 if no data found
}
public void clearCachedAudioPlaylist() {
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
mEditor.clear();
mEditor.apply();
}
}
这里我的问题开始了,所以说我们从列表中选择Artist A-> ListSongsActivity开始->例如。播放3位艺术家A演唱的歌曲。
然后,当我通过按“后退”按钮返回并选择歌手B并单击其中一首歌曲时,它仍会播放歌手A的歌曲。 出于某种原因,我在应用程序中的所有recyclerviews仅被歌手A的歌曲覆盖。
即使在显示所有歌曲的第一个片段中,当我单击某首歌曲时,它也只会播放我在“艺术家”标签中选择的艺术家的歌曲,而当位置超过3时选择另一首歌曲时,它会崩溃。 >
当我单击后退按钮时,如何更新我的回收站视图?
如果您不理解我的文章,我将在github的保管箱中添加代码,以便您自己进行测试。
预先感谢
文斯
答案 0 :(得分:1)
当您将广播发送到MediaPlayerService
时,songList
变量仍被设置为创建服务时的初始歌曲。我可以通过先更新songList
的{{1}}中的NewSongBroadcastReceiver
来使其工作:
MediaPlayerService.java