Skip to content

Instantly share code, notes, and snippets.

@zhaochunqi
Last active August 29, 2015 14:02
Show Gist options
  • Save zhaochunqi/d10fb992d02cdb8c6265 to your computer and use it in GitHub Desktop.
Save zhaochunqi/d10fb992d02cdb8c6265 to your computer and use it in GitHub Desktop.
Android 录音与播放
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="cn.zhaochunqi.testrecord007.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="录音"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="to_record"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/button2"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:onClick="play_recorded"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.zhaochunqi.testrecord007.app" >
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.zhaochunqi.testrecord007.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package cn.zhaochunqi.testrecord007.app;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private Button to_record_button;
private int flag_button = 0 ;
MediaRecorder mediaRecorder = new MediaRecorder();
File audioFile = null;
MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to_record_button = (Button)findViewById(R.id.button);
to_record_button.setOnClickListener(to_record_short_click);
to_record_button.setOnLongClickListener(to_record_long_click);
}
@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
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// public void to_record(View view) {
// if(flag_button == 0) {
//
// startRecord();
// to_record_button.setText("停止");
// flag_button = 1;
// }else {
// stopRecord();
// to_record_button.setText("录音");
// flag_button = 0;
// }
//
// }
/*
* 监听器,长按开始录音。
* @return 返回值为flase时在结束时回启动onclickListener,为true时不会调用。
*/
private View.OnLongClickListener to_record_long_click = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
to_record_button.setText("录音中");
startRecord();
flag_button = 1;
return false;
}
};
/*
* 监听器,在OnLongClickListener结束后如果返回时为flase时启动
* 如果点击时间不超过2s也回启动。
*/
private View.OnClickListener to_record_short_click = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag_button != 1) {
Toast.makeText(MainActivity.this, "太短啦", Toast.LENGTH_SHORT).show();
} else {
stopRecord();
to_record_button.setText("录音");
Log.e("hello ", "stopped");
Toast.makeText(MainActivity.this, "录音结束了呢", Toast.LENGTH_SHORT).show();
}
}
};
private void startRecord(){
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
audioFile = File.createTempFile("record_", ".amr");
mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
Log.e("Adress", audioFile.getAbsolutePath().toString());
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecord() {
mediaRecorder.stop();
}
public void play_recorded(View view) {
try {
mediaPlayer.setDataSource(audioFile.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment