简体   繁体   English

给定网址直接播放mp3文件

[英]Play mp3 file given a url directly to the mp3

I want to be able to play an mp3 file in my android application given a url (eg http://www.somedomain.com/audio.mp3 ). 我希望能够在给定网址(例如http://www.somedomain.com/audio.mp3 )的android应用程序中播放mp3文件。

This I think should be used to get a byte array using an InputStream maybe but I'm not totally sure, which then can be used with a MediaPlayer object. 我认为应该使用InputStream来获取字节数组,但是我不确定,然后可以将其与MediaPlayer对象一起使用。

Read through the SDK pages. 通读SDK页面。 Playing from a File or Stream 从文件或流播放

I found in practical life that the MediaPlayer can play just about any MP3 URL if you take care to reformat any Space characters (0x20) into "%20". 在实际生活中,我发现如果您小心地将任何空格字符(0x20)重新格式化为“%20”,MediaPlayer几乎可以播放任何MP3 URL。 I tried that URLEncoder and some other filters to little gain but simply converting those spaces before presenting the URL to MediaPlayer will take you far. 我尝试使用URLEncoder和其他一些过滤器获得很少的收益,但是在将URL呈现给MediaPlayer之前简单地转换这些空格将使您受益匪浅。

In a String you can easily substitute stuff: 在字符串中,您可以轻松替换内容:

mediaPlayer.setDataSource(urlString.replace(" ", "%20")); mediaPlayer.setDataSource(urlString.replace(“”,“%20”)));

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

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