仿微信语音对话简单模型

根据需求要来实现一个类似微信的语音通话效果,想来下,现在来实现一个简单那的模型,可供遇到的人可作参考:

废话不多说就直接贴代码吧:

RecoderDemo.java:

package com.jankey.record;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;


/**
*
* @author jankey
*
*/
public class RecordDemo extends Activity {


private Button record;
private MyDialog dialog;
private AudioRecorder mr;
private LinearLayout linear;
private MediaPlayer mediaPlayer;
private File directory;
private Button btn = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_audio);
mr = new AudioRecorder("jankey");
record = (Button) this.findViewById(R.id.record);
linear = (LinearLayout) this.findViewById(R.id.showViews);
record.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
dialog = new MyDialog(RecordDemo.this, "正在录音");
try {
record.setText("正在录音...");
mr.start();
} catch (IOException e) {
e.printStackTrace();
}
dialog.show();
return false;
}
});
record.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
try {
mr.stop();
record.setText("录音停止!");
} catch (IOException e) {
e.printStackTrace();
}
dialog.dismiss();
showView();
break;
}
return false;
}
});
}


private void showView() {
for (int i = 0; i < apklist.size(); i++) {
// num++;
btn = new Button(this);
btn.setBackgroundColor(Color.GRAY);
btn.setWidth(200);
btn.setHeight(50);
btn.setText("点击倾听");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
playFile();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(directory.getAbsolutePath());
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
btn.setText("正在播放");
}
});
linear.addView(btn);
}
}


private void playFile() {
List<String> getFiles = GetFiles(
Environment.getExternalStorageDirectory() + "/", ".3gp", true);
for (String string : getFiles) {
System.out.println(string);
}
}


private List<String> apklist = new ArrayList<String>();
public List<String> GetFiles(String Path, String Extension,
boolean IsIterative)
{
File[] files = new File(Path).listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isFile()) {
if (f.getPath()
.substring(f.getPath().length() - Extension.length())
.equals(Extension))
apklist.add(f.getPath());
if (!IsIterative)
break;
} else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
GetFiles(f.getPath(), Extension, IsIterative);
}
return apklist;
}
}


AudioRecorder.java:

package com.jankey.record;


import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;


/**
*
* @author jankey
*
*/
public class AudioRecorder {


final MediaRecorder recorder = new MediaRecorder();
final String path;

public AudioRecorder(String path) {
this.path = sanitizePath(path);
}

private String sanitizePath(String path){
if(!path.startsWith("/")){
path = "/" + path;
}
if(!path.contains(".")){
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath()+path;
}

public void start() throws IOException{
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)){
throw new IOException("SD Card is not mounted,It is " + state + ".");
}
File directory = new File(path).getParentFile();
if(!directory.exists() && !directory.mkdirs()){
throw new IOException("Path to file could not be created");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}

public void stop() throws IOException{
recorder.stop();
recorder.release();
}
}


MyDialog.java:

package com.jankey.record;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;


/**
*
* @author jankey
*
*/
public class MyDialog extends Dialog{


private Context context;
private String ly;
private Button lyss;

public MyDialog(Context context,String str) {
super(context);
this.context = context;
this.ly = str;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.my_dialog);
lyss = (Button) this.findViewById(R.id.lys);
lyss.setText(ly);
lyss.setBackgroundColor(Color.BLUE);
}

}


anim: rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360">
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="8"
android:useLevel="false">
<gradient android:type="sweep"
android:useLevel="false"
android:startColor="#000000"
android:centerColor="#FFFFFF"
android:centerY="0.50"
android:endColor="#FFFF00" />
</shape>
</rotate>

background1.png:




record_audio.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按住开始录音"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showViews"
android:background="#556633"
/>
</LinearLayout>


AlphaAnimation aa=new AlphaAnimation(0.1f,1.0f);
aa.setDuration(3000);
iv01.startAnimation(aa);
aa.setAnimationListener(new AnimationListener()
{


@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Intent it=new Intent(LogoActivity.this,LoginActivity.class);
LogoActivity.this.startActivity(it);
finish();
}


@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}


@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

}
);


my_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="易通行"
android:textSize="30dip"
android:textColor="#665533"
/>
<Button
android:id="@+id/lys"
android:layout_width="200dip"
android:layout_height="50dip"
android:text="录音"
android:background="#884477"
/>
<ProgressBar
android:id="@+id/loadProgressBar"
android:background="@drawable/background1"
android:indeterminateDrawable="@anim/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值