1.项目清单权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.activity
package com.enterise.always.activity;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Button but_start,but_stop,but_file;
private boolean issdcard = false;
File mp3file;
File filename;
MediaRecorder recorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but_start = (Button)this.findViewById(R.id.main_start);
but_stop = (Button)this.findViewById(R.id.main_stop);
but_file = (Button) this.findViewById(R.id.main_file);
but_start.setOnClickListener(this);
but_stop.setOnClickListener(this);
but_file.setOnClickListener(this);
issdcard = (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
if(issdcard) {
File file = Environment.getExternalStorageDirectory();
filename = new File(file, "demo");
if(!filename.exists()){
filename.mkdirs();
}
String medianame = System.currentTimeMillis()+".arm";
mp3file = new File(filename, medianame);
}else {
Toast.makeText(this, "sd卡不能用!", Toast.LENGTH_LONG).show();
}
}
private void initMediaRecorder() {
recorder = new MediaRecorder();
// 设置录音来源为麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//文件保存位置
recorder.setOutputFile(mp3file.getAbsolutePath());
}
@Override
public void onClick(View v) {
switch(v.getId()) {
//录音
case R.id.main_start:
try {
initMediaRecorder();
recorder.prepare();
recorder.start();
but_start.setText("录音中");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.main_stop:
if(mp3file != null) {
recorder.stop();
recorder.reset();
recorder = null;
}
break;
case R.id.main_file:
String path = filename.getAbsolutePath();
Intent intent = new Intent(MainActivity.this,ListViewActivity.class);
intent.putExtra("filename", path);
startActivity(intent);
break;
}
}
}
main.xim
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/main_start"
android:layout_weight="1" android:text="录音" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/main_stop"
android:layout_weight="1" android:text="停止" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/main_file"
android:layout_weight="1" android:text="录音文件" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
ListViewActivity
package com.enterise.always.activity;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewActivity extends Activity implements OnItemClickListener{
private ListView listView;
File file;
String filename;
String[] listname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listview);
Intent intent = getIntent();
filename = (String)intent.getExtras().get("filename");
listView = (ListView)this.findViewById(R.id.listview);
//初始化
init();
}
private void init() {
file = new File(filename);
listname = file.list();
List<HashMap<String,Object>> data = initData();
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listview_wrap,
new String[]{"name"},
new int[]{R.id.wrap_textview});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
//数据加载
private List<HashMap<String,Object>> initData() {
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
for(String name : listname) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
list.add(map);
}
return list;
}
//点击事件处理
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
@SuppressWarnings("unchecked")
HashMap<String,Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);
Set<Entry<String,Object>> entrySet = map.entrySet();
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
String value = null;
while(iterator.hasNext()) {
Entry<String, Object> next = iterator.next();
value = (String) next.getValue();
}
String path = filename+File.separator+value;
Intent intent = new Intent(this,MediaPlayerActivity.class);
intent.putExtra("path", path);
startActivity(intent);
}
}
xml文件:
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/listview"
android:layout_marginLeft="20dp" android:layout_marginTop="20dp" />
</LinearLayout>
listwrap.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_gravity="center"
android:gravity="center_horizontal" android:id="@+id/wrap_textview"
android:text="ddddddddddddddddddddd" />
</LinearLayout>
package com.enterise.always.activity;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MediaPlayerActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mediaplayer);
Intent intent = getIntent();
String path = intent.getExtras().getString("path");
MediaPlayer player = new MediaPlayer();
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}