android学习之-MediaRecorder
来源:互联网 发布:燕十八知乎 编辑:程序博客网 时间:2024/04/26 05:05
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(); } } }
- android学习之-MediaRecorder
- Android学习笔记之MediaRecorder
- android之mediarecorder (1)
- Android之MediaRecorder
- [Android N]MediaRecorder系列之初识mediaRecorder
- Android学习MediaRecorder和MediaPlayer
- android之mediarecorder(2)
- Android之MediaRecorder进行录影
- Android之MediaRecorder录制音频
- Android之录音--AudioRecord、MediaRecorder
- [Android N]MediaRecorder系列之mediaRecorder与StagefrightRecorder
- MediaRecorder 学习
- android之通过MediaRecorder进行手机录音
- android之通过MediaRecorder进行手机录音
- android教程之MediaPlayer和MediaRecorder
- Android开发之使用MediaRecorder录制声音
- Android开发之MediaRecorder类详解
- Android开发之使用MediaRecorder录制视频
- UML各种对象、关系表示方法
- android 简单图片动画播放实现
- 第三课 创建型模式 第三节 抽象工厂模式(Abstract Factory Pattern)
- 左中框同齐 不用JS实现 用CSS实现
- SQLServer 启动问题,服务器错误代码126 解决办法
- android学习之-MediaRecorder
- 程序员编程艺术:三之三续、求数组中给定下标区间内的第K小(大)元素
- asp.net web form与jQuery Validate一起工作时解决服务器端事件不能被触发的问题
- sql 语言中保留两位小数
- S3C2440 裸奔之始
- 程序员编程艺术第一~十章集锦与总结(教你如何编程)--持续更新中
- VC控件ListCtrl的使用方法
- ubuntu下svn工具Rabbitvcs安装
- freemarker 自定义变量