Skip to content

Instantly share code, notes, and snippets.

@bengongon97
Last active September 8, 2020 12:01
Show Gist options
  • Save bengongon97/a213e280840cec95334464c9cf75f7a1 to your computer and use it in GitHub Desktop.
Save bengongon97/a213e280840cec95334464c9cf75f7a1 to your computer and use it in GitHub Desktop.
@SuppressLint("StaticFieldLeak")
public void initializeManagerAndGetPlayList(final Context context) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
HwAudioPlayerConfig hwAudioPlayerConfig = new HwAudioPlayerConfig(context);
HwAudioManagerFactory.createHwAudioManager(hwAudioPlayerConfig, new HwAudioConfigCallBack() {
@RequiresApi(api = Build.VERSION_CODES.R)
@Override
public void onSuccess(HwAudioManager hwAudioManager) {
try {
mHwAudioManager = hwAudioManager;
mHwAudioPlayerManager = hwAudioManager.getPlayerManager();
mHwAudioConfigManager = hwAudioManager.getConfigManager();
mHwAudioQueueManager = hwAudioManager.getQueueManager();
playList = getLocalPlayList(MainActivity.this);
if (playList.size() > 0) {
Collections.sort(playList, new Comparator<HwAudioPlayItem>() {
@Override
public int compare(final HwAudioPlayItem object1, final HwAudioPlayItem object2) {
return object1.getAudioTitle().compareTo(object2.getAudioTitle());
}
});
}
doListenersAndNotifications(MainActivity.this);
} catch (Exception e) {
Log.e("TAG", "player init fail", e);
}
}
@Override
public void onError(int errorCode) {
Log.e("TAG", "init err:" + errorCode);
}
});
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
addListener(mPlayListener); //method coming up in the second part
PlaylistAdapter playlistAdapter = new PlaylistAdapter(playList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
binding.playlistRecyclerView.setLayoutManager(layoutManager);
playlistAdapter.setOnItemClickListener(MainActivity.this);
binding.playlistRecyclerView.setAdapter(playlistAdapter);
super.onPostExecute(aVoid);
}
}.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment