Reputation: 113
I am trying to get an audio mp3 file from a server and play it on the android app. Here is the code for the function:
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();
}
However, I get a nullpointerexception in the line mp.setDataSource. I verified that the file's absolute path exists.
In the server side too, I verified the requests are being received correctly and its a 200 response.
When I open the URL on the browser, I get the download dialog which asks whether to save or download the file.
Where then is it giving the null pointer exception?
Upvotes: 1
Views: 756