Display mp3 path instead of downloading

Asked

Viewed 211 times

1

Hello. I have a class in my project that joins several audio files and return me another mp3 with all these files in one the download starts automatically (without requiring the user to click anything).

Is there any way I can make this file play instead of downloading?

Class:

<?php
// This is the class to generate mp3 files based on the anti-spam words
// Based on the PHP mp3 class at http://www.sourcerally.net/Scripts/20-PHP-MP3-Class
// Output code based on the FPDF class at http://www.fpdf.org
class mp3
  {
    var $str;
    var $time;
    var $frames;

    // Create a new mp3
    function mp3($path="")
    {
    if($path!="")
        {
        $this->str = file_get_contents($path);
        }
    }

  // Put an mp3 behind the first mp3
  function mergeBehind($mp3)
  {
  $this->str .= $mp3->str;
  }

  // Calculate where's the end of the sound file
  function getIdvEnd()
  {
  $strlen = strlen($this->str);
  $str = substr($this->str,($strlen-128));
  $str1 = substr($str,0,3);
  if(strtolower($str1) == strtolower('TAG'))
  {
  return $str;
  }
  else
  {
  return false;
  }
  }

  // Calculate where's the beginning of the sound file
  function getStart()
  {
  $strlen = strlen($this->str);
  for($i=0;$i<$strlen;$i++)
  {
  $v = substr($this->str,$i,1);
  $value = ord($v);
  if($value == 255)
  {
  return $i;
  }
  }
  }

  // Remove the ID3 tags
  function striptags()
  {
  //Remove start stuff...
  $newStr = '';
  $s = $start = $this->getStart();
  if($s===false)
  {
  return false;
  }
  else
  {
  $this->str = substr($this->str,$start);
  }
  //Remove end tag stuff
  $end = $this->getIdvEnd();
  if($end!==false)
  {
  $this->str = substr($this->str,0,(strlen($this->str)-129));
  }
  }

  // Display an error
  function error($msg)
  {
  //Fatal error
  die('<strong>audio file error: </strong>'.$msg);
  }

 // Send the new mp3 to the browser
  function output($path)
  {
  //Output mp3
  //Send to standard output
  if(ob_get_contents())
  $this->error('Some data has already been output, can\'t send mp3 file');
  if(php_sapi_name()!='cli')
  {
  //We send to a browser
  header('Content-Type: audio/mpeg3');
  if(headers_sent())
  $this->error('Some data has already been output to browser, can\'t send mp3 file');
  header('Content-Length: '.strlen($this->str));
  header('Content-Disposition: attachment; filename="'.$path.'"');
  }
  echo $this->str;
  return '';
  }
  }
  ?>

Using:

// Specify the word 
$petersword = "testword";

$word_count = strlen($petersword);

 // Set up the first file
  if ($word_count > 0) {
  $mp3 = new mp3($cas_fontpath . 'sounds/' . substr($petersword, 0, 1) . '.mp3');
  $mp3->striptags();
  }

// Generate the mp3 file from each letter in the word 
  for ($i = 1; $i < $word_count; ++$i) {
  $cas_character = $cas_fontpath . 'sounds/' . substr($petersword, $i, 1);
  $cas_mp3equivalent = new mp3($cas_character . '.mp3');
  $mp3->mergeBehind($cas_mp3equivalent);
  $mp3->striptags();
  }

  // Spit out the audio file!  
$mp3->output('word.mp3');
  • If I understand, you want that at the time of clicking on the link it does not download the file, but rather open the computer player (which is possible) or tap directly on the page, this is it?

  • Exactly, but the problem is that I do not where to get a path for this file to play.

  • Where is the file saved? Could you provide an example?

  • The class calls a function called output and the download starts

  • http://www.theblog.ca/merge-mp3s-php This class I’m using

  • Allow me to edit your question, for its context is outside the context of your real doubt.

Show 1 more comment

2 answers

2

1


Remove this from your code $mp3->output('word.mp3'); and in the class you can add a new method, in this method you will have to manipulate the variable $this->str, would be something like:

class mp3 {
...
    function save($path) {
       file_put_contents($path, $this->str);
    }
}

Your code should look like this:

// Specify the word 
$petersword = "testword";

$word_count = strlen($petersword);

 // Set up the first file
  if ($word_count > 0) {
  $mp3 = new mp3($cas_fontpath . 'sounds/' . substr($petersword, 0, 1) . '.mp3');
  $mp3->striptags();
  }

// Generate the mp3 file from each letter in the word 
  for ($i = 1; $i < $word_count; ++$i) {
  $cas_character = $cas_fontpath . 'sounds/' . substr($petersword, $i, 1);
  $cas_mp3equivalent = new mp3($cas_character . '.mp3');
  $mp3->mergeBehind($cas_mp3equivalent);
  $mp3->striptags();
  }

//Salva o arquivo
$mp3->save('/var/www/projetc1/static/sounds/word.mp3');

And to display you will have to take the virtual path of the folder static/sounds (is just an example) that would be something like:

http://localhost/projetc1/static/sounds/word.mp3

Then you decide which way you want the file to be saved :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.