Android实现多媒体录音笔

记事本涉及到的仅仅是对string 的存储,而且在读取上并不存在什么难点,直接用textview显示便可以了。需要做的主要是使用SQLite对数据进行一个整理。
而录音笔需要考虑的就相对较多了:比如录音时中断,录音时用户点击播放按钮;未录音,用户点击停止按钮;在录音或者播放时关闭activity;listview的item中需要为button设置不同的点击效果等等。

为了便于新手学习,在此还是罗列一下涉及的主要知识点:

  • 1、Baseadapter
  • 2、JAVA的file
  • 3、MediaRecorder
  • 4、较多的AlertDialog
  • 5、MediaPlayer

遇到的问题:
在listview item中的button控件可以获得焦点时,直接为listview设置item长按事件的监听。出现了listview的item长按事件无效的情况。

解决方法:
直接在Baseadapter中对该item的布局进行长按事件的监听(在笔者demo中是linearlayout),也就是说对item中button的父布局进行长按事件的监听。

效果:

MainActivity:

package com.example.recorder; 

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; 

import java.io.File;
import java.io.IOException; 

public class MainActivity extends Activity implements OnClickListener { 

  private Button start;
  private Button stop;
  private ListView listView;
  ShowRecorderAdpter showRecord; 

  // 录音文件播放
  // 录音
  // 音频文件保存地址
  private MediaPlayer myPlayer;
  private MediaRecorder myRecorder = null;
  private String path;
  private File saveFilePath;
  // 所录音的文件名
  String[] listFile = null; 

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    //初始化控件
    InitView(); 

  } 

  private void InitView() {
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    listView = (ListView) findViewById(R.id.list); 

    myPlayer = new MediaPlayer();
    showRecord = new ShowRecorderAdpter(); 

    //如果手机有sd卡
    if (Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED)) {
      try {
        path = Environment.getExternalStorageDirectory()
            .getCanonicalPath().toString()
            + "/MyRecorders";
        File files = new File(path);
        if (!files.exists()) {
          //如果有没有文件夹就创建文件夹
          files.mkdir();
        }
        listFile = files.list();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } 

    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    listView.setAdapter(showRecord); 

  } 

  //由于在item中涉及到了控件的点击效果,所以采用BaseAdapter
  class ShowRecorderAdpter extends BaseAdapter { 

    @Override
    public int getCount() {
      return listFile.length;
    } 

    @Override
    public Object getItem(int arg0) {
      return arg0;
    } 

    @Override
    public long getItemId(int arg0) {
      return arg0; 

    } 

    @Override
    public View getView(final int postion, View arg1, ViewGroup arg2) {
      View views = LayoutInflater.from(MainActivity.this).inflate(
          R.layout.list_item, null);
      LinearLayout parent = (LinearLayout) views.findViewById(R.id.list_parent);
      TextView filename = (TextView) views.findViewById(R.id.show_file_name);
      Button plays = (Button) views.findViewById(R.id.bt_list_play);
      Button stop = (Button) views.findViewById(R.id.bt_list_stop); 

      //在textview中显示的时候把“.amr”去掉
      filename.setText(listFile[postion].substring(0, listFile[postion].length() - 4));
      parent.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
          AlertDialog aler = new AlertDialog.Builder(MainActivity.this)
              .setTitle("确定删除该录音?")
              .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog
                    , int which) {
                  File file = new File(path + "/" + listFile[postion]);
                  file.delete();
                  // 在删除文件后刷新文件名列表
                  File files = new File(path);
                  listFile = files.list(); 

                  // 当文件被删除刷新ListView
                  showRecord.notifyDataSetChanged();
                }
              })
              .setNegativeButton("取消", null)
              .create();
          //设置不允许点击提示框之外的区域
          aler.setCanceledOnTouchOutside(false);
          aler.show();
          return false;
        }
      });
      // 播放录音
      plays.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
          //确认不是在录音的过程中播放
          if (myRecorder == null) {
            try {
              myPlayer.reset();
              myPlayer.setDataSource(path + "/" + listFile[postion]);
              if (!myPlayer.isPlaying()) {
                myPlayer.prepare();
                myPlayer.start();
              } else {
                myPlayer.pause();
              } 

            } catch (IOException e) {
              e.printStackTrace();
            }
          } else {
            Toast.makeText(MainActivity.this, "请不要再录音的过程中播放!", Toast.LENGTH_SHORT).show();
          }
        }
      });
      // 停止播放
      stop.setOnClickListener(new OnClickListener() { 

        @Override
        public void onClick(View arg0) {
          if (myRecorder == null && myPlayer.isPlaying()) {
            myPlayer.stop();
          }
        }
      });
      return views;
    } 

  } 

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.start:
        final EditText filename = new EditText(this);
        AlertDialog aler = new Builder(this)
            .setTitle("请输入要保存的文件名")
            .setView(filename)
            .setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog,
                            int which) {
                    String text = filename.getText().toString();
                    //如果文件名为空则跳出提示信息
                    if (text.equals("")) {
                      Toast.makeText(MainActivity.this,
                          "请不要输入空的文件名!", Toast.LENGTH_SHORT).show();
                    } else {
                      //开启录音
                      RecorderStart(text); 

                      start.setText("正在录音中。。");
                      start.setEnabled(false);
                      stop.setEnabled(true);
                      // 在增添文件后刷新文件名列表
                      File files = new File(path);
                      listFile = files.list();
                      // 当文件增加刷新ListView
                      showRecord.notifyDataSetChanged();
                    }
                  }
                })
            .setNegativeButton("取消",null)
            .create();
        //设置不允许点击提示框之外的区域
        aler.setCanceledOnTouchOutside(false);
        aler.show();
        break;
      case R.id.stop:
        myRecorder.stop();
        myRecorder.release();
        myRecorder = null;
        // 判断是否保存 如果不保存则删除
        aler = new AlertDialog.Builder(this)
            .setTitle("是否保存该录音")
            .setPositiveButton("确定", null)
            .setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog,
                            int which) {
                    saveFilePath.delete();
                    // 在删除文件后刷新文件名列表
                    File files = new File(path);
                    listFile = files.list(); 

                    // 当文件被删除刷新ListView
                    showRecord.notifyDataSetChanged();
                  }
                }).create();
        //设置不允许点击提示框之外的区域
        aler.setCanceledOnTouchOutside(false);
        aler.show(); 

        start.setText("录音");
        start.setEnabled(true);
        stop.setEnabled(false);
      default:
        break;
    } 

  } 

  private void RecorderStart(String text) {
    try {
      myRecorder = new MediaRecorder();
      // 从麦克风源进行录音
      myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
      // 设置输出格式
      myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
      // 设置编码格式
      myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 

      String paths = path + "/" + text + ".amr";
      saveFilePath = new File(paths);
      myRecorder.setOutputFile(saveFilePath.getAbsolutePath());
      myRecorder.prepare();
      // 开始录音
      myRecorder.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  } 

  @Override
  protected void onDestroy() {
    super.onDestroy();
    // 如果myPlayer正在播放,那么就停止播放,并且释放资源
    if (myPlayer.isPlaying()) {
      myPlayer.stop();
      myPlayer.release();
    }
    //如果myRecorder有内容(代表正在录音),那么就直接释放资源
    if(myRecorder!=null) {
      myRecorder.release();
      myPlayer.release();
    }
  } 

}

activity_main:

<LinearLayout 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:orientation="vertical"
  tools:context=".MainActivity"> 

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000"
    android:padding="13dp"
    android:text="语音笔"
    android:textColor="#fff"
    android:textSize="22sp"
    android:textStyle="bold" /> 

  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="10dp"
    ></ListView> 

  <LinearLayout
    android:id="@+id/li1"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"> 

    <Button
      android:id="@+id/start"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:textSize="20sp"
      android:text="开始录音" /> 

    <Button
      android:id="@+id/stop"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:enabled="false"
      android:textSize="20sp"
      android:text="停止录音" />
  </LinearLayout> 

</LinearLayout>

list_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp"
  android:id="@+id/list_parent"
  android:orientation="horizontal"> 

  <TextView
    android:id="@+id/show_file_name"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="文件名"
    android:textColor="#000"
    android:textSize="20sp"
    /> 

  <Button
    android:id="@+id/bt_list_play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:text="播放" /> 

  <Button
    android:id="@+id/bt_list_stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:text="停止" /> 

</LinearLayout>

以上就是本文的全部内容,希望对大家实现Android软件编程有所帮助。

(0)

相关推荐

  • Android录音播放管理工具

    1.语音播放直接用系统工具就好了,这个就不多说了,根据传入的路径(网络路径或本地路径均可)播放音频文件 /** * Created by zhb on 2017/1/16. * 音乐在线播放 */ public class PlayManager { private Context mcontext; public PlayManager(Context context){ this.mcontext = context; } public void play(String song){ Med

  • android 通过MediaRecorder实现简单的录音示例

    整理文档,搜刮出一个android 通过MediaRecorder实现简单的录音示例,稍微整理精简一下做下分享. MainActivity package com.centaur.collectvoice; import android.media.MediaRecorder; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; impo

  • Android录音应用实例教程

    本文以实例形式较为详细的展示了Android录音的实现方法,分享给大家供大家参考之用.具体方法如下: 首先是xml布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" andr

  • Android简单的利用MediaRecorder进行录音的实例代码

    复制代码 代码如下: package com.ppmeet; import java.io.IOException; import android.app.Activity;  import android.graphics.PixelFormat;  import android.media.MediaRecorder;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClick

  • Android编程开发录音和播放录音简单示例

    本文实例讲述了Android编程开发录音和播放录音的方法.分享给大家供大家参考,具体如下: /* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be

  • android编程实现电话录音的方法

    本文实例讲述了android编程实现电话录音的方法.分享给大家供大家参考.具体如下: 在清单文件AndroidManifest.xml中添加权限: <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUN

  • Android中简单调用图片、视频、音频、录音和拍照的方法

    本文实例讲述了Android中简单调用图片.视频.音频.录音和拍照的方法.分享给大家供大家参考,具体如下: //选择图片 requestCode 返回的标识 Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT" innerIntent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED =

  • Android 实现电话来去自动录音的功能

    我们在使用Android手机打电话时,有时可能会需要对来去电通话自动录音,本文就详细讲解实现Android来去电通话自动录音的方法,大家按照文中的方法编写程序就可以完成此功能. 来去电自动录音的关键在于如何监听手机电话状态的转变: 1)来电的状态的转换如下(红色标记是我们要用到的状态) 空闲(IDEL)--> 响铃(RINGING)--> 接听(ACTIVE)--> 挂断(经历DISCONNECTING--DISCONNECTED)--> 空闲(IDEL) 或者  空闲(IDEL)

  • 利用libmp3lame实现在Android上录音MP3文件示例

    之前项目需要实现MP3的录音,于是使用上了Lame这个库.这次做一个demo,使用AndroidStudio+Cmake+NDK进行开发.利用Android SDK提供的AndroidRecorder进行录音,得到PCM数据,并使用jni调用Lame这个C库将PCM数据转换为MP3文件.并使用MediaPlayer对录音的MP3文件进行播放.另外此次的按键是仿微信的语音按键,按下录音,松开结束,若中途上滑松开即取消 效果如下: 项目地址: LameMp3ForAndroid_jb51.rar 一

  • Android音频录制MediaRecorder之简易的录音软件实现代码

    使用MediaRecorder的步骤:1.创建MediaRecorder对象2.调用MediRecorder对象的setAudioSource()方法设置声音的来源,一般传入MediaRecorder.MIC3.调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件的格式4.调用MediaRecorder对象的setAudioRncoder().setAudioEncodingBitRate(int bitRate).setAudioSamlingRate(i

随机推荐