scorpio1441
scorpio1441

Reputation: 3088

jPlayer does not cache the media while playing, if output is provided by PHP script

In other words: it does not cache rest of the song while it's playing.

JS:

$('#jplayer-9999').jPlayer({
    ready: function() {
        $(this).jPlayer('setMedia', {
            oga: 'http://mysite/getogg.php',
        }).jPlayer('play', 15);

        $(this).bind($.jPlayer.event.timeupdate, function(event) {
            if(event.jPlayer.status.currentTime > 55) {
                $(this).jPlayer('play', 15);
            }
        });

    },
    play: function() {
        $(this).jPlayer('pauseOthers');
    },
    cssSelectorAncestor: '#jp_container_9999',
    swfPath: '/js/jplayer',
    supplied: 'oga',
    preload: 'auto'
});

PHP (getogg.php):

<?php

header('Content-type: audio/ogg');
$output = readfile("/oggs/1234.ogg");

echo $output;

?>

However direct links work just fine and song is caching: oga: 'http://mysite/oggs/1234.ogg'

Please help resolving this issue.

Upvotes: 1

Views: 1877

Answers (2)

scorpio1441
scorpio1441

Reputation: 3088

The issue was related to OGG format. Changing media to MP3 fixed an issue.

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

Try This:

<?php
error_reporting(E_ALL);

$status=stream("./oggs/1234.ogg");

if($status !== true){
    if($status=='1'){echo('Cannot stream a folder check path!');}
    if($status=='2'){echo('File not found!');}
}

function stream($file,$speed=1024){
    if (file_exists($file)) {
        if(is_dir($file)){return '1';}
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: '.sprintf("%u", filesize($file)));

        ob_clean();
        $handle = fopen($file, "rb");
        $chunksize=(sprintf("%u", filesize($file))/$speed);

        set_time_limit(0);
        while (!feof($handle)) {
            echo fgets($handle, $chunksize);
            flush();
        }
        fclose($handle);
        return true;
    }else{
        return '2';
    }
    return;
}
?>

Upvotes: 1

Related Questions