Search in sources :

Example 1 with SoundAdapter

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();
            }
        }
    });
}
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)

Aggregations

MediaPlayer (android.media.MediaPlayer)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 View (android.view.View)1 BindView (butterknife.BindView)1 Sound (cn.nicolite.palm300heroes.model.bean.Sound)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