- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此代码旨在录制音频,将该音频保存为 wav 文件,并打印原始音频数据。当我运行代码时,它每次都会打印不同的字符串,如下所示:[B@431b2168我的问题:我是否正确地从 wav 文件中提取了音频数据?如果是这样,那串数字是什么意思?这是代码:
package com.example.wesle.noisemachine;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
/*
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
*/
public class ReceiveScreen extends AppCompatActivity {
private Button buttonStart, buttonStop, buttonDecode, buttonPlay;
private String filePath;
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
short[] audioData;
private AudioRecord recorder = null;
private int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;
Complex[] fftTempArray;
Complex[] fftArray;
int[] bufferData;
int bytesRecorded;
//byte[] payload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_screen);
filePath = getFilename();
final File wavfile = new File(filePath);
//final MediaPlayer wavRecording = MediaPlayer.create(this, "/storage/emulated/0");
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonDecode = (Button) findViewById(R.id.buttonDecode);
buttonStop.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
//outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
//System.out.println(outputFile);
bufferSize = AudioRecord.getMinBufferSize
(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING)*3;
audioData = new short [bufferSize];
//payload = new byte [bufferSize];
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,
bufferSize);
int i = recorder.getState();
if (i==1)
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
buttonStop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
if (null != recorder){
isRecording = false;
int i = recorder.getState();
if (i==1)
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
//filePath = getFilename();
copyWaveFile(getTempFilename(),filePath);
deleteTempFile();
Toast.makeText(getApplicationContext(), "Recording Completed", Toast.LENGTH_LONG).show();
buttonStart.setEnabled(true);
buttonPlay.setEnabled(true);
buttonDecode.setEnabled(true);
}
});
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
Toast.makeText(getApplicationContext(), "Recording Playing", Toast.LENGTH_LONG).show();
//play code
//File wavfile = new File(filePath);
Uri myUri1 = Uri.fromFile(wavfile);
final MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
buttonStart.setEnabled(true);
buttonDecode.setEnabled(true);
buttonPlay.setEnabled(true);
}
});
buttonDecode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
try {
ByteArrayOutputStream outt = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(wavfile));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
outt.write(buff, 0, read);
}
outt.flush();
byte[] audioBytes = outt.toByteArray();
System.out.println(audioBytes);
} catch (IOException e) {
e.printStackTrace();
}
buttonStart.setEnabled(true);
buttonDecode.setEnabled(true);
buttonPlay.setEnabled(true);
}
});
//Code for the back button
Button backbuttonR = (Button) findViewById(R.id.backbuttonR);
backbuttonR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ReceiveScreen.this, MainActivity.class));
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
System.out.println(file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_WAV);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_WAV);
}
private String getTempFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
if (tempFile.exists())
tempFile.delete();
return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}
private void writeAudioDataToFile() {
byte data[] = new byte[bufferSize];
String filename = getTempFilename();
FileOutputStream os = null;
try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int read = 0;
if (null != os) {
while(isRecording) {
read = recorder.read(data, 0, bufferSize);
if (read > 0){
}
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
//payload[] = payload + data;
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void deleteTempFile() {
File file = new File(getTempFilename());
file.delete();
}
private void copyWaveFile(String inFilename,String outFilename){
FileInputStream in = null;
FileOutputStream out = null;
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = RECORDER_SAMPLERATE;
int channels = 2;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;
byte[] data = new byte[bufferSize];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
System.out.println("File size: " + totalDataLen);
WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
while(in.read(data) != -1) {
out.write(data);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void WriteWaveFileHeader(
FileOutputStream out, long totalAudioLen,
long totalDataLen, long longSampleRate, int channels,
long byteRate) throws IOException
{
byte[] header = new byte[44];
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (2 * 16 / 8); // block align
header[33] = 0;
header[34] = RECORDER_BPP; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
out.write(header, 0, 44);
}
/*public class SoundPoolPlayer {
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();
public SoundPoolPlayer(Context pContext)
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSounds.put(R.raw.<sound_1_name>, this.mShortPlayer.load(pContext, R.raw.<sound_1_name>, 1));
mSounds.put(R.raw.<sound_2_name>, this.mShortPlayer.load(pContext, R.raw.<sound_2_name>, 1));
}
public void playShortResource(int piResource) {
int iSoundId = (Integer) mSounds.get(piResource);
this.mShortPlayer.play(iSoundId, 0.99f, 0.99f, 0, 0, 1);
}
// Cleanup
public void release() {
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}*/
public static final class Complex {
// The number stored is x+I*y.
final private double x, y;
// I don't want to allow anyone to access these numbers so I've labeled
// them private.
/** Construct a point from real and imaginary parts. */
public Complex(double real_part, double imaginary_part) {
x=real_part;
y=imaginary_part;
}
/** Construct a real number. */
public Complex(double real_part) {
x=real_part;
y=0;
}
// A static constructor.
/** Construct a complex number from the given polar coordinates. */
public static Complex fromPolar(double r, double theta) {
return new Complex(r*Math.cos(theta), r*Math.sin(theta));
}
// Basic operations on Complex numbers.
/** Return the real part. */
public double re(){
return x;
}
/** Return the imaginary part. */
public double im(){
return y;
}
/** Return the complex conjugate */
public Complex conj() {
return new Complex(x,-y);
}
/** Return the square of the absolute value. */
public double absSquared() {
return x*x+y*y;
}
/** Return the absolute value. */
public double abs() {
// The java.lang.Math package contains many useful mathematical functions,
// including the square root function.
return Math.sqrt(absSquared());
}
// ARITHMETIC
/** Add a complex number to this one.
*
* @param z The complex number to be added.
* @return A new complex number which is the sum.
*/
public Complex add(Complex z) {
return new Complex(x+z.x, y+z.y);
}
/** Subtract a complex number from this one.
*
* @param z The complex number to be subtracted.
* @return A new complex number which is the sum.
*/
public Complex minus(Complex z) {
return new Complex(x-z.x, y-z.y);
}
/** Negate this complex number.
*
* @return The negation.
*/
public Complex neg() {
return new Complex(-x, -y);
}
/** Compute the product of two complex numbers
*
* @param z The complex number to be multiplied.
* @return A new complex number which is the product.
*/
public Complex mult(Complex z) {
return new Complex(x*z.x-y*z.y, x*z.y+z.x*y);
}
/** Divide this complex number by a real number.
*
* @param q The number to divide by.
* @return A new complex number representing the quotient.
*/
public Complex div(double q) {
return new Complex(x/q,y/q);
}
/** Return the multiplicative inverse. */
public Complex inv() {
// find the square of the absolute value of this complex number.
double abs_squared=absSquared();
return new Complex(x/abs_squared, -y/abs_squared);
}
/** Compute the quotient of two complex numbers.
*
* @param z The complex number to divide this one by.
* @return A new complex number which is the quotient.
*/
public Complex div(Complex z) {
return mult(z.inv());
}
/** Return the complex exponential of this complex number. */
public Complex exp() {
return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y));
}
// FUNCTIONS WHICH KEEP JAVA HAPPY:
/** Returns this point as a string.
* The main purpose of this function is for printing the string out,
* so we return a string in a (fairly) human readable format.
*/
// The _optional_ override directive "@Override" below just says we are
// overriding a function defined in a parent class. In this case, the
// parent is java.lang.Object. All classes in Java have the Object class
// as a superclass.
@Override
public String toString() {
// Comments:
// 1) "" represents the empty string.
// 2) If you add something to a string, it converts the thing you
// are adding to a string, and then concatentates it with the string.
// We do some voodoo to make sure the number is displayed reasonably.
if (y==0) {
return ""+x;
}
if (y>0) {
return ""+x+"+"+y+"*I";
}
// otherwise y<0.
return ""+x+"-"+(-y)+"*I";
}
/** Return true if the object is a complex number which is equal to this complex number. */
@Override
public boolean equals(Object obj) {
// Return false if the object is null
if (obj == null) {
return false;
}
// Return false if the object is not a Complex number
if (!(obj instanceof Complex)) {
return false;
}
// Now the object must be a Complex number, so we can convert it to a
// Complex number.
Complex other = (Complex) obj;
// If the x-coordinates are not equal, then return false.
if (x != other.x) {
return false;
}
// If the y-coordinates are not equal, then return false.
if (y != other.y) {
return false;
}
// Both parts are equal, so return true.
return true;
}
// Remark: In Java, we should really override the hashcode function
// whenever we override the equals function. But, I don't want to
// get into this for a light introduction to programming in java.
// Hash codes are necessary for various of Java's collections. See HashSet for instance.
// The following was generated by Netbeans.
@Override
public int hashCode() {
int hash = 3;
hash = 83 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 83 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
return hash;
}
}
}
最佳答案
您得到的是数组的内存地址。您应该遍历数组并打印每个字节。你可以这样做:
for(int i = 0;i<audioBytes.length;i++){
System.out.println(audioBytes[i]);
}
关于java - 从 wav 文件解释原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44800438/
我在一个项目中工作,该项目需要 SQL 结果的最佳性能,并且希望优化查询,但经过反复试验后,我在 IN 方面遇到了一些问题。 -- THIS RETURNS NO RESULTS AT ALL. SE
在尝试创建一个实际上非常简单的 SQL 语句时,我发现自己迷失了方向。 我有一个包含 3 个表的数据库: 食谱 - 存储一些用于 cooking 的食谱名称 配料食谱 - 将配料与食谱链接 成分 -
我正在尝试理解 PHP 中的 Hebrev 函数。 https://php.net/manual/en/function.hebrevc.php 它说:“将逻辑希伯来语文本转换为视觉文本”。但我不明白
嗨,我在 Grid view 的 android 文档中发现了一段代码对于以下代码。 gridview.setOnItemClickListener(new OnItemClickListener()
谁能解释一下 InfiniBand 是什么?与以太网相比的主要区别是什么,这些差异如何使其比以太网更快? 在官方description从 mellanox 写到 Introduce InfiniBan
这个问题已经有答案了: How are java increment statements evaluated in complex expressions (1 个回答) 已关闭 8 年前。 我知道
我正在阅读 MySQL 教程,我遇到了这个: SELECT /*! SQL_NO_CACHE */ user FROM users; 为什么优化提示 SQL_NO_CACHE 包含在: /*!
我无法理解$(this),我做了一个剪刀石头布的版本,并应用了 jQuery 让用户在计算机上选择按钮选项。我希望有人能解释一下 $(this) 指的是什么,它是 btn-primary 吗?该函数在
我不是很确定 while(choice == 1 || choice ==2);谁能解释一下。我明白这一点 if(choice ==1) displayMonthly(rainfall); e
let flyRight = CABasicAnimation(keyPath: "position.x") flyRight.toValue = view.bounds.size.width/2 f
目录 解释:int型默认值为0 但我们尝试发现并不能通过: 原因: int的默认值为0,而Integer的默认值为null
我正在处理一个查询,自从一个 SSRS 服务器传输到另一个服务器后,它似乎没有按预期执行,并且 where 语句的一部分中出现了以下行 找出不同之处,或者至少从我能找到的地方来看。 where COA
我正在制作一个退回检测程序,读取退回邮件。我们的设置是发送电子邮件,在发送的邮件中添加一个 noreply@domain.tl。一些收件人不再存在,因此我们想要读取退回邮件,并检测它发送给谁。我已经崩
我有一个关于公式通过控制点弯曲的问题。 如您所知,HTML Canvas 有 quadraticCurveTo(x1, y1, x2, y2)与 x1 and x2作为控制点。 但是,当您尝试使用它绘
我有一个 Emakefile看起来像: %% -- %% %% -- {'/Users/user/projects/custom_test/trunk/*', [debug_info, {out
我有一个非常简单的问题。这不仅适用于 spray-json,而且我已经阅读了 argonaut 和 circe 的类似声明。所以请赐教。 在 spray-json 中,我遇到了 There is no
我正在为视频添加水印。我试图让水印与视频尺寸成比例。我已经使用 scale2ref 看到了十几个不同的答案,但没有解释实际发生了什么,所以我发现很难知道如何实现/更改配置以适应我的情况。 当前覆盖命令
因为我正在学习语言,所以我在玩 Haskell,我只是发现了一些我不理解的东西,我找不到解释。如果我尝试运行此代码: map (`div` 0) [1,2,3,4] 我得到一个除以 0 的异常,这是预
我正在寻找解决错误对象引用未设置到对象实例的步骤/指南。以及问题发生原因的解释。 我正在寻找更一般的解释,所以如果我收到错误,我应该采取什么步骤来查找问题。我经常看到有人提供特定代码段的帖子,而其他人
我最近想升级我的知识React ,所以我从组件生命周期方法开始。让我好奇的第一件事是这个componentWillReceiveProps .所以,文档说当组件接收新的(不一定是更新的) Prop 时
我是一名优秀的程序员,十分优秀!