我要做的是:使用Android的MediaCodec将原始PCM音频样本编码为原始AAC文件.
我遇到的问题:当我使用FFMPEG将生成的原始AAC文件打包到M4A容器中时,FFMPEG抱怨文件中缺少编解码器参数.
细节:
由于我找不到生成输出AAC文件的音频编码器的任何MediaCodec示例代码,我尝试将视频编码器修改为音频编码器.原始代码在这里:source_code
我配置了这样的音频编码器:
mEncoderFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", (int)mAudioSampleRate, 2);
// redundant?
mEncoderFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
mEncoderFormat.setInteger(MediaFormat.KEY_AAC_PROFILE,
MediaCodecInfo.CodecProfileLevel.AACObjectELD);
mEncoderFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, kSampleRates);
mEncoderFormat.setInteger(MediaFormat.KEY_BIT_RATE, kBitRates);
mEncoderFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
testEncoderWithFormat("audio/mp4a-latm", mEncoderFormat);
try {
codec.configure(
mEncoderFormat,
null /* surface */,
null /* crypto */,
MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (IllegalStateException e) {
Log.e(TAG, "codec '" + componentName + "' failed configuration.");
return;
}
Log.d(TAG, " testEncoder configured with format = " + format);
Run Code Online (Sandbox Code Playgroud)
然后我给编码器每帧提供10ms的PCM采样.编码器获取每个帧,生成一个比特流帧,然后将比特流写入FileOutputStream.循环一直持续到输入文件结束.
代码运行完成.我'adb pull'将生成的AAC文件从设备传送到我的PC,并使用FFMPEG来读取它.下面是命令和错误FFMPEG吐出:
$ ffmpeg -f aac -i BlessedNoColor_nexus7_api18.aac
ffmpeg version N-45739-g04bf2e7 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在API 16及更高版本的设备上使用Android Renderscript支持库,遵循此处描述的步骤.到目前为止事情并不顺利.
我的Renderscript代码如下所示.
#pragma version(1)
#pragma rs java_package_name(com.xxx.renderscript.test)
#include "rs_time.rsh"
rs_script flipScript;
rs_allocation gIn;
rs_allocation gOut;
int width;
int height;
int direction = 0;
void root(uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
if(direction == 0) { // flip horizontally
const uchar4 *element = rsGetElementAt(gIn, width - x, y);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r, color.g, color.b};
*v_out = rsPackColorTo8888(output);
}
else if(direction == 1) { // flip vertically
const uchar4 *element = …Run Code Online (Sandbox Code Playgroud) 我正在开发一款在相机视图上执行OpenCL/OpenGL互操作的Android应用.我正在使用GLSurfaceView.Renderer.当然,从onSurfaceCreated调用创建和初始化OpenCL运行环境(来自OpenGL)的代码,并且每个预览框架的实际处理都在onDrawFrame中进行.
一切顺利,除非我完成,我想清理OpenCL的东西.理想情况下,onSurfaceDestroyed方法是清理的最佳位置,但GLSurfaceView.Renderer中没有这样的方法.所以清理代码无处可去,我的应用程序可能存在内存泄漏.
这是我的问题:
为什么GLSurfaceView.Renderer中没有onSurfaceDestroyed方法?有onSurfaceCreated和onSurfaceChanged.人们会期望onSurfaceDestroyed在那里.
鉴于GLSurfaceView.Renderer中不存在onSurfaceDestroyed,我的清理代码应该去哪里,为什么?
从surfaceChanged()方法调用Camera.setParameters()时出现RuntimeException错误。我看到了有关同一问题的其他文章,但没有找到明确的解决方案: 将参数设置为camera时出现问题
我的问题是:
为什么在surfaceCreated()之后立即调用surfaceChanged()?(至少对我而言)在surfaceCreated()中启动预览,只是停止预览并在surfaceChanged()中重新启动另一个预览是很直观的。
第一次调用surfaceChanged()时,是什么决定传入的宽度和高度?如前所述,并在我的实验中进行了验证,Camera.setParameters()不一定支持这些值。有关我的代码段和日志信息,请参见下文。
在调用surfaceChanged()之前,有没有办法确保Camera.setParameters()支持宽度和高度值?可以在surfaceChanged()中添加代码以确保实现。例如,可以将w和h与所有支持的预览大小进行比较,并将其更改为最接近的支持值。但是,这样做会影响性能。
下面的列表是我的代码段和LogCat输出。日志记录信息表明surfaceChanged()中的Camera.setParameters()引起了错误。如果我注释掉调用setParameters()的行,则不会再看到错误消息。
[ 编辑:我正在Amazon Kindle Fire HD上进行测试。Android版本是4.0.3,API 15]
程式码片段:
public void surfaceCreated(SurfaceHolder holder) {
// Log & debug
Log.d(TAG, "surfaceCreated() is called");
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "surfaceCreated(): " + e.getMessage());
}
if (mHolder.getSurface() == null) {
Log.d(TAG, "surfaceCreated(): preview surface does not exist yet!");
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Log …Run Code Online (Sandbox Code Playgroud) android ×4
audio ×1
eclipse ×1
encoding ×1
java ×1
mediacodec ×1
opencl ×1
opengl-es ×1
preview ×1
renderscript ×1
surfaceview ×1