访微信语音聊天
package com.alva.vcd; import java.util.ArrayList; import java.util.List; import com.alva.vcd.adapter.RecorderAdapter; import com.alva.vcd.pojo.Recorder; import com.alva.vcd.view.AudioRecorderButton; import com.alva.vcd.view.AudioRecorderButton.AudioFinishRecorderListener; import com.alva.vcd.view.MediaManager; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.graphics.drawable.AnimationDrawable; import android.media.MediaPlayer; import android.os.Bundle; import android.util .Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ListView; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity implements AudioFinishRecorderListener, OnItemClickListener { private ListView mListview; private AudioRecorderButton mAudioButton; private ArrayAdapter mAdapter; private List mDatas = new ArrayList();; private View mAnimView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); initViews(); initDatas(); initEvents(); } private void initViews() { mListview = (ListView) findViewById(R.id.listview_chat); mAudioButton = (AudioRecorderButton) findViewById(R.id.recorderButton); } private void initDatas() { mAudioButton.setAudioFinishRecorderListener(this); mAdapter = new RecorderAdapter(getApplicationContext(), mDatas); mListview.setAdapter(mAdapter); } private void initEvents() { mListview.setOnItemClickListener(this); } @Override public void onFinsh(float seconds, String filePath) { Recorder mRecorder = new Recorder(); mRecorder.setFilePath(filePath); mRecorder.setTime(seconds); mDatas.add(mRecorder); mAdapter.notifyDataSetChanged(); mListview.setSelection(mDatas.size() - 1); } @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { Log.e("MediaManager", mDatas.get(arg2).getFilePath() + "sssss"); if (mAnimView != null) { mAnimView.setBackgroundResource(R.drawable.adj); mAnimView = null; } // 播放动画 mAnimView = arg1.findViewById(R.id.id_view); mAnimView.setBackgroundResource(R.drawable.play_anim); AnimationDrawable anim = (AnimationDrawable) mAnimView.getBackground(); anim.start(); // 播放音频 MediaManager.playSound(mDatas.get(arg2).getFilePath(), new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { mAnimView.setBackgroundResource(R.drawable.adj); } }); } @Override protected void onPause() { super.onPause(); MediaManager.pause(); } @Override protected void onResume() { super.onResume(); MediaManager.resume(); } @Override protected void onDestroy() { super.onDestroy(); MediaManager.release(); } }
用户评论