Limit legnth on audio files on web audio player?

Discussion in 'PHP' started by Brinked, Mar 29, 2008.

  1. #1
    Does anyone know of a way to limit the amount of seconds an audio file plays either using a php function or a certain audio player? For example, I would like to play only the first 25 seconds of all the audio files on my website. thanks for any suggestions.
     
    Brinked, Mar 29, 2008 IP
  2. Finney

    Finney Peon

    Messages:
    458
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Way that first comes to my mind would be to re-record or shorten the original audo file to just the first 25 seconds of it, then put an if statement in php to say whether to show the link to play the full file or shortened file depended on something 'eg: they are registered or not'
     
    Finney, Mar 29, 2008 IP
  3. Brinked

    Brinked Well-Known Member

    Messages:
    777
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    160
    #3
    I need something that would do this automatically...as users will be uploading the files and it will take too long to make shorter versions of all the files...especially if the site gets big
     
    Brinked, Mar 29, 2008 IP
  4. Finney

    Finney Peon

    Messages:
    458
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm, I think this could be done using restrictions or some sort of script, give me 5 minutes and Ill see what I can find out.
     
    Finney, Mar 29, 2008 IP
  5. c4st

    c4st Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I came up with this.

    <?php
    
    $local_file = '/path/to/file.mp3'; // Set this to the file that you are trying to play.
    $bitrate = 128; // Bitrate of .mp3 (128, 256, 320, etc).
    $length = 29; // Length in seconds to play.
     
    
    if(file_exists($local_file) && is_file($local_file)) {
    
        $file_size = (32 / 8) + (($bitrate / 8) * $length); // .mp3 has a 32bit header, and divide by 8 to get bits to bytes.
    	 
        header('Content-Type: audio/mpeg'); 
        header('Content-Length: ' . $file_size);
            
        $file = fopen($local_file, "rb");    
        echo fread($file, $file_size);
        fclose($file);
        
    }
    
    ?>
    PHP:
    It's untested! Let me know if it works.
     
    c4st, Mar 29, 2008 IP