Android - 网络广播流媒体

发布于 2024-12-27 08:24:56 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2025-01-03 08:24:56

源的 URL 为: http://shoutcast2.omroep.nl:8104/

初始化MediaPlayer,你需要几行代码。现在

MediaPlayer player = new MediaPlayer();
player.setDataSource("http://shoutcast2.omroep.nl:8104/");

,MediaPlayer 对象已初始化,您可以开始流式传输了。好吧,实际上不是。您将需要发出 MediaPlayer 的准备命令。有两种变体。

1.准备():这是一个同步调用,它会被阻塞,直到MediaPlayer对象进入准备状态。如果您尝试播放需要 MediaPlayer 更长时间的本地文件,这是可以的,否则您的主线程将被阻塞。
prepareAsync():顾名思义,这是一个异步调用。它立即返回。但是,这显然并不意味着 MediaPlayer 已经准备好。您仍然需要等待它进入准备状态,但由于此方法不会阻塞您的主线程,因此当您尝试从其他地方流式传输某些内容时,可以使用此方法。当 MediaPlayer 通过 onPrepared(MediaPlayer mp) 方法准备好时,您将收到回调,然后就可以开始播放了。
因此,对于我们的示例,最佳选择是:

2。 player.prepareAsync();
您需要将侦听器附加到 MediaPlayer 以在准备好时接收回调。这是代码。

player.setOnPreparedListener(new OnPreparedListener(){
            public void onPrepared(MediaPlayer mp) {
                     player.start();
            } 
});

The URL for the source is: http://shoutcast2.omroep.nl:8104/

To initialize the MediaPlayer, you need a few lines of code. There you go:

MediaPlayer player = new MediaPlayer();
player.setDataSource("http://shoutcast2.omroep.nl:8104/");

Now that the MediaPlayer object is initialized, you are ready to start streaming. Ok, not actually. You will need to issue the MediaPlayer's prepare command. There are 2 variations of this.

1. prepare(): This is a synchronous call, which is blocked until the MediaPlayer object gets into the prepared state. This is okay if you are trying to play local files that would take the MediaPlayer longer, else your main thread will be blocked.
prepareAsync(): This is, as the name suggests, an asynchronous call. It returns immediately. But, that obvisouly, doesn't mean that the MediaPlayer is prepared yet. You will still have to wait for it to get into the prepared state, but since this method will not block your main thread, you can use this method when you are trying to stream some content from somewhere else. You will get a callback, when the MediaPlayer is ready through onPrepared(MediaPlayer mp) method, and then, the playing can start.
So, for our example, the best choice would be:

2. player.prepareAsync();
You need to attach a listener to the MediaPlayer to receive the callback when it is prepared. This is the code for that.

player.setOnPreparedListener(new OnPreparedListener(){
            public void onPrepared(MediaPlayer mp) {
                     player.start();
            } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文