gstb.fd.eofficial.oa.im.widget.chatrow.EaseChatRowVoicePlayClickListener.java Source code

Java tutorial

Introduction

Here is the source code for gstb.fd.eofficial.oa.im.widget.chatrow.EaseChatRowVoicePlayClickListener.java

Source

/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gstb.fd.eofficial.oa.im.widget.chatrow;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import org.apache.http.client.ClientProtocolException;

import gstb.fd.eofficial.oa.R;
import gstb.fd.eofficial.oa.eofficial.db.MessageDao;
import gstb.fd.eofficial.oa.im.imbean.EMMessage;
import gstb.fd.eofficial.oa.im.imcontroller.EaseUI;
import gstb.fd.eofficial.oa.im.imutils.IMCache;
import gstb.fd.eofficial.oa.im.imutils.IMConstants;
import gstb.fd.eofficial.oa.im.imutils.IMFileUtils;

/**
 * row?
 *
 */
public class EaseChatRowVoicePlayClickListener implements View.OnClickListener {
    private static final String TAG = "VoicePlayClickListener";
    EMMessage message;
    ImageView voiceIconView;

    private AnimationDrawable voiceAnimation = null;
    MediaPlayer mediaPlayer = null;
    ImageView iv_read_status;
    Activity activity;
    private EMMessage.ChatType chatType;
    private BaseAdapter adapter;

    public static boolean isPlaying = false;
    public static EaseChatRowVoicePlayClickListener currentPlayListener = null;
    //   public static String playMsgId;

    public EaseChatRowVoicePlayClickListener(EMMessage message, ImageView v, ImageView iv_read_status,
            BaseAdapter adapter, Activity context) {
        this.message = message;
        this.iv_read_status = iv_read_status;
        this.adapter = adapter;
        voiceIconView = v;
        this.activity = context;
        this.chatType = message.chatType();
    }

    public void stopPlayVoice() {
        voiceAnimation.stop();
        if (message.direct() == EMMessage.Direct.RECEIVE) {
            voiceIconView.setImageResource(R.drawable.ease_chatfrom_voice_playing);
        } else {
            voiceIconView.setImageResource(R.drawable.ease_chatto_voice_playing);
        }
        // stop play voice
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        isPlaying = false;
        //      playMsgId = null;
        adapter.notifyDataSetChanged();
    }

    public void playVoice(String filePath) {
        Log.e("sssssss", "sssssssssssssss");
        if (!(new File(filePath).exists())) {
            return;
        }
        //      playMsgId = message.getMsgId();
        AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);

        mediaPlayer = new MediaPlayer();
        //      if (EaseUI.getInstance().getSettingsProvider().isSpeakerOpened()) {
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setSpeakerphoneOn(true);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
        //      } else {
        //         audioManager.setSpeakerphoneOn(false);// 
        // ?Earpiece???
        //         audioManager.setMode(AudioManager.MODE_IN_CALL);
        //         mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        //      }
        try {
            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepare();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mediaPlayer.release();
                    mediaPlayer = null;
                    stopPlayVoice(); // stop animation
                }

            });
            isPlaying = true;
            currentPlayListener = this;
            mediaPlayer.start();
            showAnimation();

            // ?
            if (message.direct() == EMMessage.Direct.RECEIVE) {

                //             if (!message.isAcked() && chatType == EMMessage.ChatType.Chat) {
                //                       // ??
                ////                     EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                //             }
                if (!message.isListened() && iv_read_status != null
                        && iv_read_status.getVisibility() == View.VISIBLE) {
                    // ????
                    iv_read_status.setVisibility(View.INVISIBLE);
                    message.setListened(true);
                    MessageDao.instance(activity).updateListen(message.getMsgId());
                    //               EMClient.getInstance().chatManager().setMessageListened(message);
                }

            }

        } catch (Exception e) {
            System.out.println();
        }
    }

    // show the voice playing animation
    private void showAnimation() {
        // play voice, and start animation
        if (message.direct() == EMMessage.Direct.RECEIVE) {
            voiceIconView.setImageResource(R.drawable.voice_from_icon);
        } else {
            voiceIconView.setImageResource(R.drawable.voice_to_icon);
        }
        voiceAnimation = (AnimationDrawable) voiceIconView.getDrawable();
        voiceAnimation.start();
    }

    @Override
    public void onClick(View v) {
        String st = activity.getResources().getString(R.string.Is_download_voice_click_later);
        if (isPlaying) {
            //         if (playMsgId != null && playMsgId.equals(message.getMsgId())) {
            //            currentPlayListener.stopPlayVoice();
            //            return;
            //         }
            currentPlayListener.stopPlayVoice();
        }

        if (message.direct() == EMMessage.Direct.SEND) {
            // for sent msg, we will try to play the voice file directly
            playVoice(message.getExt());
        } else {
            if (message.status() == EMMessage.Status.SUCCESS) {
                File file = new File(message.getExt());
                if (file.exists()) {
                    playVoice(message.getExt());
                } else {
                    Log.e(TAG, "file not exist");
                    Toast.makeText(activity, st, Toast.LENGTH_SHORT).show();
                    new AsyncTask<Void, Void, Void>() {

                        @Override
                        protected Void doInBackground(Void... params) {
                            //                  EMClient.getInstance().chatManager().downloadAttachment(message);
                            try {
                                Log.e(TAG, "file not exist1");
                                IMFileUtils.down_file(IMConstants.path + message.getFileId(), message.getExt());
                            } catch (ClientProtocolException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                            super.onPostExecute(result);
                            adapter.notifyDataSetChanged();
                        }

                    }.execute();

                }

            } else if (message.status() == EMMessage.Status.INPROGRESS) {
                Toast.makeText(activity, st, Toast.LENGTH_SHORT).show();
            } else if (message.status() == EMMessage.Status.FAIL) {
                Toast.makeText(activity, st, Toast.LENGTH_SHORT).show();

            }

        }
    }

}