欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-07 16:46:57
本文实例讲述了android编程开发录音和播放录音的方法。分享给大家供大家参考,具体如下: /* * the application needs to hav...

本文实例讲述了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 set in the
 * application's androidmanifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.write_external_storage" />
 * <uses-permission android:name="android.permission.record_audio" />
 *
 */
package com.android.audiorecordtest;
import android.app.activity;
import android.widget.linearlayout;
import android.os.bundle;
import android.os.environment;
import android.view.viewgroup;
import android.widget.button;
import android.view.view;
import android.view.view.onclicklistener;
import android.content.context;
import android.util.log;
import android.media.mediarecorder;
import android.media.mediaplayer;
import java.io.ioexception;
public class audiorecordtest extends activity
{
  private static final string log_tag = "audiorecordtest";
  private static string mfilename = null;
  private recordbutton mrecordbutton = null;
  private mediarecorder mrecorder = null;
  private playbutton  mplaybutton = null;
  private mediaplayer  mplayer = null;
  private void onrecord(boolean start) {
    if (start) {
      startrecording();
    } else {
      stoprecording();
    }
  }
  private void onplay(boolean start) {
    if (start) {
      startplaying();
    } else {
      stopplaying();
    }
  }
  private void startplaying() {
    mplayer = new mediaplayer();
    try {
      mplayer.setdatasource(mfilename);
      mplayer.prepare();
      mplayer.start();
    } catch (ioexception e) {
      log.e(log_tag, "prepare() failed");
    }
  }
  private void stopplaying() {
    mplayer.release();
    mplayer = null;
  }
  private void startrecording() {
    mrecorder = new mediarecorder();
    mrecorder.setaudiosource(mediarecorder.audiosource.mic);
    mrecorder.setoutputformat(mediarecorder.outputformat.three_gpp);
    mrecorder.setoutputfile(mfilename);
    mrecorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);
    try {
      mrecorder.prepare();
    } catch (ioexception e) {
      log.e(log_tag, "prepare() failed");
    }
    mrecorder.start();
  }
  private void stoprecording() {
    mrecorder.stop();
    mrecorder.release();
    mrecorder = null;
  }
  class recordbutton extends button {
    boolean mstartrecording = true;
    onclicklistener clicker = new onclicklistener() {
      public void onclick(view v) {
        onrecord(mstartrecording);
        if (mstartrecording) {
          settext("stop recording");
        } else {
          settext("start recording");
        }
        mstartrecording = !mstartrecording;
      }
    };
    public recordbutton(context ctx) {
      super(ctx);
      settext("start recording");
      setonclicklistener(clicker);
    }
  }
  class playbutton extends button {
    boolean mstartplaying = true;
    onclicklistener clicker = new onclicklistener() {
      public void onclick(view v) {
        onplay(mstartplaying);
        if (mstartplaying) {
          settext("stop playing");
        } else {
          settext("start playing");
        }
        mstartplaying = !mstartplaying;
      }
    };
    public playbutton(context ctx) {
      super(ctx);
      settext("start playing");
      setonclicklistener(clicker);
    }
  }
  public audiorecordtest() {
    mfilename = environment.getexternalstoragedirectory().getabsolutepath();
    mfilename += "/audiorecordtest.3gp";
  }
  @override
  public void oncreate(bundle icicle) {
    super.oncreate(icicle);
    linearlayout ll = new linearlayout(this);
    mrecordbutton = new recordbutton(this);
    ll.addview(mrecordbutton,
      new linearlayout.layoutparams(
        viewgroup.layoutparams.wrap_content,
        viewgroup.layoutparams.wrap_content,
        0));
    mplaybutton = new playbutton(this);
    ll.addview(mplaybutton,
      new linearlayout.layoutparams(
        viewgroup.layoutparams.wrap_content,
        viewgroup.layoutparams.wrap_content,
        0));
    setcontentview(ll);
  }
  @override
  public void onpause() {
    super.onpause();
    if (mrecorder != null) {
      mrecorder.release();
      mrecorder = null;
    }
    if (mplayer != null) {
      mplayer.release();
      mplayer = null;
    }
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android多媒体操作技巧汇总(音频,视频,录音等)》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。