音频录制



 

可以使用MediaRecorder录制音频和视频

 

 

 

准备三张图片,名字分别为:file_icon、record、stop。

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#3399ff"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <LinearLayout

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:orientation="horizontal"

      android:layout_marginTop="8dp">

      <ImageButton

          android:id="@+id/record"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:src="@drawable/record"/>

      <ImageButton

          android:id="@+id/stop"

          android:layout_marginLeft="8dp"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:src="@drawable/stop"/>

  </LinearLayout>

  <TextView

      android:id="@+id/info"

      android:gravity="center_horizontal"

      android:layout_marginTop="8dp"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="文字提示信息"

      android:textColor="#ffffff"/>

  <ListView

      android:id="@+id/reclist"

      android:layout_marginLeft="8dp"

      android:layout_marginTop="8dp"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"/>

</LinearLayout>

 

 

 

 

 

 

新建布局文件recordfile.xml:

 

<TableLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#3399ff">

  <TableRow

      android:gravity="center_vertical">

      <ImageView

          android:id="@+id/icon"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:src="@drawable/file_icon"/>

      <TextView

          android:id="@+id/filename"

          android:layout_marginLeft="8dp"

          android:textColor="#ffffff"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

  </TableRow>

</TableLayout>

 

 

 

 

 

在MyMediaRecorderDemo.java中:

 

package com.li.mediarecorder;

 

import java.io.File;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import android.app.Activity;

import android.content.Intent;

import android.media.MediaRecorder;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ImageButton;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

 

public class MyMediaRecorderDemo extends Activity {

  private ImageButton record = null;

  private ImageButton stop = null;

  private TextView info = null;

  private ListView reclist = null;

  private SimpleAdapter recordSimpleAdapter = null;

  private MediaRecorder mediaRecorder = null;

  private boolean sdcardExists = false; // 判断sd卡是否存在

  private File recordAudioSaveFileDir = null; // 保存所有音频文件的文件夹

  private File recordAudioSaveFile = null;  // 每次保存音频文件的名称

  private String recordAudioSaveFileName = null; // 每次保存音频文件的名称

  private String recDir = "liyewenrec"; // 保存的目录名称

  private boolean isRecord = false ; // 录音的标志

  private List<Map<String,Object>> recordFiles = null ;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.record = (ImageButton) super.findViewById(R.id.record);

     this.stop = (ImageButton) super.findViewById(R.id.stop);

     this.info = (TextView) super.findViewById(R.id.info);

     this.reclist = (ListView) super.findViewById(R.id.reclist);

     // 如果存在则将状态给了sdcardExists属性

     if ((this.sdcardExists = Environment.getExternalStorageState().equals(

         Environment.MEDIA_MOUNTED))) { // 判断sd卡是否存在

       this.recordAudioSaveFileDir = new File(Environment

            .getExternalStorageDirectory().toString()

            + File.separator

            + MyMediaRecorderDemo.this.recDir + File.separator);

       if (!this.recordAudioSaveFileDir.exists()) { // 文件夹不存在

         this.recordAudioSaveFileDir.mkdirs(); // 创建文件夹

       }

     }

     this.stop.setEnabled(false) ; // 按钮现在不可用

     this.record.setOnClickListener(new RecordOnClickListenerImpl());

     this.stop.setOnClickListener(new StopOnClickListenerImpl());

     this.reclist.setOnItemClickListener(new OnItemClickListenerImpl()) ;

     this.getRecordFiles() ;

  }

 

  private class RecordOnClickListenerImpl implements OnClickListener {

 

     public void onClick(View v) {

       if(MyMediaRecorderDemo.this.sdcardExists) { // 如果sd卡存在

         MyMediaRecorderDemo.this.recordAudioSaveFileName = MyMediaRecorderDemo.this.recordAudioSaveFileDir

              .toString()

              + File.separator

              + "MLDNRecord_"

              + System.currentTimeMillis() + ".3gp";  // 每次的录音文件名称都不一样

         MyMediaRecorderDemo.this.recordAudioSaveFile = new File(

              MyMediaRecorderDemo.this.recordAudioSaveFileName);

         MyMediaRecorderDemo.this.mediaRecorder = new MediaRecorder(); // 实例化对象

         // 在进行录制之前必须配置若干个参数

         MyMediaRecorderDemo.this.mediaRecorder

              .setAudioSource(MediaRecorder.AudioSource.MIC); // 音频来源是MIC

         MyMediaRecorderDemo.this.mediaRecorder

              .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

         MyMediaRecorderDemo.this.mediaRecorder

              .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

         MyMediaRecorderDemo.this.mediaRecorder

              .setOutputFile(MyMediaRecorderDemo.this.recordAudioSaveFileName);

         try {  // 进入到就绪状态

            MyMediaRecorderDemo.this.mediaRecorder.prepare() ;

         } catch (Exception e) {

            // Log.i("MyMediaRecorderDemo", e.toString()) ;

         }

         MyMediaRecorderDemo.this.mediaRecorder.start() ; // 开始录音

         MyMediaRecorderDemo.this.info.setText("正在录音中...") ;

         MyMediaRecorderDemo.this.stop.setEnabled(true); // 停止录音按钮可以使用了

         MyMediaRecorderDemo.this.record.setEnabled(false) ;

         MyMediaRecorderDemo.this.isRecord = true ;  // 正在录音

       }

     }

  }

 

  private class StopOnClickListenerImpl implements OnClickListener {

     public void onClick(View v) {

       if(MyMediaRecorderDemo.this.isRecord) { // 正在录音

         MyMediaRecorderDemo.this.mediaRecorder.stop() ;  // 停止

         MyMediaRecorderDemo.this.mediaRecorder.release() ;  // 释放资源

         MyMediaRecorderDemo.this.record.setEnabled(true) ;

         MyMediaRecorderDemo.this.stop.setEnabled(false) ;

         MyMediaRecorderDemo.this.info.setText("录音结束,文件路径为:"

              + MyMediaRecorderDemo.this.recordAudioSaveFileName);

         MyMediaRecorderDemo.this.getRecordFiles() ;

       }

     }

  }

 

  private void getRecordFiles(){ // 将一个文件夹之中的全部文件列出

     this.recordFiles = new ArrayList<Map<String, Object>>();

     if(this.sdcardExists) { // 有sd卡存在

       File files [] = this.recordAudioSaveFileDir.listFiles() ;  // 列出目录中的文件

       for (int x = 0; x < files.length; x++) {

          Map<String, Object> fileInfo = new HashMap<String, Object>();

         fileInfo.put("filename", files[x].getName()) ;

         this.recordFiles.add(fileInfo) ;

       }

       this.recordSimpleAdapter = new SimpleAdapter(this,

            this.recordFiles, R.layout.recordfile,

           new String[] { "filename" }, new int[] { R.id.filename });

       this.reclist.setAdapter(this.recordSimpleAdapter) ;

     }

  }

  private class OnItemClickListenerImpl implements OnItemClickListener {

 

     public void onItemClick(AdapterView<?> parent, View view, int position,

         long id) {

       if (MyMediaRecorderDemo.this.recordSimpleAdapter.getItem(position) instanceof Map) {

         Map<?, ?> map = (Map<?, ?>) MyMediaRecorderDemo.this.recordSimpleAdapter

              .getItem(position);

         Uri uri = Uri

              .fromFile(new File(MyMediaRecorderDemo.this.recordAudioSaveFileDir

                   .toString()

                   + File.separator

                   + map.get("filename")));

         Intent intent = new Intent(Intent.ACTION_VIEW) ;

         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;

         intent.setDataAndType(uri, "audio/mp3") ;

         MyMediaRecorderDemo.this.startActivity(intent) ;

       }

     }

  }

}

 

 

 

 

 

 

修改AndroidManifest.xml:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.mediarecorder"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyMediaRecorderDemo"

            android:label="@string/title_activity_my_media_recorder_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip
毕设新项目基于python3.7+django+sqlite开发的学生就业管理系统源码+使用说明(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 学生就业管理系统(前端) ## 项目开发环境 - IDE: vscode - node版本: v12.14.1 - npm版本: 6.13.4 - vue版本: @vue/cli 4.1.2 - 操作系统: UOS 20 ## 1.进入项目目录安装依赖 ``` npm install ``` ## 2.命令行执行进入UI界面进行项目管理 ``` vue ui ``` ## 3.编译发布包(请注意编译后存储路径) #### PS:需要将编译后的包复制到后端项目的根目录下并命名为'static' 学生就业管理系统(后端) ## 1.项目开发环境 - IDE: vscode - Django版本: 3.0.3 - Python版本: python3.7.3 - 数据库 : sqlite3(测试专用) - 操作系统 : UOS 20 ## 2.csdn下载本项目并生成/安装依赖 ``` pip freeze > requirements.txt pip install -r requirements.txt ``` ## 3.项目MySQL数据库链接错误 [点击查看解决方法](https://www.cnblogs.com/izbw/p/11279237.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值