- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一款使用 mediarecorder 录制语音并使用 mediaplayer 播放音乐的 Android 应用。
我的目标是将两个音频混合到一个文件中,因为 Android 没有为此提供任何 API,所以我正在寻找一个合理的解决方案。
目前,在播放时,我正在使用带有 MIC 源的新媒体记录器来捕获音频并保存它,但这非常糟糕!!!
要混合音频吗?包括任何原生解决方案 lix SOX 或 FFMPEG?
或者,无论如何要使用媒体播放器输出作为源而不是使用 MIC 来记录到文件中?
如有任何建议,我们将不胜感激。
谢谢。
最佳答案
当我遇到同样的问题时,我找到了混合文件的解决方案。由于无法混合两个 mp3 文件,您必须先将其转换为 wave 格式,然后设置标题值。之后添加数据字段。我按照以下方式做到了这一点。希望我的代码能帮到你。
类 MixFile 扩展了 AsyncTask{
ProgressDialog dialog;
protected void onPreExecute() {
dialog= new ProgressDialog(MainActivity.this);
dialog.setCancelable(false);
dialog.setMessage("Mixing two wav files");
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
short[] audioData1 = null;
short[] audioData2 = null;
int n = 0;
try {
DataInputStream in1;
//in1 = new DataInputStream(new FileInputStream(Environment.getExternalStorageDirectory() + "/Soundrecpluspro/one.wav")); in1 = new DataInputStream(new FileInputStream(path1)); ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData1 = new short[sb.capacity()];
for (int i = 0; i < sb.capacity(); i++) {
audioData1[i] = sb.get(i);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
DataInputStream in1;
in1 = new DataInputStream(new FileInputStream(path2));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData2= new short[sb.capacity()];
sb.get(audioData2);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
// find the max:
float max = 0;
Log.d("File audio lenght 1 ", ""+audioData1.length);
Log.d("File audio lenght 2 ", ""+audioData2.length);
System.out.println("MainActivity.MixFile.doInBackground() 1"+audioData1.length);
System.out.println("MainActivity.MixFile.doInBackground() 2"+audioData2.length);
if(audioData1.length > audioData2.length){
for (int i = 22; i < audioData2.length; i++) {
if (Math.abs(audioData1[i] + audioData2[i]) > max)
max = Math.abs(audioData1[i] + audioData2[i]);
}
System.out.println("" + (Short.MAX_VALUE - max));
int a, b, c;
// now find the result, with scaling:
for (int i = 22; i < audioData2.length; i++) {
a = audioData1[i];
b = audioData2[i];
c = Math.round(Short.MAX_VALUE * (audioData1[i] + audioData2[i])
/ max);
if (c > Short.MAX_VALUE)
c = Short.MAX_VALUE;
if (c < Short.MIN_VALUE)
c = Short.MIN_VALUE;
audioData1[i] = (short) c;
}
// to turn shorts back to bytes.
byte[] end = new byte[audioData1.length * 2];
ByteBuffer.wrap(end).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioData1);
try {
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/assets/mixer12.wav");
for (int i = 0; i < end.length; i++) {
out.write(end[i]);
out.flush();
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("MainActivity.MixFile.doInBackground() smaller one");
for (int i = 22; i < audioData1.length; i++) {
if (Math.abs(audioData2[i] + audioData1[i]) > max)
max = Math.abs(audioData2[i] + audioData1[i]);
}
System.out.println("" + (Short.MAX_VALUE - max));
int a, b, c;
// now find the result, with scaling:
for (int i = 22; i < audioData1.length; i++) {
a = audioData2[i];
b = audioData1[i];
c = Math.round(Short.MAX_VALUE * (audioData2[i] + audioData1[i])
/ max);
if (c > Short.MAX_VALUE)
c = Short.MAX_VALUE;
if (c < Short.MIN_VALUE)
c = Short.MIN_VALUE;
audioData2[i] = (short) c;
}
// to turn shorts back to bytes.
byte[] end = new byte[audioData2.length * 2];
ByteBuffer.wrap(end).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioData2);
try {
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Assets/mixer1.wav");
for (int i = 0; i < end.length; i++) {
out.write(end[i]);
out.flush();
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
在 Activity 中我这样调用它
public class MainActivity extends Activity implements OnClickListener {
new MixFile().execute();
}
这里path1和path2是你要混音的wav文件的路径
关于Android:混合音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803889/
我知道您不应该将打印与 printf,cout 和 wprintf,wcout 混合使用,但是很难找到一个好的答案,为什么以及是否可以绕过它。问题是我使用了一个用 printf 打印的外部库,而我自己
我有以下问题: class A: animal = 'gerbil' def __init__(self): self.result = self.calculate_
我在屏幕上渲染了一堆形状(多边形),我没有使用深度测试。 我只是希望这些形状在绘制在空白区域时使用自己的颜色,并且在绘制到任何非空区域时使用红色像素,即在我的情况下绘制在另一个多边形上。 这里的问题实
我正在尝试在我的 Groovy/Grails 应用程序中混入一个类,我正在使用 the syntax defined in the docs ,但我不断收到错误消息。 我有一个如下所示的域类: cla
我已经找到了 5349574673 个关于 Alpha 混合的页面,但我仍然无法获得想要的结果。我正在尝试使用 opengl 使 gif/png 文件正确显示(具有透明度/半透明度)。 这是我的初始化
我正在尝试记录以下代码,但我似乎无法让 JSDoc 记录该类,甚至无法引用它的存在。 // SomeMixin.js export default superclass => class SomeMi
我有一个类型家族,我想使用 mixin 以模块化方式“丰富”它们。例如: trait Family { self => trait Dog { def dogname:String
我在 Storyboard中有 Collection View 。我在 Storyboard中有一部分单元格,还有我以编程方式创建的部分单元格。我应该在 sizeForItemAtIndexPath
我有一个字节数组,我想更改它的访问方式。这是数组: char bytes[100]; 我想要另一个数组来改变原始数组的访问方式。如果我们可以将引用放在数组中,它看起来像这样: char& bytes_
我需要从 c 文件调用 cpp 方法。我为此编写了这个界面.. cpp文件 extern "C" void C_Test(int p){ Class::CPP_Test(p); } c文件
我的网站有两份 CSS 表,一份是主 CSS,一份是移动 CSS。问题是在移动设备(iPhone、Android)上查看时,两个样式表会混淆。例如,在 iPhone 上查看网站时,会应用主样式表中的某
维护人员的说明:此问题涉及已过时的 bokeh.charts API,该 API 已于多年前删除。有关使用现代 Bokeh 创建各种条形图的信息,请参阅: https://docs.bokeh.org
在下图中,蓝色圆圈仅用于调试目的。我的目标是蓝色圆圈后面的每一层都应该是透明的。我只想保持蓝色圆圈外面的可见。 这是用 swift 编写的代码: let croissantView = UIV
我不是 SQL 专家。我正在使用 SQL Server 2005,我正在尝试弄清楚如何构造一个查询,以便它可以满足多种要求。我有两个表定义如下: Classroom - ID - Departme
原创: 我之前问过这个问题,但我最初的例子有点不完整,我想我现在可以更具体地说明我的问题。 对于上下文,我在旧的 Apple mac 计算机上使用 openGL 3.3 并尝试渲染四边形的重叠层。每个
是否可以将内联(类似 json)映射与同一对象的常规映射定义混合使用? 考虑以下示例: person: {age: 32, weight: 82} name: foo 生成的人应具有给定的年龄、体
假设我有一个 Parent 类,它有四个字段 A、B、C 和 D,这样 C 和 D 可以选择传递或使用默认实现进行初始化: open class Parent(val a: A, val b: B,
我正在使用 symphony (1.4) 框架在 PHP 中开发一个 Web 应用程序。该代码使用 SVN 进行版本控制。在此网络应用程序中,我们所有客户共享一个共同的基础,以及一些专门为每个客户创建
我想使用两个小部件(一次一个)作为我的应用程序的基础/背景,上面有一个 QML UI 和一个无边框窗口。它应该看起来像这样: 基于 OpenGL 的扫描组件 通过窗口句柄操作的 3D 可视化组件 多个
我们有一个混合的 AngularJS/Angular 8 应用程序,并且我们不断遇到来自不同版本框架的组件之间的变化检测非常慢的问题。到目前为止,我们只在 Angular 组件中使用 AngularJ
我是一名优秀的程序员,十分优秀!