Search in sources :

Example 1 with Sound

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();
            }
        }
    });
}
Also used : Sound(cn.nicolite.palm300heroes.model.bean.Sound) IOException(java.io.IOException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) BindView(butterknife.BindView) LRecyclerView(com.github.jdsjlzx.recyclerview.LRecyclerView) View(android.view.View) IHeroSoundView(cn.nicolite.palm300heroes.view.iview.IHeroSoundView) LRecyclerViewAdapter(com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter) HeroSoundPresenter(cn.nicolite.palm300heroes.presenter.HeroSoundPresenter) SoundAdapter(cn.nicolite.palm300heroes.view.oldAdapter.SoundAdapter) OnRefreshListener(com.github.jdsjlzx.interfaces.OnRefreshListener) MediaPlayer(android.media.MediaPlayer)

Example 2 with Sound

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;
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) Sound(cn.nicolite.palm300heroes.model.bean.Sound) ImageView(android.widget.ImageView) BindView(butterknife.BindView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 3 with Sound

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());
                }
            }
        }
    });
}
Also used : BmobException(cn.bmob.v3.exception.BmobException) BmobQuery(cn.bmob.v3.BmobQuery) Sound(cn.nicolite.palm300heroes.model.bean.Sound) List(java.util.List) SoundDao(cn.nicolite.palm300heroes.db.dao.SoundDao)

Aggregations

Sound (cn.nicolite.palm300heroes.model.bean.Sound)3 View (android.view.View)2 BindView (butterknife.BindView)2 DialogInterface (android.content.DialogInterface)1 MediaPlayer (android.media.MediaPlayer)1 AlertDialog (android.support.v7.app.AlertDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 BmobQuery (cn.bmob.v3.BmobQuery)1 BmobException (cn.bmob.v3.exception.BmobException)1 SoundDao (cn.nicolite.palm300heroes.db.dao.SoundDao)1 HeroSoundPresenter (cn.nicolite.palm300heroes.presenter.HeroSoundPresenter)1 IHeroSoundView (cn.nicolite.palm300heroes.view.iview.IHeroSoundView)1 SoundAdapter (cn.nicolite.palm300heroes.view.oldAdapter.SoundAdapter)1 OnRefreshListener (com.github.jdsjlzx.interfaces.OnRefreshListener)1 LRecyclerView (com.github.jdsjlzx.recyclerview.LRecyclerView)1 LRecyclerViewAdapter (com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter)1 IOException (java.io.IOException)1