MediaPlayer 流随机剪切
我正在尝试从 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 系统正在谋杀它。我不知道从这里该去哪里。
有谁知道可能出了什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的手机收到 Gingerbread (2.3.4) 更新后,这不再是问题。我知道更新中修复了 MediaPlayer,因此我必须假设该问题是已修复的 MediaPlayer 中的错误。
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.