In this post you will find a step by step explanation on how to make the CCK audio field display 1 Pixel Out MP3 player. Currently I am working on a project that needs a content type where members of the staff can add audio files with a name and a description. The audio file was required to be played with a simple flash player, 1PixelOut preferably. I found that audiofile CCK field, part of Media Field Project worked fine but it did not display a flash player to hear the audio. It just gives you a link to download the file.

Steps

1. Install mediafield, this module requires getID3() library 1.7.7. I install this library in /misc/lib/getid3/getid3/*.php

2. I installed SWF Tools which allowed me to embed audio files with a simple syntax like:

<?php
print swf('test.mp3');
?>

This module is helpful because it already have integration with 1 Pixel Out MP3 Player.

3. Then I downloaded the 1 Pixel Out MP3 Player and installed it in /sites/all/modules/swftools/shared/1pixelout/player.swf. You can remove all other files in the audio-player.zip

4. Now we need to edit audiofield.module to make it display our 1 Pixel Out Player. In the code look for the last function called theme_audiofield(). Replace the content with this code:

<?php
/**
 * Themed output for audiofield on a node view page. Called from formatter hook.
 */
function theme_audiofield($file, $item, $field) {
  $file = (array)$file;
  if (is_file($file['filepath'])) {
    $name = $file['filename'];
    return swf($name);
  }
}
?>

5. There is an error in audiofield.module that we need to fix, you can find more info here. In a function called audiofield_field_formatter() add a reference to multimediafile.inc

<?php
/**
 * Implementation of hook_field_formatter().
 */
function audiofield_field_formatter($field, $item, $formatter) {
  require_once(drupal_get_path('module', 'audiofield') .'/multimediafile.inc');
  if (!isset($item['fid'])) {
    return '';
  }
  $file = _field_file_load($item['fid']);
  return theme('audiofield', $file, $item, $filed);
}
?>

6. Now create a new content type and add Audio field to it. Leave blank the field called "Path to save uploaded file to" because 1 Pixel Out will look in the /files directory only.

That's it you should have a simple Audio player working with your new content type.

Comments

ShadowDrupal’s picture

All,

I am very new to Drupal.I have followed the above steps but still the player is not playing the mp3 file. Intially i got a link for my Audio file.. After doing these changes, i get a button for my player. once i click on play .. no output. Can anybody help me?

edex13’s picture

i follow all the steps and i get "error opening file" after i click 1pixeloout play button. anybody can help us?

colan’s picture

Now that there's a Mediafield Display module, you shouldn't have to deal with any of this. Hopefully it'll help you. It really did the trick for me!

suit4’s picture

i think the problem is a wrong encoded media url.

the returned url to the embeded mp3 file starts with http%3A/%252Fwww.mydomain.org/files/myaudio.mp3, which decodes to http:/%2www.mydomain.org/files/myaudio.mp3 - at least, if I try zo open the link in amarok mp3 player.
It seems to be problemtaik for flash to decode that string correctly.

Quick workaround for this bug:
modify swftools.module to use this function:

function _swftools_get_flashvars_string(&$flashvars) {
  foreach ($flashvars AS $var => $value) {
    $flashvars[$var] = str_replace(array('&', '=', '?'), array('%26', '%3D', '%3F'), $value);
  }
  $encoded = drupal_query_string_encode($flashvars,array('soundFile'));
  $encoded .= "&soundFile=".$flashvars['soundFile'];  // don't use drupals internal url encoding, flash can't read that.
  // '#' seems to encode as %2523, reverse this, using a more robust hex prefix..
  $encoded = str_replace('%2523', '0x', $encoded);
  return $encoded;
}
victorortiz’s picture

I forgot to add in the instructions my fix for this encoding issue in swftools module. But your solution is way better than mine, so I will use this one now. Thanks suit4.

bwv’s picture

Thanks a million for this code. Now I got 1pixelout working in Firefox, IE6 and IE7. regards

----------------------------------------------------------------------
http://www.bwv810.com/

I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.

Rhino’s picture

I wish I could say that this helped but I seem to get the same issue even after applying your fix here.

My audio posts gives me a flash "play" button but then "error opening file" which is because it looks for a file at;

http://myurl.com/%2Fmyurl.com/path/to/file - can't seem to get rid of that %2F

Rhino’s picture

headkit’s picture

nice work and thank you for sharing!
I have the problem that I need to get a bunch of mp3-files into a playlist. inserting a set of files to a node is not the problem, but to tell a mp3-player that there is a playlist is not easy - for me.
thanx for your help!

rvb’s picture

I couldn't find a great way to embed audio (mp3) on a page (node) on Drupal 7. One can insert it as a CCK field, but I needed to insert it into an ubercart product page.

My Solution:

I downloaded the 1pixelout code from http://wpaudioplayer.com/download/ from which I got 3 main files and uploaded it to my server like this:

http://www.fakedomain.com/myname/audio/player.swf
http://www.fakedomain.com/myname/audio/audio-player.js
http://www.fakedomain.com/myname/audio/music.mp3
and followed the tutorial here http://www.macloo.com/examples/audio_player/

I inserted the following code on the product page of my ubercart online music store:

For a second player on a page I used this HTML:

I hope it will work for you too in Drupal 7