use of cn.nicolite.palm300heroes.view.oldAdapter.SoundAdapter in project Palm300Heroes by nicolite.
the class HeroSoundFragment method doBusiness.
@Override
protected void doBusiness() {
lRecyclerView.setLayoutManager(new LinearLayoutManager(context, OrientationHelper.VERTICAL, false));
SoundAdapter soundAdapter = new SoundAdapter(context, dataList);
lRecyclerViewAdapter = new LRecyclerViewAdapter(soundAdapter);
lRecyclerView.setAdapter(lRecyclerViewAdapter);
lRecyclerView.setLoadMoreEnabled(false);
heroSoundPresenter = new HeroSoundPresenter(this, this);
lRecyclerView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
heroSoundPresenter.showSound(unCode, true);
}
});
soundAdapter.setOnItemClickListener(new SoundAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, final SoundAdapter.SoundViewHolder viewHolder, int position, List<Sound> soundList) {
String url = Constants.HERO_SOUND_URL + Uri.encode(soundList.get(position).getUrl());
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
if (oldViewHolder != null && !oldViewHolder.equals(viewHolder)) {
oldViewHolder.getPlaySound().setImageResource(R.drawable.ic_media_play);
}
oldViewHolder = viewHolder;
try {
if (dataList.get(position).getUrl().equals(soundList.get(position).getUrl())) {
viewHolder.getPlaySound().setImageResource(R.drawable.ic_media_pause);
}
mediaPlayer.reset();
mediaPlayer.setDataSource(url);
mediaPlayer.setAudioStreamType(STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
viewHolder.getPlaySound().setImageResource(R.drawable.ic_media_play);
mp.stop();
mp.reset();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
viewHolder.getPlaySound().setImageResource(R.drawable.ic_media_play);
mp.stop();
mp.reset();
mp.release();
mediaPlayer = null;
Toast.makeText(getActivity(), "播放失败", Toast.LENGTH_SHORT).show();
return true;
}
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Aggregations