使用 ffmpeg 转换声音文件以在 Android 应用程序中使用
简短: 我试图在我的 Android 应用程序中简单地播放用 ffmpeg 转换的声音文件,但碰巧在使其正常工作时遇到问题。
长:我们有一个 iPhone 应用程序和一个 Android 应用程序做同样的事情,我必须移植在用户交互时播放声音的功能。我有 aiff
格式的源文件,并尝试将其转换为 mp3
for android。但当应用程序尝试加载文件时,它会不断崩溃
AssetFileDescriptor fileDescriptor = context.getResources().openRawResourceFd(resid);
final MediaPlayer mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
,更具体地说,mp.setDataSource
崩溃了。一些挖掘让我相信编码有问题。声音文件本身位于 res/raw 中。
11-29 17:11:48.012: ERROR/SoundManager(15580): java.io.IOException: setDataSourceFD failed.: status=0x80000000
11-29 17:11:48.012: ERROR/SoundManager(15580): at android.media.MediaPlayer.setDataSource(Native Method)
...
我尝试过的:
- 使用不同的mp3,该mp3已经在不同的地方使用相同的代码。这有效。
- 将其转换为 wav 文件。这并没有导致应用程序崩溃,但它也没有播放声音。那可能是一个不同的问题。
- 将其转换为ogg;崩溃了
,所以,ffmpeg
转换参数如下:
$ ffmpeg -i click_24db.aif -f mp3 ~/foobar/wheel_click.mp3
ffmpeg version 0.7.8, Copyright (c) 2000-2011 the FFmpeg developers
built on Nov 24 2011 14:31:00 with gcc 4.2.1 (Apple Inc. build 5666) (dot 3)
configuration: --prefix=/opt/local --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libopenjpeg --enable-libxvid --enable-libx264 --enable-libvpx --enable-libspeex --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/gcc-4.2 --arch=x86_64 --enable-yasm
libavutil 50. 43. 0 / 50. 43. 0
libavcodec 52.123. 0 / 52.123. 0
libavformat 52.111. 0 / 52.111. 0
libavdevice 52. 5. 0 / 52. 5. 0
libavfilter 1. 80. 0 / 1. 80. 0
libswscale 0. 14. 1 / 0. 14. 1
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, aiff, from 'click_24db.aif':
Duration: 00:00:00.01, start: 0.000000, bitrate: 1570 kb/s
Stream #0.0: Audio: pcm_s16be, 44100 Hz, 2 channels, s16, 1411 kb/s
Output #0, mp3, to '/Users/xyz/foobar/wheel_click.mp3':
Metadata:
TSSE : Lavf52.111.0
Stream #0.0: Audio: libmp3lame, 44100 Hz, 2 channels, s16, 64 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop, [?] for help
size= 1kB time=00:00:00.05 bitrate= 92.9kbits/s
video:0kB audio:0kB global headers:0kB muxing overhead 45.563549%
生成的文件在itunes中播放良好,在vlc中无法播放,并且在使用android.media.MediaPlayer加载时崩溃(注意:我第一次尝试)使用 SoundPool lib,同时使用 mp3 和 ogg,但这也不起作用)。
我还尝试了以下参数,但不起作用:
ffmpeg -i inputfile.aif -f mp3 -acodec libmp3lame -ab 192000 -ar 44100 outputfile.mp3
我正在 osx 上工作,今天用 macports 构建了 ffmpeg,android api 级别为 7(google api,2.1-update1)。查看 dev.android 上的“支持的格式”表并没有表明我的文件不符合规范,但我可能会弄错。
我对比特率等一无所知,所以有人可以指出我正确的 ffmpeg 参数组合以获得适用于 Android 的工作 mp3 吗?我不在乎生成的文件是 mp3、ogg 还是 3gp 或其他什么。
short: i'm trying to simply play a sound file converted with ffmpeg in my android app, but happen to have problems getting it to work.
long: we have an iphone app and an android app doing the same thing, and i have to port the feature playing a sound on an user interaction. i have the source file in the aiff
format, and tried to convert it to mp3
for android. but the app keeps crashing when it tries to load the file
AssetFileDescriptor fileDescriptor = context.getResources().openRawResourceFd(resid);
final MediaPlayer mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
more specifically, mp.setDataSource
crashes. some digging around led me to believe that something's wrong with the encoding. the sound file itself resides in res/raw.
11-29 17:11:48.012: ERROR/SoundManager(15580): java.io.IOException: setDataSourceFD failed.: status=0x80000000
11-29 17:11:48.012: ERROR/SoundManager(15580): at android.media.MediaPlayer.setDataSource(Native Method)
...
what i tried:
- using a different mp3 that's already used with the same code in a different place. this works.
- converted it to wav file. this didn't cause the app to crash, but it neither played a sound. that might be a different problem.
- converted it to ogg; crashed
so, the the ffmpeg
conversion parameters are as follows:
$ ffmpeg -i click_24db.aif -f mp3 ~/foobar/wheel_click.mp3
ffmpeg version 0.7.8, Copyright (c) 2000-2011 the FFmpeg developers
built on Nov 24 2011 14:31:00 with gcc 4.2.1 (Apple Inc. build 5666) (dot 3)
configuration: --prefix=/opt/local --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libopenjpeg --enable-libxvid --enable-libx264 --enable-libvpx --enable-libspeex --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/gcc-4.2 --arch=x86_64 --enable-yasm
libavutil 50. 43. 0 / 50. 43. 0
libavcodec 52.123. 0 / 52.123. 0
libavformat 52.111. 0 / 52.111. 0
libavdevice 52. 5. 0 / 52. 5. 0
libavfilter 1. 80. 0 / 1. 80. 0
libswscale 0. 14. 1 / 0. 14. 1
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, aiff, from 'click_24db.aif':
Duration: 00:00:00.01, start: 0.000000, bitrate: 1570 kb/s
Stream #0.0: Audio: pcm_s16be, 44100 Hz, 2 channels, s16, 1411 kb/s
Output #0, mp3, to '/Users/xyz/foobar/wheel_click.mp3':
Metadata:
TSSE : Lavf52.111.0
Stream #0.0: Audio: libmp3lame, 44100 Hz, 2 channels, s16, 64 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop, [?] for help
size= 1kB time=00:00:00.05 bitrate= 92.9kbits/s
video:0kB audio:0kB global headers:0kB muxing overhead 45.563549%
the resulting file plays nice in itunes, does not play in vlc and crashes when loaded with the android.media.MediaPlayer (note: i first tried it with the SoundPool lib, with both mp3 and ogg, but that didn't work either).
i also tried the following paramters, which didn't work:
ffmpeg -i inputfile.aif -f mp3 -acodec libmp3lame -ab 192000 -ar 44100 outputfile.mp3
i'm working on osx, built ffmpeg with macports today, android api level is 7 (google api, 2.1-update1). looking at the "supported formats" table on dev.android didn't indicate my file to be out of the spec, but i may be mistaken in that.
i don't have the slightest clue regarding bitrates and so on, so could anybody please point me to the right combination of ffmpeg parameters to get a working mp3 for android? i don't care if the resulting file would be mp3, ogg or 3gp or whatever.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许不是您想要的,但对于音频工作,SoX 可能会更好。它有点像 ffmpeg 只是用于音频。但在我看来,你用 ffmpeg 所做的事情应该是有效的。
Perhaps not what you're looking for, but for audio work you might have better luck with SoX. It's sort've like ffmpeg just for audio. Though to my eyes what you've done with ffmpeg should've worked.