Android?????????????
???????????? ???????[ 2014/8/11 17:16:36 ] ????????Android ?????? ???
????????????????????
???????????????????????????λ?????????????????????????????????????????????????????????????????????ü??????????????????????????????????????????????????????????????????????·???????£??????????????λ???????????????????仯???????????????????????????Ч?????????????е??????????????????????????ɡ?
????????????????????28????????????????????28????????????д?????????
????1??????????? Lesson37_YYRecorder ????????? MainActivity.java ??
????2?????????????28??????? MainActivity.java ??????????????
????Java????
package basic.android.lesson37;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//?????????
private Button recordButton;
private Button stopButton;
//????????????
private long initTime = 0;
private long lastTime = 0;
private long curTime = 0;
private long duration = 0;
private float last_x = 0.0f;
private float last_y = 0.0f;
private float last_z = 0.0f;
private float shake = 0.0f;
private float totalShake = 0.0f;
//y???????????
private MediaRecorder mr;
//??????????
private boolean isRecoding = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// UI???
recordButton = (Button) this.findViewById(R.id.Button01);
stopButton = (Button) this.findViewById(R.id.Button02);
final TextView tx1 = (TextView) this.findViewById(R.id.TextView01);
// ???????????
recordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//??????????????????????????????
if(!isRecoding){
startRecord();
}
}
});
// ???????????
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initShake();
//???????????????????????
if (mr != null) {
mr.stop();
mr.release();
mr = null;
recordButton.setText("???");
Toast.makeText(getApplicationContext()?? "??????"?? Toast.LENGTH_LONG).show();
isRecoding = false;
}
}
});
// ???????????????
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// ?????????????
Sensor acceleromererSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// ???崫?????????????
SensorEventListener acceleromererListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor?? int accuracy) {
//???????
}
//??????????????
@Override
public void onSensorChanged(SensorEvent event) {
//?????п???????????????????????????????????????????????
if(!isRecoding){
//???????????????????????
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
//???????????????
curTime = System.currentTimeMillis();
//100?????????
if ((curTime - lastTime) > 100) {
duration = (curTime - lastTime);
// ???????????ζ?
if (last_x == 0.0f && last_y == 0.0f && last_z == 0.0f) {
//last_x??last_y??last_z???0??????????????
initTime = System.currentTimeMillis();
} else {
// ???λζ?????
shake = (Math.abs(x - last_x) + Math.abs(y - last_y) + Math.abs(z - last_z)) / duration * 100;
}
//????ε?ζ????????????????ζ?????
totalShake += shake;
// ?ж???????????????????д????????????????????????????????????^_^
if (totalShake > 10 && totalShake / (curTime - initTime) * 1000 > 10) {
startRecord();
initShake();
}
tx1.setText("????ζ?????="+totalShake+ "
????ζ?????="+totalShake / (curTime - initTime) * 1000 );
}
last_x = x;
last_y = y;
last_z = z;
lastTime = curTime;
}
}
};
//???????????????????????
sm.registerListener(acceleromererListener?? acceleromererSensor?? SensorManager.SENSOR_DELAY_NORMAL);
}
// ??????
public void startRecord() {
//?????????????????
isRecoding = true;
//??????
File file = new File("/sdcard/" + "YY"
+ new DateFormat().format("yyyyMMdd_hhmmss"?? Calendar.getInstance(Locale.CHINA)) + ".amr");
Toast.makeText(getApplicationContext()?? "????????????????" + file.getAbsolutePath()?? Toast.LENGTH_LONG).show();
// ??????????
mr = new MediaRecorder();
// ??????????????
mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// ??????????
mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// ?????????
mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// ??????????
mr.setOutputFile(file.getAbsolutePath());
try {
// ???????
file.createNewFile();
// ??????
mr.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// ??????
mr.start();
recordButton.setText("?????……");
}
//????????
public void initShake() {
lastTime = 0;
duration = 0;
curTime = 0;
initTime = 0;
last_x = 0.0f;
last_y = 0.0f;
last_z = 0.0f;
shake = 0.0f;
totalShake = 0.0f;
}
}
????4??????С????£???Android2.2?汾??????????и???????????????????????????????????????????????????????????Щ???? ?????? event.values ??????????????????????Щ???????????з????????????????????????????3????????л????????ж?????????ж????????????????……???? ????????????????????????л????????????????′?????ɡ?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11