use of cn.nicolite.palm300heroes.model.bean.Sound 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();
}
}
});
}
use of cn.nicolite.palm300heroes.model.bean.Sound in project Palm300Heroes by nicolite.
the class SoundAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final SoundViewHolder holder, final int position) {
Sound sound = dataList.get(position);
holder.getSoundContent().setText(sound.getContent());
if (onItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClick(holder.itemView, holder, position, dataList);
}
});
}
holder.getSoundContent().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
new AlertDialog.Builder(context).setMessage("确认下载?").setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showToastShort("已开始下载");
DownLoadUtils.downLoadManager(context, DownLoadUrlHead + Uri.encode(dataList.get(position).getUrl()), "/P300Heroes/Sound", "audio/mpeg");
}
}).setNegativeButton("取消", null).show();
return true;
}
});
}
use of cn.nicolite.palm300heroes.model.bean.Sound in project Palm300Heroes by nicolite.
the class HeroSoundPresenter method showSound.
public void showSound(final String UNCode, boolean isForce) {
final SoundDao soundDao = getDaoSession().getSoundDao();
if (!isForce && getView() != null) {
List<Sound> list = soundDao.queryBuilder().where(SoundDao.Properties.UNCode.eq(UNCode)).list();
if (!ListUtils.isEmpty(list)) {
getView().showSound(list);
return;
}
}
BmobQuery<Sound> query = new BmobQuery<>(Constants.BMOB_Sound);
query.addWhereEqualTo("UNCode", UNCode).findObjects(new FindListener<Sound>() {
@Override
public void done(final List<Sound> list, BmobException e) {
if (getView() != null) {
if (e == null) {
getView().showSound(list);
if (!isSaving) {
isSaving = true;
new Thread(new Runnable() {
@Override
public void run() {
List<Sound> oldList = soundDao.queryBuilder().where(SoundDao.Properties.UNCode.eq(UNCode)).list();
for (Sound sound : oldList) {
soundDao.delete(sound);
}
for (Sound sound : list) {
soundDao.insert(sound);
}
isSaving = false;
}
}).start();
}
} else {
getView().loadFailure();
BmobUtils.showErrorMessage(e.toString());
}
}
}
});
}
Aggregations