I have a problem playing (streaming) MP3 files using Android's Media Player. My idea is to read NFC tag and place that data URL
into a string
(songs_urls). After that, media player should use that string and play file from that link.
Everything works fine when I define link, for example String link = "www.blabla.com/audiofile.mp3", but the problem is when I want to read it from the NFC tag.
I have followed this tutorial to build Media Player: http://www.youtube.com/watch?v=LKL-efbiIAM. Everything regarding Media Player is in onCreate() function. NFC part is in the onResume() function. So I have, just moved the Media Player code below from onCreate() to onResume().
Here is the MP code I've moved to onResume()
:
ImageButton btn_play = (ImageButton) findViewById(R.id.imageButton3);
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_song_title.setText(songs_urls);
try {
mediaPlayer.setDataSource(songs_urls);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}
});
The line
txt_song_title.setText(songs_urls);
works (text changes) but everything below (in try/catch blocks
) doesn't work - the file can't be played.
Thanks in advance.