From 82cc6c94fdc08b1814fbf7195e88cf335eed3e8d Mon Sep 17 00:00:00 2001
From: haijiang <181069201@qq.com>
Date: Wed, 19 Sep 2018 14:00:15 +0800
Subject: [PATCH] 提交

---
 app/src/main/java/com/moral/yunfushao/activity/VoiceSetActivity.java |  178 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 147 insertions(+), 31 deletions(-)

diff --git a/app/src/main/java/com/moral/yunfushao/activity/VoiceSetActivity.java b/app/src/main/java/com/moral/yunfushao/activity/VoiceSetActivity.java
index 04c1a2f..5149048 100644
--- a/app/src/main/java/com/moral/yunfushao/activity/VoiceSetActivity.java
+++ b/app/src/main/java/com/moral/yunfushao/activity/VoiceSetActivity.java
@@ -1,21 +1,36 @@
 package com.moral.yunfushao.activity;
 
+import android.content.Intent;
 import android.content.res.AssetManager;
 import android.media.MediaPlayer;
+import android.net.Uri;
 import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.widget.RecyclerView;
 import android.view.View;
 
+import com.afollestad.materialdialogs.DialogAction;
+import com.afollestad.materialdialogs.MaterialDialog;
 import com.lzy.okgo.cache.CacheMode;
 import com.moral.andbrickslib.baseadapter.headandfooter.DividerItemDecoration;
+import com.moral.andbrickslib.baseadapter.recyclerview.MultiItemTypeAdapter;
+import com.moral.andbrickslib.utils.FastJsonTools;
+import com.moral.andbrickslib.utils.dialog.MaterialDialogUtils;
 import com.moral.yunfushao.MainApp;
 import com.moral.yunfushao.R;
 import com.moral.yunfushao.adapter.VoiceAdapter;
 import com.moral.yunfushao.base.BaseListActivity;
+import com.moral.yunfushao.ble.BLECommon;
 import com.moral.yunfushao.common.API;
+import com.moral.yunfushao.common.AppConfig;
 import com.moral.yunfushao.httputils.HttpCallBack;
 import com.moral.yunfushao.httputils.HttpUtils;
+import com.moral.yunfushao.model.EventMessage;
 import com.moral.yunfushao.model.VoiceBean;
 
+import org.greenrobot.eventbus.EventBus;
+
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
@@ -24,34 +39,77 @@
  * Created by haijiang on 2017/7/12.
  */
 
-public class VoiceSetActivity extends BaseListActivity implements VoiceAdapter.OnBtnClickListener{
+public class VoiceSetActivity extends BaseListActivity implements VoiceAdapter.OnBtnClickListener {
     private VoiceAdapter adapter;
     private ArrayList<VoiceBean> voiceList = new ArrayList<>();
     AssetManager am;
     MediaPlayer mp;
+
+    private MaterialDialog resetDialog;
+
+    private void showReset() {
+        resetDialog = MaterialDialogUtils.showCallBaclDialog(this, "������������", "���������������������������?", new MaterialDialog.SingleButtonCallback() {
+            @Override
+            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+                reset();
+                resetDialog.dismiss();
+            }
+        }, new MaterialDialog.SingleButtonCallback() {
+            @Override
+            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+                resetDialog.dismiss();
+            }
+        });
+        resetDialog.show();
+    }
+
     @Override
     protected void initOtherView() {
         tv_title.setText("������������");
-        VoiceBean voiceBean = new VoiceBean();
-        voiceBean.setId(0);
-        voiceBean.setRes(R.raw.music);
-        voiceBean.setName("music");
-        voiceList.add(voiceBean);
-        voiceBean = new VoiceBean();
-        voiceBean.setId(1);
-        voiceBean.setRes(R.raw.music1);
-        voiceBean.setName("music1");
-        voiceList.add(voiceBean);
+        tv_right.setText("������");
         DividerItemDecoration line = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
         mRecyclerView.addItemDecoration(line);
         adapter = new VoiceAdapter(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 onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (requestCode == 111) {
+            if (resultCode == RESULT_OK && data != null) {
+                int index = data.getIntExtra("position", 0);
+                String url = data.getStringExtra("url");
+                String name = data.getStringExtra("name");
+                voiceList.get(index).setDefaultMusic(false);
+                voiceList.get(index).setSetMusic(url);
+                voiceList.get(index).setVoiceName(name);
+                MainApp.theApp.sharedPreferencesUtil.saveVideoList(FastJsonTools.toJson(voiceList));
+                adapter.notifyDataSetChanged();
+                mToatUtils.showSingleLongToast("������������������");
+                EventMessage eventMessage = new EventMessage();
+                eventMessage.setType(AppConfig.SET_VOICE);
+                EventBus.getDefault().post(eventMessage);
+            }
+        }
     }
 
     @Override
     protected void onLoadMore() {
-
 
 
     }
@@ -64,7 +122,7 @@
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        if(mp!=null&&mp.isPlaying()){
+        if (mp != null && mp.isPlaying()) {
             mp.stop();
             mp.release();
         }
@@ -83,16 +141,23 @@
     @Override
     protected void initData() {
         setNormalView();
-        int index = MainApp.theApp.sharedPreferencesUtil.getLoginInfo().getVideo();
-        voiceList.get(index).setSet(true);
-        adapter.notifyDataSetChanged();
+        ArrayList<VoiceBean> temp = (ArrayList<VoiceBean>) FastJsonTools.getArrayJson(MainApp.theApp.sharedPreferencesUtil.getVideoList(), VoiceBean.class);
+        if (temp != null) {
+            voiceList.addAll(temp);
+            adapter.notifyDataSetChanged();
+        } else {
+            reset();
+        }
     }
 
     @Override
     protected void processClick(View view) {
-        switch (view.getId()){
+        switch (view.getId()) {
             case R.id.tv_left:
                 finish();
+                break;
+            case R.id.tv_right:
+                showReset();
                 break;
         }
 
@@ -106,20 +171,33 @@
     @Override
     public void playClick(int position) {
         VoiceBean voiceBean = voiceList.get(position);
-        if(voiceBean.isPlay()){
+        if (voiceBean.isPlay()) {
             mp.stop();
             voiceBean.setPlay(false);
-        }else{
-            if(mp!=null&&mp.isPlaying()){
+        } else {
+            if (mp != null && mp.isPlaying()) {
                 mp.stop();
                 mp.release();
-                for (VoiceBean voice :voiceList){
+                for (VoiceBean voice : voiceList) {
                     voice.setPlay(false);
                 }
                 adapter.notifyDataSetChanged();
             }
-            mp = MediaPlayer.create(this, voiceBean.getRes());
-            mp.start();
+            if (voiceBean.isDefaultMusic()) {
+                mp = MediaPlayer.create(this, Uri.parse(voiceBean.getRes()));
+                mp.start();
+            } else {
+                mp = new MediaPlayer();
+                try {
+                    mp.setDataSource(voiceBean.getSetMusic());
+                    mp.prepare();
+                    mp.start();
+                    voiceBean.setPlay(true);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
             voiceBean.setPlay(true);
         }
         adapter.notifyDataSetChanged();
@@ -127,17 +205,14 @@
 
     @Override
     public void setClick(int position) {
-        update("video",position+"");
-        for (VoiceBean voice :voiceList){
-            voice.setSet(false);
-        }
-        VoiceBean voiceBean = voiceList.get(position);
-        voiceBean.setSet(true);
-        adapter.notifyDataSetChanged();
+        Intent intent = new Intent(VoiceSetActivity.this, ChooseSongActivity.class);
+        intent.putExtra("position", position);
+        startActivityForResult(intent, 111);
     }
 
     /**
      * ������������������������������������['refresh_frequency', 'video', 'is_open_upload']
+     *
      * @param field
      * @param value
      */
@@ -164,7 +239,7 @@
             }
 
             @Override
-            public void onFail(int errno,String s) {
+            public void onFail(int errno, String s) {
                 mToatUtils.showSingletonToast(s);
                 if (progressDialog.isShowing()) {
                     progressDialog.dismiss();
@@ -172,4 +247,45 @@
             }
         });
     }
+
+    private void reset() {
+        voiceList.clear();
+        VoiceBean voiceBean = new VoiceBean();
+        voiceBean.setId(0);
+        voiceBean.setRes(AppConfig.voideUrl1);
+        voiceBean.setName("������������������");
+        voiceBean.setVoiceName("������");
+        voiceList.add(voiceBean);
+        voiceBean = new VoiceBean();
+        voiceBean.setId(1);
+        voiceBean.setRes(AppConfig.voideUrl2);
+        voiceBean.setName("������������������");
+        voiceBean.setVoiceName("������");
+        voiceList.add(voiceBean);
+        voiceBean = new VoiceBean();
+        voiceBean.setId(2);
+        voiceBean.setRes(AppConfig.voideUrl3);
+        voiceBean.setName("������������������");
+        voiceBean.setVoiceName("������");
+        voiceList.add(voiceBean);
+        MainApp.theApp.sharedPreferencesUtil.saveVideoList(FastJsonTools.toJson(voiceList));
+        adapter.notifyDataSetChanged();
+        EventMessage eventMessage = new EventMessage();
+        eventMessage.setType(AppConfig.SET_VOICE);
+        EventBus.getDefault().post(eventMessage);
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        if (mp != null && mp.isPlaying()) {
+            mp.stop();
+            mp.release();
+            for (VoiceBean voice : voiceList) {
+                voice.setPlay(false);
+            }
+            adapter.notifyDataSetChanged();
+            mp = null;
+        }
+    }
 }

--
Gitblit v1.8.0