我正试图在Droid X2上使用MediaRecorder录制音频,而且我遇到了问题。 MediaRecorder似乎准备好并开始录制,但是当我停止录制并尝试收听生成的文件时,播放会立即停止。 即使我尝试在标准媒体播放器应用程序中打开音频文件,它也会立即停止。
这是我用来记录的代码:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName; mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setOutputFile(mCurrentRecordingFilePath); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // mRecorder.setAudioEncodingBitRate(16); // mRecorder.setAudioSamplingRate(44100); try { mRecorder.prepare(); mIsPrepared = true; } catch (IOException e) { Log.e("Test", "MediaRecorder prepare() failed"); e.printStackTrace(); } if (mRecorder != null && mIsPrepared) { mRecorder.start(); mIsRecording = true; Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath); }我注释掉了setAudioEncodingBitRate()调用,因为它导致prepare()失败,为了安全起见,我还注释掉了setAudioSamplingRate() 。 我已经尝试了可用的输出格式和音频编码器的每种组合,结果总是相同的。 我没有例外,并且创建了一个非空的文件,但它无法正常播放。
不确定它是否有助于诊断,但这是我用来停止录制的代码:
if (mRecorder != null && mIsRecording) { mRecorder.stop(); mRecorder.release(); mRecorder = null; mIsPrepared = false; mIsRecording = false; Log.i("Test", "Stopped audio capture"); }录制代码在Galaxy Nexus,Nexus S,EVO 4G和Galaxy SII上运行良好。 知道为什么我的音频文件会在Droid X2上出错吗?
I'm trying to record audio using MediaRecorder on the Droid X2, and I'm running into issues. The MediaRecorder seems to prepare and start recording just fine, but when I stop recording and try to listen to the file that is produced the playback immediately stops. Even if I try to open the audio file in the standard media player app, it immediately stops.
Here's the code I'm using to record:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName; mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setOutputFile(mCurrentRecordingFilePath); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // mRecorder.setAudioEncodingBitRate(16); // mRecorder.setAudioSamplingRate(44100); try { mRecorder.prepare(); mIsPrepared = true; } catch (IOException e) { Log.e("Test", "MediaRecorder prepare() failed"); e.printStackTrace(); } if (mRecorder != null && mIsPrepared) { mRecorder.start(); mIsRecording = true; Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath); }I commented out the setAudioEncodingBitRate() call because it was causing the prepare() to fail, and just to be on the safe side I also commented out setAudioSamplingRate(). I have tried every combination of output format and audio encoder that is available and the result is always the same. I get no exceptions, and a file is created that is not empty, but it will not play back properly.
Not sure if it will help diagnose, but here's the code I use to stop recording:
if (mRecorder != null && mIsRecording) { mRecorder.stop(); mRecorder.release(); mRecorder = null; mIsPrepared = false; mIsRecording = false; Log.i("Test", "Stopped audio capture"); }The recording code works fine on a Galaxy Nexus, Nexus S, EVO 4G, and Galaxy SII. Any idea why my audio file would be bad on the Droid X2?
最满意答案
在这里找到答案: MediaPlayer无法播放程序数据文件夹中的音频文件?
问题不在于记录,而是我设置MediaPlayer数据源的方式。 虽然我不知道为什么股票媒体播放器应用程序不会播放文件...也许这是一个许可的事情。 这是我用来播放文件的代码:
mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDataSource(fileName); mPlayer.prepare(); mPlayer.start();如果文件存储在外部文件目录中,则以下代码将起作用:
mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDataSource((new FileInputStream(fileName)).getFD()); mPlayer.prepare(); mPlayer.start();Found the answer here: MediaPlayer cant play audio files from program data folder?
Problem wasn't the recording, it was the way I was setting the MediaPlayer's datasource. Though I have no idea why the stock media player app wouldn't play the file... maybe it's a permission thing. Here's the code I was using to play the file:
mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDataSource(fileName); mPlayer.prepare(); mPlayer.start();Here's the code that will work if the file is stored in the external files directory:
mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDataSource((new FileInputStream(fileName)).getFD()); mPlayer.prepare(); mPlayer.start();更多推荐
发布评论