Eri*_*rik 13 audio android sd-card media-player
我有一个应用程序,将记录和播放音频文件.使用httpclient使用简单的标准http下载下载一些音频文件.它长久以来就像一种魅力.现在突然间我无法播放我下载的文件.它失败了这个堆栈.我将文件存储在SD卡上,我在手机和USB连接设备上都遇到了问题.
我已检查下载的文件在服务器上是否很酷,我可以毫无问题地播放它.
这些是我使用的代码片段(我知道recordingFile是文件的有效路径).
// inside the activity class
private void playRecording() throws IOException{
File recordingFile = new File(recordingFileName);
FileInputStream recordingInputStream = new FileInputStream(recordingFile);
audioMediaPlayer.playAudio(recordingInputStream);
}
Run Code Online (Sandbox Code Playgroud)
这是媒体播放器代码:
// inside my media player class which handles the recordings
public void playAudio(FileInputStream audioInputStream) throws IOException {
mediaPlayer.reset();
mediaPlayer.setDataSource(audioInputStream.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
}
Run Code Online (Sandbox Code Playgroud)
这是一个例外:
E/MediaPlayerService( 555): offset error
E/MediaPlayer( 786): Unable to to create media player
W/System.err( 786): java.io.IOException: setDataSourceFD failed.: status=0x80000000
W/System.err( 786): at android.media.MediaPlayer.setDataSource(Native Method)
W/System.err( 786): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:632)
W/System.err( 786): at net.xxx.xxx.AudioMediaPlayer.playAudio(AudioMediaPlayer.java:69)
W/System.err( 786): at net.xxx.xxx.Downloads.playRecording(Downloads.java:299)
W/System.err( 786): at net.xxx.xxx.Downloads.access$0(Downloads.java:294)
W/System.err( 786): at net.xxx.xxx.Downloads$1.onClick(Downloads.java:135)
Run Code Online (Sandbox Code Playgroud)
我试过寻找偏移误差的一些答案,但不清楚这个问题可能是什么.
PS我用这段代码下载文件:
public FileOutputStream executeHttpGet(FileOutputStream fileOutputStream) throws ClientProtocolException, IOException{
try {
// Execute HTTP Post Request
httpResponse = httpClient.execute(httpPost, localContext);
int status = httpResponse.getStatusLine().getStatusCode();
// we assume that the response body contains the error message
if (status != HttpStatus.SC_OK) {
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(ostream);
fileOutputStream = null;
} else {
InputStream content = httpResponse.getEntity().getContent();
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = content.read(buffer)) > 0 ) {
fileOutputStream.write(buffer,0, len);
}
fileOutputStream.close();
content.close(); // this will also close the connection
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
fileOutputStream = null;
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
fileOutputStream = null;
}
return fileOutputStream;
}
Run Code Online (Sandbox Code Playgroud)
我自己解决了。正如我在上面所说的,解决方案是这样的:
当我重构部分代码时,我在允许下载而不是允许下载的哈希代码上犯了一个拼写错误。不幸的是,当我下载文件时我没有正确的捕获,迫使文件为空。基本上,如果您尝试在没有正确激活码的情况下检索文件,我会发送错误的请求标头。
罪魁祸首就在这里:
if (status != HttpStatus.SC_OK) {
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(ostream);
fileOutputStream = null;
} else {
InputStream content = httpResponse.getEntity().getContent();
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = content.read(buffer)) > 0 ) {
fileOutputStream.write(buffer,0, len);
}
fileOutputStream.close();
content.close(); // this will also close the connection
}
Run Code Online (Sandbox Code Playgroud)
对于状态代码返回为错误的情况(即阻止访问的错误请求标头)。我错过的是捕获空指针的情况,这导致 SQLite 条目被更新,声称应用程序下载成功,但实际上却没有。
经验教训:即使对于原型,也始终对这些情况进行空检查。:-)
| 归档时间: |
|
| 查看次数: |
16661 次 |
| 最近记录: |