1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.moral.yunfushao.activity;
 
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
 
import com.moral.andbrickslib.baseadapter.headandfooter.DividerItemDecoration;
import com.moral.andbrickslib.baseadapter.recyclerview.MultiItemTypeAdapter;
import com.moral.yunfushao.R;
import com.moral.yunfushao.adapter.SongAdapter;
import com.moral.yunfushao.base.BaseListActivity;
import com.moral.yunfushao.model.Song;
import com.moral.yunfushao.utils.AudioUtils;
import com.moral.yunfushao.utils.PermissionUtil;
 
import java.io.IOException;
import java.util.ArrayList;
 
/**
 * Created by haijiang on 2017/7/12.
 */
 
public class ChooseSongActivity extends BaseListActivity implements SongAdapter.OnBtnClickListener {
    private SongAdapter adapter;
    private ArrayList<Song> voiceList = new ArrayList<>();
    AssetManager am;
    MediaPlayer mp;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                setNormalView();
                mPtrFrame.refreshComplete();
                adapter.notifyDataSetChanged();
            }
        }
    };
    private int index;
 
    @Override
    protected void initOtherView() {
        PermissionUtil.verifyStoragePermissions(this);
        index = getIntent().getIntExtra("position", 0);
        tv_title.setText("本地铃声");
        DividerItemDecoration line = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        mRecyclerView.addItemDecoration(line);
        adapter = new SongAdapter(mRecyclerView, R.layout.list_item_voice, voiceList);
        adapter.setOnBtnClickListener(this);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
 
            }
 
            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }
 
    @Override
    protected void onLoadMore() {
 
 
    }
 
    @Override
    protected void onRefresh() {
        getData();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            mp.release();
        }
    }
 
    @Override
    protected void getBundleExtras(Bundle extras) {
 
    }
 
    @Override
    protected void initListener() {
 
    }
 
    Thread mThread;
 
    @Override
    protected void initData() {
        getData();
    }
 
    private void getData() {
        setLoadingView();
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                ArrayList<Song> temp = AudioUtils.getAllSongs(ChooseSongActivity.this);
                if (temp != null) {
                    voiceList.clear();
                    voiceList.addAll(temp);
                    mHandler.sendEmptyMessage(1);
                }
            }
        });
        mThread.start();
    }
 
    @Override
    protected void processClick(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
        }
 
    }
 
    @Override
    protected void onErrorPageClick() {
 
    }
 
    @Override
    public void playClick(int position) {
        Song voiceBean = voiceList.get(position);
        if (voiceBean.isPlay()) {
            mp.stop();
            voiceBean.setPlay(false);
        } else {
            if (mp != null && mp.isPlaying()) {
                mp.stop();
                mp.release();
                for (Song voice : voiceList) {
                    voice.setPlay(false);
                }
                adapter.notifyDataSetChanged();
            }
            mp = new MediaPlayer();
            try {
                mp.setDataSource(voiceBean.getFileUrl());
                mp.prepare();
                mp.start();
                voiceBean.setPlay(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        adapter.notifyDataSetChanged();
    }
 
    @Override
    public void setClick(int position) {
        Intent intent = new Intent();
        intent.putExtra("position", index);
        intent.putExtra("url", voiceList.get(position).getFileUrl());
        intent.putExtra("name", voiceList.get(position).getTitle());
        setResult(RESULT_OK, intent);
        finish();
    }
}