- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在市场上有一个应用程序使用 AudioRecord 类录制音频并将其保存到 SD 卡。新 HTC One 系列手机的用户无法录制。
当开始录制时,它会将一个 1kb 的文件保存到 SD 卡并在那里停止。通常它至少会创建一个 44KB 的 header ,所以我相信它会在 prepare() 函数的某个地方停止。我没有 HTC 一台来测试这个,所以我傻眼了。我附上了我用来录制的类(class),我正在录制未压缩的音频。在我的录音 Activity 中,我使用构造函数初始化 extAudioRecorder 对象,如下所示:
extAudioRecorder = new ExtAudioRecorder(true,
AudioSource.MIC,
44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
我觉得这应该适用于较新的手机。
如有任何帮助,我们将不胜感激。这是我用于录制的类(class)的完整来源。它也可能对 LG Optimus One 手机做同样的事情。
public class ExtAudioRecorder implements Runnable
{
private final static int[] sampleRates = {44100, 22050, 11025, 8000};
private static final String AUDIO_RECORDER_FOLDER = "FOLDER";
String sdPath = Environment.getExternalStorageDirectory().getPath();
File file = new File(sdPath,AUDIO_RECORDER_FOLDER);
String FullFilePath;
String fileName;
static int sampleRate;
private volatile boolean recording = false;
double RecordReadDelayInSeconds = 0;
double RecordDelayInSeconds = 0;
private MediaPlayer mPlayer;
private static String mp3Path = Environment.getExternalStorageDirectory() + "/FOLDER/tmp/tmp.mp3";
double beatDelayInSeconds = 0;
public ExtAudioRecorder getInstanse(Boolean recordingCompressed)
{
ExtAudioRecorder result = null;
if(recordingCompressed)
{
result = new ExtAudioRecorder( false,
AudioSource.MIC,
sampleRates[3],
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
}
else
{
int i=0;
do
{
result = new ExtAudioRecorder( true,
AudioSource.MIC,
sampleRates[i],
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
sampleRate = sampleRates[i];
} while((++i<sampleRates.length) & !(result.getState() == ExtAudioRecorder.State.INITIALIZING));
}
return result;
}
/**
* INITIALIZING : recorder is initializing;
* READY : recorder has been initialized, recorder not yet started
* RECORDING : recording
* ERROR : reconstruction needed
* STOPPED: reset needed
*/
public enum State {INITIALIZING, READY, RECORDING, ERROR, STOPPED};
public static final boolean RECORDING_UNCOMPRESSED = true;
public static final boolean RECORDING_COMPRESSED = false;
// The interval in which the recorded samples are output to the file
// Used only in uncompressed mode
private static final int TIMER_INTERVAL = 120;
// Toggles uncompressed recording on/off; RECORDING_UNCOMPRESSED / RECORDING_COMPRESSED
private boolean rUncompressed;
// Recorder used for uncompressed recording
private AudioRecord audioRecorder = null;
// Recorder used for compressed recording
private MediaRecorder mediaRecorder = null;
// Stores current amplitude (only in uncompressed mode)
private int cAmplitude= 0;
// Output file path
private String filePath = null;
// Recorder state; see State
private State state;
// File writer (only in uncompressed mode)
private RandomAccessFile randomAccessWriter;
// Number of channels, sample rate, sample size(size in bits), buffer size, audio source, sample size(see AudioFormat)
private short nChannels;
private int sRate;
private short bSamples;
private int bufferSize;
private int aSource;
private int aFormat;
// Number of frames written to file on each output(only in uncompressed mode)
private int framePeriod;
// Buffer for output(only in uncompressed mode)
private byte[] buffer;
// Number of bytes written to file after header(only in uncompressed mode)
// after stop() is called, this size is written to the header/data chunk in the wave file
private int payloadSize;
/**
*
* Returns the state of the recorder in a RehearsalAudioRecord.State typed object.
* Useful, as no exceptions are thrown.
*
* @return recorder state
*/
public State getState()
{
return state;
}
/*
*
* Method used for recording.
*
*/
private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener()
{
@Override
public void onPeriodicNotification(AudioRecord recorder)
{
if (state != State.STOPPED)
{
audioRecorder.read(buffer, 0, buffer.length); // Fill buffer
try
{
randomAccessWriter.write(buffer); // Write buffer to file
payloadSize += buffer.length;
if (bSamples == 16)
{
for (int i=0; i<buffer.length/2; i++)
{ // 16bit sample size
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
if (curSample > cAmplitude)
{ // Check amplitude
cAmplitude = curSample;
}
}
}
else
{ // 8bit sample size
for (int i=0; i<buffer.length; i++)
{
if (buffer[i] > cAmplitude)
{ // Check amplitude
cAmplitude = buffer[i];
}
}
}
}
catch (IOException e)
{
Log.e(ExtAudioRecorder.class.getName(), "Error occured in updateListener, recording is aborted");
stop();
}
}
}
@Override
public void onMarkerReached(AudioRecord recorder)
{
// NOT USED
}
};
/**
*
*
* Default constructor
*
* Instantiates a new recorder, in case of compressed recording the parameters can be left as 0.
* In case of errors, no exception is thrown, but the state is set to ERROR
*
*/
public ExtAudioRecorder(boolean uncompressed, int audioSource, int sampleRate, int channelConfig, int audioFormat)
{
try
{
rUncompressed = uncompressed;
if (rUncompressed)
{ // RECORDING_UNCOMPRESSED
if (audioFormat == AudioFormat.ENCODING_PCM_16BIT)
{
bSamples = 16;
}
else
{
bSamples = 8;
}
if (channelConfig == AudioFormat.CHANNEL_IN_MONO)
{
nChannels = 1;
}
else
{
nChannels = 2;
}
aSource = audioSource;
sRate = sampleRate;
aFormat = audioFormat;
framePeriod = sampleRate * TIMER_INTERVAL / 1000;
bufferSize = framePeriod * 2 * bSamples * nChannels / 8;
if (bufferSize < AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat))
{ // Check to make sure buffer size is not smaller than the smallest allowed one
bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
// Set frame period and timer interval accordingly
framePeriod = bufferSize / ( 2 * bSamples * nChannels / 8 );
Log.w(ExtAudioRecorder.class.getName(), "Increasing buffer size to " + Integer.toString(bufferSize));
}
audioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize);
if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
throw new Exception("AudioRecord initialization failed");
audioRecorder.setRecordPositionUpdateListener(updateListener);
audioRecorder.setPositionNotificationPeriod(framePeriod);
} else
{ // RECORDING_COMPRESSED
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
cAmplitude = 0;
filePath = null;
state = State.INITIALIZING;
} catch (Exception e)
{
if (e.getMessage() != null)
{
Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "Unknown error occured while initializing recording");
}
state = State.ERROR;
}
}
/**
* Sets output file path, call directly after construction/reset.
*
* @param output file path
*
*/
public void setOutputFile(String argPath)
{
try
{
if (state == State.INITIALIZING)
{
filePath = argPath;
if (!rUncompressed)
{
mediaRecorder.setOutputFile(sdPath + "/FOLDER/" + filePath);
}
}
}
catch (Exception e)
{
if (e.getMessage() != null)
{
Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "Unknown error occured while setting output path");
}
state = State.ERROR;
}
}
/**
*
* Returns the largest amplitude sampled since the last call to this method.
*
* @return returns the largest amplitude since the last call, or 0 when not in recording state.
*
*/
public int getMaxAmplitude()
{
if (state == State.RECORDING)
{
if (rUncompressed)
{
int result = cAmplitude;
cAmplitude = 0;
return result;
}
else
{
try
{
return mediaRecorder.getMaxAmplitude();
}
catch (IllegalStateException e)
{
return 0;
}
}
}
else
{
return 0;
}
}
/**
*
* Prepares the recorder for recording, in case the recorder is not in the INITIALIZING state and the file path was not set
* the recorder is set to the ERROR state, which makes a reconstruction necessary.
* In case uncompressed recording is toggled, the header of the wave file is written.
* In case of an exception, the state is changed to ERROR
*
*/
public void prepare()
{
try
{
if (state == State.INITIALIZING)
{
if (rUncompressed)
{
if ((audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) & (filePath != null))
{
// write file header
if(!file.exists())
file.mkdirs();
fileName = filePath;
FullFilePath = file.getAbsoluteFile() + "/" + fileName;
randomAccessWriter = new RandomAccessFile(FullFilePath, "rw");
randomAccessWriter.setLength(0); // Set file length to 0, to prevent unexpected behavior in case the file already existed
randomAccessWriter.writeBytes("RIFF");
randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
randomAccessWriter.writeBytes("WAVE");
randomAccessWriter.writeBytes("fmt ");
randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
randomAccessWriter.writeShort(Short.reverseBytes(nChannels));// Number of channels, 1 for mono, 2 for stereo
randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
randomAccessWriter.writeInt(Integer.reverseBytes(sRate*bSamples*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
randomAccessWriter.writeShort(Short.reverseBytes((short)(nChannels*bSamples/8))); // Block align, NumberOfChannels*BitsPerSample/8
randomAccessWriter.writeShort(Short.reverseBytes(bSamples)); // Bits per sample
randomAccessWriter.writeBytes("data");
randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0
buffer = new byte[framePeriod*bSamples/8*nChannels];
state = State.READY;
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "prepare() method called on uninitialized recorder");
state = State.ERROR;
}
}
else
{
mediaRecorder.prepare();
state = State.READY;
}
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "prepare() method called on illegal state");
release();
state = State.ERROR;
}
}
catch(Exception e)
{
if (e.getMessage() != null)
{
Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "Unknown error occured in prepare()");
}
state = State.ERROR;
}
}
/**
*
*
* Releases the resources associated with this class, and removes the unnecessary files, when necessary
*
*/
public void release()
{
if (state == State.RECORDING)
{
stop();
}
else
{
if ((state == State.READY) & (rUncompressed))
{
try
{
randomAccessWriter.close(); // Remove prepared file
}
catch (IOException e)
{
Log.e(ExtAudioRecorder.class.getName(), "I/O exception occured while closing output file");
}
//delete file
//(new File(filePath)).delete();
}
}
if (rUncompressed)
{
if (audioRecorder != null)
{
audioRecorder.release();
}
}
else
{
if (mediaRecorder != null)
{
mediaRecorder.release();
}
}
}
/**
*
*
* Resets the recorder to the INITIALIZING state, as if it was just created.
* In case the class was in RECORDING state, the recording is stopped.
* In case of exceptions the class is set to the ERROR state.
*
*/
public void reset()
{
try
{
if (state != State.ERROR)
{
release();
filePath = null; // Reset file path
cAmplitude = 0; // Reset amplitude
if (rUncompressed)
{
audioRecorder = new AudioRecord(aSource, sRate, nChannels+1, aFormat, bufferSize);
}
else
{
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
state = State.INITIALIZING;
}
}
catch (Exception e)
{
Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
state = State.ERROR;
}
}
/**
*
*
* Starts the recording, and sets the state to RECORDING.
* Call after prepare().
*
*/
public void start()
{
if (state == State.READY)
{
if (rUncompressed)
{
payloadSize = 0;
RecordReadDelayInSeconds = 0;
RecordDelayInSeconds = 0;
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mp3Path);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long recordstarted = System.nanoTime();
audioRecorder.startRecording();
long recordstopped = System.nanoTime();
long recordDelay = recordstopped - recordstarted;
double RecordDelayInSeconds = recordDelay / 1000000.0;
Log.i("StartRecording() Delay in seconds", String.valueOf(RecordDelayInSeconds));
long recordreadstarted = System.nanoTime();
audioRecorder.read(buffer, 0, buffer.length);
long recordreadstopped = System.nanoTime();
long recordreadDelay = recordreadstopped - recordreadstarted;
RecordReadDelayInSeconds = recordreadDelay / 1000000.0;
Log.i("Record read() Delay in seconds", String.valueOf(RecordReadDelayInSeconds));
long mediastarted = System.nanoTime();
mPlayer.start();
long mediastopped = System.nanoTime();
long beatDelay = mediastopped - mediastarted;
beatDelayInSeconds = 0;
beatDelayInSeconds = (beatDelay) / 1000000000.0;
Log.i("Beat Delay in seconds", String.valueOf(beatDelayInSeconds));
}
else
{
mediaRecorder.start();
}
state = State.RECORDING;
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "start() called on illegal state");
state = State.ERROR;
}
}
/**
*
*
* Stops the recording, and sets the state to STOPPED.
* In case of further usage, a reset is needed.
* Also finalizes the wave file in case of uncompressed recording.
*
*/
public void stop()
{
if (state == State.RECORDING)
{
if (rUncompressed)
{
audioRecorder.stop();
try
{
randomAccessWriter.seek(4); // Write size to RIFF header
randomAccessWriter.writeInt(Integer.reverseBytes(36+payloadSize));
randomAccessWriter.seek(40); // Write size to Subchunk2Size field
randomAccessWriter.writeInt(Integer.reverseBytes(payloadSize));
randomAccessWriter.close();
//mPlayer.stop();
}
catch(IOException e)
{
Log.e(ExtAudioRecorder.class.getName(), "I/O exception occured while closing output file");
state = State.ERROR;
}
}
else
{
mediaRecorder.stop();
}
state = State.STOPPED;
}
else
{
Log.e(ExtAudioRecorder.class.getName(), "stop() called on illegal state");
state = State.ERROR;
}
}
/*
*
* Converts a byte[2] to a short, in LITTLE_ENDIAN format
*
*/
private short getShort(byte argB1, byte argB2)
{
return (short)(argB1 | (argB2 << 8));
}
public String[] GetFileProperties()
{
String[] fileProperties = new String[3];
fileProperties[0] = FullFilePath;
fileProperties[1] = fileName;
fileProperties[2] = Integer.toString(sampleRate);
return fileProperties;
}
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
start();
}
public void StopMediaPlayer()
{
try {
mPlayer.stop();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean isRecording() {
return recording;
}
public int GetSampleRate()
{
return sampleRate;
}
public double GetBeatDelay()
{
return beatDelayInSeconds;
}
public int GetRecordDelay()
{
return (int)(RecordReadDelayInSeconds +RecordDelayInSeconds);
}
最佳答案
我能够让这些值(value)观发挥作用:
AudioSource.MIC
44100
AudioFormat.CHANNEL_IN_MONO
AudioFormat.ENCODING_PCM_16BIT
使用此代码尝试所有值:
https://github.com/lnanek/Misc/tree/master/TestOneXAudioRecord
它在 One X(AT&T)、One X(欧洲)和 One S(T-mobile)上输出此日志条目:
D/TestOneXAudioRecordActivity(10500): 尝试频率 44100Hz, bits: 2, channel: 16
D/TestOneXAudioRecordActivity(10500):AudioRecord.getState = AudioRecord.STATE_INITIALIZED
I/TestOneXAudioRecordActivity(10500): 初始化一个 AudioRecord = android.media.AudioRecord@40ddbcf0
程序一度无法初始化 AudioRecord,但我重新启动了设备,它开始工作了。我认为另一个程序正在使用麦克风,或者我之前的一个尝试没有发布 AudioRecord,这意味着硬件很忙。因此,请确保您尝试重新启动手机,并且不要让任何其他录音应用程序运行。
关于android - AudioRecord 与新的 HTC One 系列安卓手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939311/
注意:这不是库存控制系统。我只是想绘制给哪个患者服用哪种药物的 map 。我没有考虑多少药包等。只是一次用药事件 我对数据库关系突然感到困惑,即使在与他们合作多年之后也是如此。以下是我的情况。 我有一
当用 PHP 发送群发邮件时,是向每个订阅者发送一封电子邮件(对所有电子邮件地址运行一个 for 循环)更好,还是仅将密件抄送中的所有内容添加到逗号分隔的列表中,并且因此只发送一封电子邮件? 谢谢。
我不确定我是否正确地为这种类型的关系建模,也许有人可以提供一些见解来判断这是否合理: 假设我们有一个典型的亲子类型关系,其中每个 parent 都可以有很多 child ,但我们需要跟踪 parent
我有模板和模板版本。一个模板可以有多个 template_version,但在任何给定时间只有一个事件的 template_version。我有以下两个模型: class Template 'Tem
如果我的代码是这样的: if($seconds < 60) $interval = "$seconds seconds ago"; else if($seconds < 3600) $
当我创建一对一关系迁移时,laravel 创建一对多关系。 PHP 7.1 和 MySQL 5.7 模型是:角色和用户。 角色: public function user() { return
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Java Strings: “String s = new String(”silly“);” 我正在浏览一
我正在创建一个社交网络,用户可以在其中上传和发布他们的图像和视频。现在,我不知道是否最好在我的数据库中创建一个表,将其命名为 media,并有一个 media_type 列或创建单独的图像和视频表。这
有n个线程可以访问的单例类。 每个线程加载此类的实例并在循环中调用此类的方法。 我必须控制执行流程,这样每个线程都可以调用第一个方法并暂停,只有在所有线程调用该方法一次之后,才必须恢复它们的工作。线程
存在参数数量未知(动态构建)的 MySQL 查询,其格式如下: SELECT * FROM actions WHERE user1 = ? AND user10 = ? AND user11 = ?
我检查了维基百科页面,但找不到它们之间的区别,两者似乎都将多类转换为多个线性分类器。 最佳答案 这是关于分割训练数据的策略。假设您有 N 个包含 C 类的数据样本。 一对一:在这里,您一次选择 2 个
我尝试在 sql 中插入多行。但它仅插入最后一行,并且在该行中仅存储每列的第一个字符。我通过 echo 打印查询,它只显示最后一行,但给出了每列的所有字符。另一件事是我通过单击提交按钮在两个表中插入值
我有两个实体:个人和公司。一家公司有一个或多个联系人(人)。公司至少有一个主要联系人(人)。实现这一点的最佳方法是什么? 实体如下: public class Person { public
我是 iOS 开发的新手,已经开始使用 Swift。我目前正在使用包含 3 个选项卡/导航的选项卡栏导航。我应该将 UIViewController 子类化并将其用于所有 3 个场景,还是每个场景都应
我的要求是,我需要打开两个窗口,但第二个窗口必须在第一个窗口打印并关闭后打开。可能吗? 但第二个窗口与第一个窗口同时打开。 HTML/JSP 代码打印 Java脚本函数打印(id){
经过几个小时的反复试验,我找到了这个 thread其中解释了如何建立具有相同两种类型的一对多关系和一对一关系。 但是,我无法让它与级联删除一起使用: Thrown: "Unable to determ
我想验证我的表单,如果任何输入字段为空,错误警告将显示在空白输入字段旁边。对于空白输入,错误信息必须一次全部输出,而不是一一显示。如何做到这一点? 下面是我的javascript代码: fun
我有一系列这样的字体值(命令分隔一行): Yeseva+One, Yrsa, ... 我正在寻找一个 SED(或其他 bash 工具表达式)来将每个值转换为这样的行语句: --font-yeseva-
我正在研究 中的核心音频转换服务 Learning Core Audio 我对他们 sample code 中的这个例子感到震惊: while(1) { // wrap the destina
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!