简体   繁体   中英

playframework 2.0 streaming audio

So im using playframework 2.0 and I am successfully streaming audio using the following code on the server:

public static Result recording() {
    File wavFile = new File("C:\\test.wav");
    return ok(wavFile);
}

And this works fine.

the problem i have is that it changes to a new page. I would like it instead to open up a little dialog box maybe, or create a new surrounding div. So I figure I have to create an ajax request, and I can create aa new div but im not sure what to do with the successful data object. Any examples would be really helpful I havent been able to find any:

$('#sound').click( function(evt) {
            $.ajax({
                type: 'POST',
                url:  jQuery("#sound").attr("href"),
                data: jQuery("#sound").serialize(),
                dataType: "json",
                success: function(data) {
                            //What do i need to do here
                    alert('Call history download');
                },
                error: function(data) {
                    setError('Call history download failed');
                }
            });
            return false;
        });

Check out the html5 <audio> tag .

Something like this should do the trick:

<audio src="@routes.MyController.recording()" controls autoplay loop>  
   <p>Your browser does not support the audio element </p>  
</audio>

If you want to start the sound with javascript, you can skip all the ajax stuff, and just add a element using javascript/jQuery. Then the sound should start automatically.

Note, that wave files are not supported by IE (figures!!). IE 9 only supports MP3. But Firefox and Opera does not support MP3. To support multiple file formats, you can do something like this:

<audio controls autoplay loop>  
   <source src="@routes.MyController.recordingWave()" type="audio/wave" />
   <source src="@routes.MyController.recordingMp3()" type="audio/mp3" />
   <p>Your browser does not support the audio element </p>  
</audio>

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