0

I have a weird issue with a Drupal website I am working on. The site has an audio player, jplayer, that plays MP3 files. Some of the files do not play. The only difference I've found between the files that play and the ones that do not is that the files that do not play have a src in the form of public://audio/example.mp3. The ones that do play have a typical url of http://example.com/audio/example.mp3. Is there anything that I should investigate as to why some of the urls get printed out with a public:// url?

This is the source code that makes the jplayer work..

<?php


/**
 * Implementation of theme_menu_tree().
 */
function wyep_menu_tree(&$variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}


/**
 * Implementation of theme_file_link().
 */
function play_file_link($variables) {

  //watchdog('theme_file_link', '<code><pre>' . print_r($variables, TRUE) . '</pre></code>');

  global $base_path, $base_url;

  $file = $variables['file'];

  $file->url = str_replace('public:/', $base_url . '/' . variable_get('file_public_path', 'files'), $file->uri);


  switch ($file->filemime) {
case 'audio/mpeg':  
    return '<script type="text/javascript">
              jQuery(document).ready(function(){
                jQuery("#jquery_jplayer_' . $file->fid . '").jPlayer({
                    ready: function (event) {
                        jQuery(this).jPlayer("setMedia", {
                            mp3:"/files/' . $file->filename . '"
                        });
                    },
                    cssSelectorAncestor: "#jp_container_' . $file->fid . '",
                    swfPath: "' . $base_url . '/sites/all/libraries/jplayer/Jplayer.swf",
                    supplied: "mp3",
                    wmode: "window"
                });
              });
            </script>

                <div id="jquery_jplayer_' . $file->fid . '" class="jp-jplayer"></div>

                <div id="jp_container_' . $file->fid . '" class="jp-audio">
                    <div class="jp-type-single">
                        <div class="jp-gui jp-interface">
                            <ul class="jp-controls">
                                <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
                                <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
                                <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
                            </ul>
                            <div class="jp-progress">
                                <div class="jp-seek-bar">
                                    <div class="jp-play-bar"></div>
                                </div>
                            </div>
                            <div class="jp-time-holder">
                                <div class="jp-current-time"></div>
                                <div class="jp-duration"></div>
                            </div>
                        </div>
                        <div class="jp-no-solution">
                            <span>Update Required</span>
                            To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
                        </div>
                    </div>
                </div>';
  }

}
2
  • 1
    Sounds like a rogue module not passing the URI through file_create_url(). What module(s) are you using for the MP3 functionality?
    – Clive
    Aug 9, 2012 at 14:35
  • The MP3 gets uploaded via a file field in the Content Type. There aren't any modules handling the jplayer. I'll update my question with the code that creates the jplayer.
    – Dustin
    Aug 9, 2012 at 14:39

2 Answers 2

2

Drupal 7 uses stream wrappers instead of "file paths" to reference files in the public and private upload directories. If you want to research that more, the rabbit hole starts here - http://drupal.org/node/555118

In a "vanilla" D7 setup "public://" is equivalent to the D6 "sites//files" or "sites/default/files" directory. These directories still exist in D7 but have been moved in D8 - using a stream wrapper means the files can be moved anywhere, even a remote location, and still be referenced in a consistent fashion if you update the default stream wrapper.

Clive's comment referencing file_create_url() is a function that takes a stream uri and converts it to a url that a web browser can use.

The problem is that your server-side code delivering media to jPlayer is not sending URIs in the right format. If it's a contrib module, you should open an issue in the queue and link to it here; Clive or myself might be able to provide a patch.

I'm going to assume that the module you're using was originally released in D6 and then upgraded at some point to D7? (maybe the upgrade didn't go so well :P)

Edit:

Based on the code that was posted after I originally put this answer up I'd suggest looking into two things:

  1. Shouldn't $file->url = str_replace('public:/', $base_url . '/' . variable_get('file_public_path', 'files'), $file->uri); be $file->url = file_create_url($file->uri); ?

  2. The "setMedia" action for jPlayer, I can't remember off the top of my head if it needs to be a url or a file path, but if it's a url shouldn't you be using $file->url? if it's a file path shouldn't it be generated by drupal_realpath()?

9
  • ah, you added your code as I was posting this. I think I see the problem, give me a minute... Aug 9, 2012 at 14:46
  • You guys are probably on to something. I know this site was in development for 3 years (yikes) and I've just recently come on board to help maintain it and fix some things. I'm betting it was upgraded from D6 to D7 at some point.
    – Dustin
    Aug 9, 2012 at 14:46
  • Beat me to it :) $file->url = file_create_url($file->uri); in place of the old line should do the trick
    – Clive
    Aug 9, 2012 at 14:47
  • Yep, that's what I was thinking too, Clive. I'll give this a shot. Thanks!!
    – Dustin
    Aug 9, 2012 at 14:48
  • Hmmm for some reason this doesn't work either. Its still putting in that public url. Very odd. However, I just changed this line mp3:"' . $file->url . '" to mp3:"/files/' . $file->filename . '" and it works. Is this an OK workaround?
    – Dustin
    Aug 9, 2012 at 14:59
0

FROM drupal.org

https://drupal.org/node/877212

file_create_url(): Translate a filepath like public://somefile.txt into an accessible URL like http://example.com/sites/default/files/somefile.txt.

Example: $url = file_create_url("public://somefile.txt");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.