传感器应用的demo自动录音器
生活随笔
收集整理的這篇文章主要介紹了
传感器应用的demo自动录音器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面我通過一個實例來學會如何使用某一個類型的傳感器。我們這里使用加速度傳感器來實現這樣一個功能:開啟我們的錄音程序放在你的口袋或者提包里,需要錄音的時候把衣服整理一下,或者把提包挪動個位置,那么此時手機就會感受到變化從而開始錄音。由此達到神不知鬼不覺的錄音效果。說起來似乎有點神,其實做起來很簡單。
activity類
public class MainActivity extends ActionBarActivity {private TextView tx1;//錄音和停止按鈕 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; //是否正在錄音 private boolean isRecoding = false; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// UI組件 recordButton = (Button) findViewById(R.id.b01);stopButton = (Button) findViewById(R.id.b02); tx1 = (TextView)findViewById(R.id.text1); // 錄音按鈕點擊事件 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(); //如果正在錄音,那么可以停止錄音 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+ "\n平均晃動幅度="+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();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; } }布局文件: <RelativeLayout 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"tools:context="com.example.sensordemo.MainActivity" ><Buttonandroid:id="@+id/b01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="134dp"android:text="錄音" /><Buttonandroid:id="@+id/b02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignRight="@+id/b01"android:layout_below="@+id/b01"android:layout_marginTop="32dp"android:text="停止" /><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/b02"android:layout_marginLeft="16dp"android:layout_marginTop="20dp"android:text="傳感器" /></RelativeLayout>
效果圖:
-------------------------------------------------轉載請注明出處。。
總結
以上是生活随笔為你收集整理的传感器应用的demo自动录音器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android传感器开发详解
- 下一篇: 四大组建进程间通信--基础