简体   繁体   中英

Streaming audio via http

Given an audio .mp3 file how could one create an audio stream from this file and share it as url "http://..." . I have actually tried to implement a Servlet that would serve my file to a MediaPlayer object but it seems that when requesting the servlet, one would rather download the file instead of getting the audio stream (the onPrepared condition for the mediaPlayer is forever set to false although the process keeps running). Here is the source code :

button_play.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            try{
                mPlayer.reset();                   
                mPlayer.setDataSource("http://localhost:8080/FileServlet/song/sample.mp3");                    
                mPlayer.prepareAsync();

                mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
                    @Override
                    public void onPrepared(MediaPlayer mp){
                        mp.start();

                    }
                });

            } catch (IllegalStateException e) {
                System.out.println(e.getMessage());
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
            catch (IllegalArgumentException e) {
                System.out.println(e.getMessage());
            }
            catch (SecurityException e) {
                System.out.println(e.getMessage());
            }

        }

    });

Any idea please how may I achieve that?

Actually the MediaPlayer object is the one who decides how to use the "file" : either download it and play it afterwards OR play it as it downloads.

Method 1 is "safe" because server might go down, or some sites dont allow downloads that take more than 15 minutes and tries to disconnect them.Method 2 is bad on slow internet providers (you woudnt want to hear 1 second of music every 5 seconds as it streams)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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