我正在尝试从 URL 流式传输 1.5-4 小时的音频文件。当在浏览器中单击指向 mp3 文件的链接时,我会运行我的服务,并使用以下代码播放它:
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);
}
因此,您可以看到我正在使用 Android 系统的前台服务,除非绝对必要,否则不会丢弃该服务。问题是流在播放 10 到 20 分钟后随机中断,而且它不仅仅是命中某个数据块并崩溃,因为如果我多次播放同一个流,它每次都会在不同的地方中断。OnCompletionListener、OnErrorListener 和 OnInfoListener 什么都不做。我刚刚创建了它们并在它们上放置了断点以尝试查看发生了什么。当流切断时,它直接进入 OnCompletionListener。它从不调用 info 或 error 方法。
当流停止时,前台服务的通知保留在通知栏中,所以我很确定 Android 系统正在谋杀它。我不知道从这里去哪里。
有谁知道可能出了什么问题?