简体   繁体   English

Android MediaPlayer实时音频流传输:如何知道是否无法流式传输实时URL?

[英]Android MediaPlayer live audio streaming : how to know if a live streaming URL can't be streamed?

I have an app to stream live audio streams. 我有一个用于直播音频流的应用程序。

My question is : how to check if a live streaming url can't be streamed ? 我的问题是:如何检查实时流URL是否不能流?

There are several reasons why an url can't be streamed : 网址无法流传输的原因有几个:

  • totally wrong url which points to nothing 完全错误的网址,指向无内容
  • url which does not point to an audio stream (but to an image for example) 不指向音频流的url(例如,指向图像)
  • url relative to an audio stream which cannot be streamed because the audio format is not compatible with android MediaPlayer 相对于音频流的URL,该音频流无法传输,因为音频格式与android MediaPlayer不兼容
  • probably many other reasons... 可能还有其他原因...

If possible, I would like to check if an url can be streamed by Android MediaPlayer without launching the buffering/preparing process. 如果可能的话,我想检查一下是否可以在不启动缓冲/准备过程的情况下通过Android MediaPlayer流式传输URL。

How can I do that ? 我怎样才能做到这一点 ?

Thanks ! 谢谢 !

The quickest way to check if a given remote URL is alive is to do a HTTP request and check the response header. 检查给定远程URL是否有效的最快方法是发出HTTP请求并检查响应标头。

However, this will only tell you if the URL is alive, but no if it's a valid streamable resource or not. 但是,这只会告诉您该URL是否有效,而不能告诉您是否为有效的可流传输资源。

Your question asks if it is is possible to check for the latter without starting the buffering process. 您的问题询问是否可以在不启动缓冲过程的情况下检查后者。 As far as I know, there is no way. 据我所知,这是不可能的。

Your best bet is to prepare a MediaPlayer object asynchronously (via prepareAsync() ) and register a MediaPlayer.OnErrorListener . 最好的选择是异步准备MediaPlayer对象(通过prepareAsync() )并注册MediaPlayer.OnErrorListener Don't register a setOnPreparedListener() if you don't want to play the media after it is done pre-buffering. 如果您不想在完成预缓冲后播放媒体,则不要注册setOnPreparedListener()。 However, do note that the OnErrorListener will not be called if the stream URL is valid. 但是,请注意,如果流URL有效,则不会调用OnErrorListener。

Your best bet for just plain checking is to register both OnErrorListener and OnPreparedListener and call your own callback depending on which one of the two was called first. 仅作简单检查的最佳选择是同时注册OnErrorListener和OnPreparedListener并调用您自己的回调,这取决于首先调用两者中的哪一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM