1.只实现了语音录制,然后点击ImageView播放动画的同时播放语音
package com.example.example03;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener {
private MediaPlayer mediaPlayer;
private AnimationDrawable animationDrawable;
private File soundFile;
private MediaRecorder mRecorder;
private ImageView imageView;
private Button startButton;
private AlertDialog dialog;
private AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
builder=new AlertDialog.Builder(MainActivity.this);
imageView = (ImageView) findViewById(R.id.animation);
startButton = (Button) findViewById(R.id.startButton);
imageView.setOnTouchListener(this);
startButton.setOnTouchListener(this);
builder.setMessage("正在录音。。。。。");
builder.setTitle("小提示:");
dialog=builder.create();
dialog.setCancelable(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
/*if (soundFile != null && soundFile.exists()) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}*/
super.onDestroy();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.animation:
imageView.setImageResource(R.drawable.animation1);
animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
mediaPlayer = new MediaPlayer();
try {
/*mediaPlayer
.setDataSource("/storage/sdcard0/zzenglish/ui/scene/multimedia/audiopackage/1021/abc.mp3");
*/mediaPlayer.setDataSource("/storage/sdcard0/sound.amr");
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
animationDrawable.stop();
}
});
break;
case R.id.startButton:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(MainActivity.this, "sd卡不存在,请插入sd卡!",
Toast.LENGTH_SHORT).show();
return false;
}
try {
dialog.show();
soundFile = new File(Environment
.getExternalStorageDirectory().getCanonicalFile()
+"/sound.amr");
Log.i("Main", soundFile.toString());
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(soundFile.getAbsolutePath());
mRecorder.prepare();
mRecorder.start();
} catch (Exception e) {
// TODO: handle exception
}
}else if(event.getAction()==MotionEvent.ACTION_UP){
if (soundFile != null && soundFile.exists()) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
dialog.dismiss();
/* ImageView imageView1 = new ImageView(MainActivity.this);
imageView1.setImageResource(R.drawable.animation1);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout.addView(imageView1);*/
}
break;
}
return true;
}
}
2.带跳转activity的语音录制与播放。当要录制语音时,跳转到另一个页面进行,可以取消录制的语音或者重新录制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- <ImageView
android:id="@+id/animation"
android:layout_width="100sp"
android:layout_height="60sp"
android:layout_marginLeft="30sp"
android:paddingRight="50px"
android:src="@drawable/animation1" />-->
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_weight="12"
android:gravity="right"
android:layout_gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50sp"
android:layout_marginRight="10sp"
android:textStyle="normal"
android:textSize="20sp"
android:text="0''" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_marginRight="6sp"
android:maxHeight="50sp"
android:maxWidth="80sp"
android:layout_gravity="right"
android:scaleType="fitEnd"
android:src="@drawable/icon1" />
</LinearLayout>
<Button
android:id="@+id/startbutton"
android:layout_width="match_parent"
android:layout_height="60sp"
android:layout_marginBottom="20sp"
android:layout_weight="1"
android:text="跳转到录音页面" />
</LinearLayout>
package com.example.example03;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnTouchListener {
private MediaPlayer mediaPlayer;
private AnimationDrawable animationDrawable;
private File soundFile;
private Button startButton;
private TextView textView;
private ImageButton imageButton;
private String time="0''";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.startbutton);
textView = (TextView) findViewById(R.id.textView1);
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnTouchListener(this);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
RecordActivity.class);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0 && resultCode == 0) {
Bundle data = intent.getExtras();
time=data.getString("time");
if(time.equals("0''")){
soundFile=null;
}else{
soundFile = new File(data.getString("soundPath"));
}
textView.setText(time);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
/*
* if (soundFile != null && soundFile.exists()) { mRecorder.stop();
* mRecorder.release(); mRecorder = null; }
*/
super.onDestroy();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageButton1:
imageButton.setImageResource(R.drawable.animation1);
// imageView.setImageResource(R.drawable.animation1);
animationDrawable = (AnimationDrawable) imageButton.getDrawable();
if(!time.equals("0''")){
animationDrawable.start();
}
mediaPlayer = new MediaPlayer();
try {
if(soundFile==null){
return false;
}
mediaPlayer.setDataSource(soundFile.toString());
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
imageButton.setImageResource(R.drawable.icon1);
animationDrawable.stop();
}
});
break;
}
return true;
}
}
<?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" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="20sp"
android:layout_weight="12"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50sp"
android:layout_marginRight="10sp"
android:text="0''"
android:textSize="20sp"
android:textStyle="normal" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_gravity="right"
android:layout_marginRight="6sp"
android:maxHeight="50sp"
android:maxWidth="80sp"
android:scaleType="fitEnd"
android:src="@drawable/icon1" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="@+id/cancelbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="cancel" />
<Button
android:id="@+id/sendbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
</LinearLayout>
<Button
android:id="@+id/startrecordbutton"
android:layout_width="match_parent"
android:layout_height="80sp"
android:layout_marginBottom="10sp"
android:text="开始录音" />
</LinearLayout>
package com.example.example03;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class RecordActivity extends Activity implements OnTouchListener {
private MediaPlayer mediaPlayer;
private AnimationDrawable animationDrawable;
private File soundFile;
private MediaRecorder mRecorder;
private TextView textView;
private ImageButton imageButton;
private Button cancelButton;
private Button sendButton;
private Button startRecordButton;
private AlertDialog dialog;
private AlertDialog.Builder builder;
private long startTime;
private long endTime;
private String time="0''";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
textView = (TextView) findViewById(R.id.textView1);
imageButton = (ImageButton) findViewById(R.id.imageButton1);
cancelButton = (Button) findViewById(R.id.cancelbutton);
sendButton = (Button) findViewById(R.id.sendbutton);
startRecordButton = (Button) findViewById(R.id.startrecordbutton);
startRecordButton.setOnTouchListener(this);
imageButton.setOnTouchListener(this);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=getIntent();
intent.putExtra("time", "0''");
RecordActivity.this.setResult(0, intent);
RecordActivity.this.finish();
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=getIntent();
if(time.equals("0''")){
intent.putExtra("time", time);
}else{
intent.putExtra("soundPath", soundFile.toString());
intent.putExtra("time", time);
}
RecordActivity.this.setResult(0, intent);
RecordActivity.this.finish();
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageButton1:
imageButton.setImageResource(R.drawable.animation1);
// imageView.setImageResource(R.drawable.animation1);
animationDrawable = (AnimationDrawable) imageButton.getDrawable();
animationDrawable.start();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/storage/sdcard0/sound1.amr");
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
imageButton.setImageResource(R.drawable.icon1);
animationDrawable.stop();
}
});
break;
case R.id.startrecordbutton:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(RecordActivity.this, "sd卡不存在,请插入sd卡!",
Toast.LENGTH_SHORT).show();
return false;
}
try {
builder = new AlertDialog.Builder(RecordActivity.this);
builder.setMessage("正在录音。。。。。");
builder.setTitle("小提示:");
dialog = builder.create();
dialog.setCancelable(true);
dialog.show();
soundFile = new File(Environment
.getExternalStorageDirectory().getCanonicalFile()
+ "/sound1.amr");
Log.i("Main", soundFile.toString());
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(soundFile.getAbsolutePath());
mRecorder.prepare();
startTime = System.currentTimeMillis();
mRecorder.start();
} catch (Exception e) {
// TODO: handle exception
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (soundFile != null && soundFile.exists()) {
mRecorder.stop();
endTime = System.currentTimeMillis();
time = String
.valueOf((int) (endTime - startTime) / 1000)+ "''";
textView.setText(time);
mRecorder.release();
mRecorder = null;
}
dialog.dismiss();
}
break;
}
return true;
}
}