我正在尝试从服务器获取音频 mp3 文件并在 android 应用程序上播放。这是该函数的代码:
protected void managerOfSound(String theText) throws MalformedURLException, IOException {
if (mp != null) {
mp.reset();
mp.release();
}
URLConnection conn = new URL("http://192.168.1.68:3000/play/song.mp3").openConnection();
conn.connect();
InputStream stream = conn.getInputStream();
File file = File.createTempFile("downloadingMedia", ".mp3");
byte[] buffer = new byte[4096];
int n = - 1;
OutputStream output = new FileOutputStream( file );
while ( (n = stream.read(buffer)) != -1)
{
if (n > 0)
{
output.write(buffer, 0, n);
}
}
output.close();
System.out.println("PATH IS" + file.getAbsolutePath());
mp.setDataSource(file.getAbsolutePath());
mp.start();
}
但是,我在 mp.setDataSource 行中得到了一个 nullpointerexception。我验证了文件的绝对路径存在。
在服务器端,我也验证了请求被正确接收并且它是 200 响应。
当我在浏览器上打开 URL 时,会出现下载对话框,询问是保存还是下载文件。
那么它在哪里给出空指针异常?