How to play just 30 seconds even if mp3 is 3 minutes?

1,550 views
Skip to first unread message

Manmade

unread,
Apr 20, 2012, 4:07:14 AM4/20/12
to jpl...@googlegroups.com
Im using the latest jplayer 2.1.0 as a musicplayer with a playlist.

Is it possible to only play 30 seconds of a mp3 even if it is 3 minutes long?

Could this be done so I could set the "timetoplay" in the playlist,something like this:

{
title:"Song title",
mp3:"mymp3file.mp3",
timetoplay: "0.30"

}

This would be great if it was possible, so any input appreciated, thanks!

Manmade

unread,
Apr 23, 2012, 3:15:01 AM4/23/12
to jpl...@googlegroups.com
Is it at all possible, Mark?

Nathan Horton

unread,
Apr 23, 2012, 1:07:38 PM4/23/12
to jpl...@googlegroups.com
        $("#jpId").bind($.jPlayer.event.timeupdate, function(event) {
            if(event.jPlayer.status.currentTime > 30) {
                $(this).jPlayer("stop");                
            }   
        });

Manmade

unread,
Apr 25, 2012, 4:13:06 AM4/25/12
to jpl...@googlegroups.com
Hi Nathan and thanks a lot!
It works great, it stops the song after 30 seconds, but I would like it to go to the next song in the playlist and I have tested a lot of things, but I cant get it right Im afraid.
Do you know how to step to the next song in the playlist?

And this also greats another problem, the progressbar is based on how long the mp3 is, so somehow that has to be changed aswell, so the progressbar is based on 30 seconds instead. I guess a variable that contains the amount of seconds? I havent looked into that yet, where is the progressbar reading the mp3 file to see how long it is? I will look and see if I find where it is.
Message has been deleted
Message has been deleted
Message has been deleted

Manmade

unread,
Apr 25, 2012, 6:10:23 AM4/25/12
to jpl...@googlegroups.com


I dont know how I missed it but myPlaylist.next(); makes it jump to the next song :-) , but it doesnt work on the last song in the playlist?
So I guess I have to find out if it is the last song in the playlist and if it is, then use  $(this).jPlayer("stop");   instead.

So how do I know if it is the last song?

And I still have to make the progressbar based on 30 seconds, any input about this, really appreciated, thanks! 
Message has been deleted

Manmade

unread,
Apr 25, 2012, 7:27:54 AM4/25/12
to jpl...@googlegroups.com
When I thought about it some more, the answer came to me :-) Stop it first and then go to the next!
 $(this).jPlayer("stop"); 
myPlaylist.next(); 

Ok, one problem solved, next is to get the progressbar based on 30 seconds?

Manmade

unread,
Apr 25, 2012, 8:04:13 AM4/25/12
to jpl...@googlegroups.com
Ok, solved that too!
Row 1015 in jquery.jplayer.js I changed:
cpr = 100 * media.currentTime / media.seekable.end(media.seekable.length-1);
to
cpr = 100 * media.currentTime / 30;


And now the progressbar seams to work all the time aswell with cpr = 100 * media.currentTime / 30;, so something is a bit wrong in media.seekable.end(media.seekable.length-1); since it doesnt work all the time in the iPhone?
The question is what?

Nathan Horton

unread,
Apr 25, 2012, 10:59:23 AM4/25/12
to jpl...@googlegroups.com
Good idea with the forced 30 seconds in the jplayer.js. I was going to suggest creating your own progress bar that is a 'fake one' and the real one is hidden, that way all the jplayer code stays the same preventing more problems. This can easily be done with events from the player to say when your fake progress bar should start/stop/reset.

Not working all the time in iphone seems like response header issues.

Manmade

unread,
Apr 26, 2012, 10:26:22 AM4/26/12
to jpl...@googlegroups.com
I have checked the response headers and the index.html file has "Accept-Ranges bytes" when I look in Firebug in Firefox.
But if I look at the musicplayer page itself that load in the index file, then the "Accept-Ranges" is not there, only "Accept text/html, */*; q=0.01", dont know if it is as it should or not?

What should I look for in the response headers exept the "Accept-Ranges" ?

Nathan Horton

unread,
Apr 26, 2012, 12:04:16 PM4/26/12
to jpl...@googlegroups.com
Is there a dev site we can see? 
Are you pointing right to the music file or is it being served by a PHP or some other script? (mp3:http://www.mydomain/mp3s/mp3file.mp3 OR mp3:encodeURI(http://mydomain/php/servefile?filename=file.mp3 )

You'll need to find a way to have your server email you the request headers that the iphone is sending. They should look something like this (there will be more than one request per song in most cases)
First request (just wants a range of 1 byte to TEST that range requests is available on your server):

User-Agent: AppleCoreMedia/1.0.0.9B176 (iPhone; U; CPU OS 5_1 like Mac OS X; en_us)

 Accept: */* Range: bytes=0-1

The response to this request should look like this:

Expires: -1 

Cache-Control: public, must-revalidate, max-age=0 

Pragma: public 

Accept-Ranges: bytes <----important

 Content-Range: bytes 0-1/16534377 <----important

 Content-Disposition: inline; filename=01.mp3 

Content-Length: 1 <----important

Connection: close

 Content-Transfer-Encoding: binary 

Second Request (if first request works, it requests the whole file this time):

User-Agent: AppleCoreMedia/1.0.0.9B176 (iPhone; U; CPU OS 5_1 like Mac OS X; en_us)

 Accept: */* Range: bytes=0-16534377 

Response headers should be......

Expires: -1 

Cache-Control: public, must-revalidate, max-age=0

 Pragma: public

 Accept-Ranges: bytes <----important

Content-Range: bytes 0-16534376/16534377 <----important format should be bytes begin-(end-1)/end

 Content-Disposition: inline; filename=01.mp3

 Content-Length: 16534376 <----important 

Connection: close

Content-Transfer-Encoding: binary 

Like I had said, it may or may not make more range requests like Accept: */* Range: bytes= 6569866-16534376 and you will have to supply the correct content length and content range headers for the duration and all that to work on an iphone/ipad/iwhatever.

check the group for smartreadfile function for php. if you give the function a file location it will read the file size and create the correct headers to send back. I've also got a modified version that is a little different and has some extra steps to make sure it works in every browser since they all have their own set of rules with response headers on my mp3 files. I'd be happy to share it with the group, it has been working for me pretty well so far cross browser/platform www.audiobooksnow.com

Reply all
Reply to author
Forward
0 new messages