android 手机一边录音一边播放 仿yy的试听功能

2023-10-31 19:18

本文主要是介绍android 手机一边录音一边播放 仿yy的试听功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 android中AudioRecord采集音频的参数说明

在android中采集音频的api是android.media.AudioRecord类

其中构造器的几个参数就是标准的声音采集参数

以下是参数的含义解释

public AudioRecord (int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes)

Since: API Level 3

Class constructor.

Parameters

audioSource

the recording source. See MediaRecorder.AudioSource for recording source definitions.

音频源:指的是从哪里采集音频。这里我们当然是从麦克风采集音频,所以此参数的值为MIC

sampleRateInHz

the sample rate expressed in Hertz. Examples of rates are (but not limited to) 44100, 22050 and 11025.

采样率:音频的采样频率,每秒钟能够采样的次数,采样率越高,音质越高。给出的实例是44100、22050、11025但不限于这几个参数。例如要采集低质量的音频就可以使用4000、8000等低采样率。

channelConfig

describes the configuration of the audio channels. See CHANNEL_IN_MONO and CHANNEL_IN_STEREO

声道设置:android支持双声道立体声和单声道。MONO单声道,STEREO立体声

audioFormat

the format in which the audio data is represented. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT

编码制式和采样大小:采集来的数据当然使用PCM编码(脉冲代码调制编码,即PCM编码。PCM通过抽样、量化、编码三个步骤将连续变化的模拟信号转换为数字编码。) android支持的采样大小16bit 或者8bit。当然采样大小越大,那么信息量越多,音质也越高,现在主流的采样大小都是16bit,在低质量的语音传输的时候8bit足够了。

bufferSizeInBytes

the total size (in bytes) of the buffer where audio data is written to during the recording. New audio data can be read from this buffer in smaller chunks than this size. SeegetMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioRecord instance. Using values smaller than getMinBufferSize() will result in an initialization failure.

采集数据需要的缓冲区的大小,如果不知道最小需要的大小可以在getMinBufferSize()查看。

二 代码

package com.example.superb.yy4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends Activity {
PipedInputStream in;
boolean isRrcord;
mAudio mm ;
mAudioPlayer m;

TextView T1,T2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn=findViewById(R.id.search_close_btn);T1=findViewById(R.id.dddd);isRrcord = false;}
public void btnclick(View v){if (isRrcord){isRrcord = false;mm.stopRecord();m.stopPlay();btn.setText("开始");T1.setText("点击开始");}else{isRrcord = true;startRecord();btn.setText("停止");T1.setText("点击停止");}
}private void startRecord(){in = new PipedInputStream();new Thread(new Runnable() {@Overridepublic void run() {try {mm = new mAudio(MainActivity.this, in);mm.StartAudioData();} catch (IOException e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {byte[] buffer = new byte[1024];PipedOutputStream pout = new PipedOutputStream();m = new mAudioPlayer();try {m.setOutputStream(pout);new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubm.startPlayAudio();}}).start();} catch (IOException e1) {e1.printStackTrace();}int size = 0 ;try {while (true){while (in.available()>0){size = in.read(buffer);pout.write(buffer, 0, size);}}} catch (IOException e) {e.printStackTrace();}}}).start();
}

}

package com.example.superb.yy4;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

/*

  • To getaudio or play audio

  • */
    public class mAudio {
    private AudioRecord audioRecord;
    private Context context;
    private boolean isRecording = false ;
    private PipedOutputStream outstream ;//利用管道传输数据
    public mAudio(Context context , PipedInputStream instream) throws IOException {
    this.context = context;
    //初始化管道流 用于向外传输数据
    outstream = new PipedOutputStream();
    outstream.connect(instream);
    }
    public void StartAudioData(){//得到录音数据
    int frequency = 11025;

     //frequency采样率:音频的采样频率,每秒钟能够采样的次数,采样率越高,音质越高。// 给出的实例是44100、22050、11025但不限于这几个参数。// 例如要采集低质量的音频就可以使用4000、8000等低采样率。@SuppressWarnings("deprecation")int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;//立体声录制通道int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;//编码制式和采样大小:采集来的数据当然使用PCM编码(脉冲代码调制编码,// 即PCM编码。PCM通过抽样、量化、编码三个步骤将连续变化的模拟信号转换为数字编码。) android支持的采样大小16bit 或者8bit。// 当然采样大小越大,那么信息量越多,音质也越高,// 现在主流的采样大小都是16bit,在低质量的语音传输的时候8bit足够了。//int buffersize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
    

    //采集数据需要的缓冲区的大小,如果不知道最小需要的大小可以在getMinBufferSize()查看。
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
    frequency, channelConfiguration, audioEncoding, buffersize);
    //音频源:指的是从哪里采集音频。这里我们当然是从麦克风采集音频,所以此参数的值为MIC

     //frequency采样率:音频的采样频率,每秒钟能够采样的次数,采样率越高,音质越高。// 给出的实例是44100、22050、11025但不限于这几个参数。// 例如要采集低质量的音频就可以使用4000、8000等低采样率。byte[]buffer  = new byte[buffersize];audioRecord.startRecording();//开始录音isRecording = true;int bufferReadSize = 1024;while (isRecording){audioRecord.read(buffer, 0, bufferReadSize);try {outstream.write(buffer, 0, bufferReadSize);} catch (IOException e) {e.printStackTrace();}}
    

    }
    public void stopRecord(){//停止录音
    isRecording = false;
    audioRecord.stop();
    try {
    outstream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

}

package com.example.superb.yy4;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

public class mAudioPlayer {
private PipedInputStream instream;
private boolean isPlaying ;
private AudioTrack audioplayer;
private byte[] buffer;
public mAudioPlayer() {
isPlaying = false;
instream = null;
//初始化播音类
@SuppressWarnings(“deprecation”)
int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);
}
//设置管道流,用于接受音频数据
public void setOutputStream(PipedOutputStream out) throws IOException{
instream = new PipedInputStream(out);

}
public void startPlayAudio(){ //调用之前先调用setOutputStream 函数isPlaying = true;audioplayer.play();//开始接受数据流播放buffer = new byte[1024];while (instream!=null&&isPlaying){try {while (instream.available()>0){int size = instream.read(buffer);audioplayer.write(buffer, 0, size);//不断播放数据}} catch (IOException e) {e.printStackTrace();}}
}
public void stopPlay(){//停止播放isPlaying = false ;try {instream.close();} catch (IOException e) {e.printStackTrace();}audioplayer.stop();
}

}
参考:
https://www.cnblogs.com/nanguabing/archive/2012/12/16/2820732.html
http://www.cnblogs.com/mythou/p/3242000.html

下载demo

这篇关于android 手机一边录音一边播放 仿yy的试听功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/317542

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python脚本轻松实现检测麦克风功能

《Python脚本轻松实现检测麦克风功能》在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的,本文将介绍一个简单的Python脚本,能够帮助我们检测本地麦克风的功能,需要的... 目录轻松检测麦克风功能脚本介绍一、python环境准备二、代码解析三、使用方法四、知识扩展轻松检测麦

Java实现TXT文件导入功能的详细步骤

《Java实现TXT文件导入功能的详细步骤》在实际开发中,很多应用场景需要将用户上传的TXT文件进行解析,并将文件中的数据导入到数据库或其他存储系统中,本文将演示如何用Java实现一个基本的TXT文件... 目录前言1. 项目需求分析2. 示例文件格式3. 实现步骤3.1. 准备数据库(假设使用 mysql

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook