MediaPlayer stream cuts out randomly
I'm trying to stream 1.5-4 hour audio files from a URL. I have my service run when a link to an mp3 file is clicked in the browser and I use this code to play it:
String sData = intent.getDataString();
if (sData != null && sData.startsWith("http:") && sData.endsWith(".mp3"))
{
if (p_oPlayer == null)
{
CreateMediaPlayer(); // sets p_oPlayer = new MediaPlayer() and creates callbacks for OnPreparedListener, OnErrorListener, OnInfoListener & OnCompletionListener
}
else
{
if (p_oPlayer.isPlaying())
p_oPlayer.stop();
p_oPlayer.reset();
}
// The next 5 lines are just to make a title开发者_运维知识库 out of a filename
String sFile = "";
Pattern p = Pattern.compile("([^/]*)\\.mp3");
Matcher m = p.matcher(sData);
if (m.find())
sFile = m.group(1).replace("%20", " ");
p_oPlayer.setDataSource(sData);
p_oPlayer.prepareAsync(); // OnPreparedListener callback just calls start() on the MediaPlayer
Intent i = new Intent(this, Main.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);
Notification notice = new Notification(R.drawable.icon2, "WFKO stream is playing", System.currentTimeMillis());
notice.setLatestEventInfo(this, "WFKO Radio", sFile, pIntent);
notice.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notice);
}
So you can see I'm using a foreground service to the Android system knows not to trash the service unless its absolutely necessary. The problem is the streams randomly cut out 10-20 minutes into playing, and it's not just hitting a certain chunk of data and crashing because if I play the same stream multiple times it will cut out at different places each time. OnCompletionListener, OnErrorListener, and OnInfoListener all do nothing. I just created them and put breakpoints on them to try to see what's happening. When the stream cuts out it goes straight to OnCompletionListener. It never calls the info or error methods.
The notification for the foreground service stays in the notification bar when the stream stops so I'm pretty sure the Android system is murdering it. I don't know where to go from here.
Does anyone have any ideas what could be wrong?
After my phone received the Gingerbread (2.3.4) update this is no longer an issue. I know there were MediaPlayer fixes in the update, so I'm going to have to assume the issue was a bug in MediaPlayer that was fixed.
精彩评论