3

I have an upload phone app that uploads either images or audio recordings. The image can be uploaded to a mysql database and displayed no problem in a webpage, The problem i am having is playing the audio on the webpage, i have tested the webpage by playing mp3 files i have placed in the database myself though the webpage. When i upload an mp3 file from the phone or emulator nothing plays. The database is populated with a file size and on the webpage a media player does load, but nothing plays

Edit 1 Since writing this i have learnt that android records in .amr files and not .mp3 which i had been creating the file as. Since then i can play the file on my desktop using either quicktime or real player. When i attempt to play the file through a web browser i get a quicktime logo with a question mark in the middle. Below is my code for playing the file.

header('Content-type: audio/amr');

$query = mysql_query("SELECT * FROM media WHERE media_id= '$idd'"); 

$row = mysql_fetch_array($query); 

echo $row['file'];

Embed code where 110 is $idd in the above

<object data="sound.php?id=110 width='391' height='298'"> <embed src='sound.php' width='391' height='298'></embed>'ERROR' </object>

Edit 2

If i put the same file directly on my server and NOT in the database this code downloads the file

<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="100" height="100"codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
<param name='src' value="apr.amr">
<param name='autoplay' value="true">
<param name='controller' value="true">
<param name='loop' value="true">
<EMBED src="apr.amr" width="100" height="100" autoplay="true" controller="true" loop="true" pluginspage='http://www.apple.com/quicktime/download/'>
</EMBED>
</OBJECT>

Edit 3 upload code

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      $size = $_FILES['image']['size'];
      $type = $_FILES['image']['type'];

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  

      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      fclose($fp);

      $data = addslashes($data);

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO media";
      $query .= "(file, file_size, file_type) VALUES ('$data','$size','$type')";
      $results = mysql_query($query, $link);

      $mediaid = mysql_insert_id();

    $gender = $_POST['gender'];
    $cat_id = $_POST['cat'];
    $name = $_POST['name'];
    $lat = $_POST['lat'];
    $lon = $_POST['lon'];
    $user = $_POST['user'];


  $query="INSERT INTO instance (name, gender, cat_id, lon, lat, user_id) VALUES ('$name', '$gender', '$cat_id', '$lon', '$lat', '$user')";
  $result=mysql_query($query);


      $instanceid = mysql_insert_id();

      $query4 = "INSERT INTO media_link";
      $query4 .="(media_id, instance_id) Values ('$mediaid','$instanceid')";
      $results4 = mysql_query($query4, $link);

    }

// Close our MySQL Link
mysql_close($link);
?>
James
  • 486
  • 1
  • 9
  • 24
  • Would speculate that your upload code is broken, but without seeing it there's no way to tell. Does the web page actually fetch the uploaded audio file correctly and can that audio file be played using a desktop media player? – Femi Apr 29 '11 at 20:25
  • Hi since writing this i have found out that the file type cant be mp3, instead it is .amr and im unsure of how to play .amr files through a web browser as converting it is not an option – James Apr 30 '11 at 12:27
  • Have you considered the `audio` element? – alex Apr 30 '11 at 17:45
  • change `@mysql_query("select ...instance_id = $id");` to `@mysql_query("select ... instance_id = '$id'");` (put single quotes `'` around `$id`). This code is unsafe, even with **mysql_real_escape_string** – Johan May 02 '11 at 18:02

8 Answers8

3

Yes.

// Get the file contents from the database...

// Assuming a DB wrapper here.
$results = $db->query('SELECT `file_contents` FROM `mp3s` WHERE `id` = :id', array(':id' => $_GET['id'])->execute();
$fileContents = $results[0]['file_contents'];

// Send appropriate headers for content type and force download
header('Content-Type: mpeg/audio');
header('Content-Disposition: attachment; filename="BullsOnPowerade.mp3"')
echo $fileContents;
alex
  • 479,566
  • 201
  • 878
  • 984
  • I just get a blank page? edited my post to include the code im using – James May 01 '11 at 13:30
  • @James The DB wrapper I used was one I made up. Also, if you are getting a blank page, enable error reporting. – alex May 01 '11 at 13:33
1

So if you have an AMR file (I haven't tested this, so you'll have to try this out) you will need to extract the AMR audio and convert it to MP3. Unfortunately it appears to be rather rare in Java, but a (rather painful) Google search turned up this link: http://www.benmccann.com/blog/extracting-amr-audio-from-android-3gp-files/ which claims to demonstrate extracting the audio from a 3gpp video file, so they at least have an AMR parser. I would look at the underlying library they use, isobox4j (discussed at http://groups.google.com/group/android-developers/browse_thread/thread/245c9de4132c2ab0?fwc=1) and see if you can either:

  1. Extract the audio in the phone app, convert to mp3 and then upload the mp3 (probably the easiest option, frankly: keeps the server simple).
  2. Extract the audio on the server and convert to mp3 before streaming the mp3 to the web page (probably harder, frankly).

Good luck.

EDIT: or you could hope the client has an appropriate browser plugin: the broken quicktime mark indicates quicktime doesn't have a codec for it.

EDIT 2: You probably need to set a content-type for the plugin so it knows what to do. For AMR try adding

header("Content-type: audio/amr");

before the echo statement in your PHP and if that isn't enough add a mime type to your embed like this:

<object data="sound.php?id=110 width='391' height='298'"> <embed src='sound.php' type="audio/amr" width='391' height='298'></embed>'ERROR' </object>

See if that works.

EDIT: try this. Not sure if the $row['file'] is considered a string or an array, but the trim might do what you need.

<?php

header('Content-type: audio/amr');

$query = mysql_query("SELECT * FROM media WHERE media_id= '$idd'"); 

$row = mysql_fetch_array($query); 

echo trim($row['file']);
exit;
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
Femi
  • 64,273
  • 8
  • 118
  • 148
  • When i pull the file on to my desktop it plays through both real player and quicktime – James Apr 30 '11 at 14:55
  • So either your browser plugins aren't properly configured, or you need to use the right tag to get it to play. See http://support.apple.com/kb/ta26485 for QuickTime embed details – Femi Apr 30 '11 at 14:57
  • My object is fine with mp3 (although they play through windows media player) updated code to show embedding – James Apr 30 '11 at 16:16
  • I already have the header and i still get the quicktime logo with a question mark – James Apr 30 '11 at 17:52
  • Your quicktime plugin is not correctly installed (as I can play downloaded amr files in QuickTime with the embed string ``) or (more likely) you are introducing extra bytes when echoing the file that prevent quicktime from correctly decoding the file. Try putting `exit();` after the echo and ensuring there is absolutely no whitespace sent before the file data. You may want to actually download the file FROM THE URL that you are providing to Quicktime (don't copy it from the phone) and ensure that that saved file plays correctly. – Femi Apr 30 '11 at 21:17
  • is it possible to stream or is downloading the only way btw? – James Apr 30 '11 at 21:39
  • That's determined by the plugin: if you have it properly setup and the file data isn't being corrupted the player should do the right thing. When you download it directly from the database does it play on your desktop, or does it only play when you copy the file from the phone? – Femi May 01 '11 at 04:55
  • Odd. Well, not sure what else to say. If you have a test page available to look at then I could take a look, but if the file is valid and your plugin is working right then is SHOULD do the right thing. – Femi May 01 '11 at 16:17
  • no longer is the file playing when downloaded... i asked the question here http://stackoverflow.com/questions/5848996/download-amr-file-from-mysql-and-play – James May 01 '11 at 17:28
  • Ah, honestly, further debugging will need access to a URL that generates the file to be able to figure out WHAT exactly you are doing wrong. – Femi May 01 '11 at 17:31
  • How can i do this? I have uploaded the file and the database always thinks the file type is application/octet stream. but i set it to audio/amr (what it should be) – James May 01 '11 at 17:51
  • The database typically doesn't have a content-type setting: I expect your `header()` statement is being somehow superseded. You'll need to post a URL to the PHP file you are using so I can look at the headers and see what the content is. – Femi May 01 '11 at 18:37
  • http://uncertws.aston.ac.uk/dalejc/download.php?id=111 Should see you download an .amr file. And http://uncertws.aston.ac.uk/dalejc/item.php?id=85 Should is where i am trying to stream the .amr file – James May 01 '11 at 20:35
  • It is as I expected: you are printing out a line break character BEFORE you echo the file, so the file doesn't start with the right data which is why the playback fails. Ensure that the `echo` statement that you are using has NO OUTPUT before the echo, and by no output there must be NO whitespace and NO other echo or PHP instructions that output any value. – Femi May 01 '11 at 20:48
  • my php code is in my question, how do you recommend me changing it? – James May 01 '11 at 21:16
  • Try the latest edit to my answer. I expect you might be saving the extra character at the beginning of the table column. – Femi May 01 '11 at 22:32
  • anymore ideas femi? i think you're my only hope – James May 02 '11 at 05:12
  • Yeah, the trim didn't work: must not be a string. Try removing the trim and using print instead of echo. There's a similar question at http://stackoverflow.com/questions/1760754/how-to-display-an-image-from-a-mysql-blob – Femi May 02 '11 at 05:20
  • No adding print doesnt change anything. If you see http://uncertws.aston.ac.uk/dalejc/item.php?id=62 you can see the same code working with an mp3 (the headers are changed when its an mp3) also if i use the code i have added to my original question in google chrome the file downloads? but doesnt in any other browser – James May 02 '11 at 10:19
  • So I'm pretty baffled: after I download the file and chop off the extra newline at the beginning I can play the file if I make my QuickTime plugin pop up a separate QuickTime window, but it fails if I use : I couldn't get ANY combination of parameters to do the right thing. You may simply be out of luck there: perhaps the QuickTime browser plugin just can't do the right thing with amr over http. – Femi May 02 '11 at 14:17
  • You CAN download the file and play it? please explain how you did that? - Also thank you very much for your continued help – James May 02 '11 at 14:43
  • well, I downloaded the file to a local web server with `wget -S -O apr2.amr "http://uncertws.aston.ac.uk/dalejc/download.php?id=111"`. Then I opened up the file in a text editor and removed the very first character in the file (which is a newline that shouldn't be there). I modified my Firefox settings for audio/amr files to use the quicktime app and then when I visit http://localnetworkserver/apr2.amr Quicktime opens up in a separate window and plays the file. Somehow when you inserted into the database you added an extra newline at the front of the file, and I had to chop that off. – Femi May 02 '11 at 14:49
  • ok so the character that shouldnt be there, would you say its fixable when uploading? added to my original question is the code for my android app to upload to the database – James May 02 '11 at 15:58
  • i see what you mean, i opened in notepad++ and see the line you are talking about – James May 02 '11 at 16:23
  • try wrapping a `trim()` around the `addslashes()` call. I've really never used BLOBs in PHP (only in Java, where its a regular InputStream so there's no confusion about whether it is a string or a byte sequence). – Femi May 02 '11 at 16:27
  • ive done it!!! thank you do much for the help. I had a blank line at the start of my php script to download. – James May 02 '11 at 17:47
  • Ah, good. That was one of my initial comments: **Try putting exit(); after the echo and ensuring there is absolutely no whitespace sent before the file data.** but glad to hear this finally worked out. – Femi May 02 '11 at 17:53
1

If HTML5 is not an option, for inline playback, the best option supported out of the box by the majority of current browsers will be flash based. Flash still requires a plugin, but it's much more widely installed than Quicktime AFAIK.

The Google mp3 player that Tomasz suggested is a good choice. There are many more out there though. See for example http://www.webdesignbooth.com/10-easy-to-implement-flash-based-mp3-players-for-your-website/

Thilo
  • 17,565
  • 5
  • 68
  • 84
0

This is a really really broad question, but here are some starter tips:

  • Look at the apache httpcomponents project for a good HTTP library, including the ability to do multi-part MIME attachments and POST requests
  • Take a look at the PHP docs for handling file uploads
  • If you don't want your app to lock up during uploads, I would encourage you to look at the android AsyncTask pattern and supporting classes so that your UI thread does not block during the uploads
  • There are any number of ways to play an audio file in a web page (Flash being the most prevalent probably)
Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
0

Yes, the audio file is just a binary file and can be saved an a blob data field. Then recalled from a query. Then play it any way you want.You may be better off saving the audio file to local storage and keeping track of where you put it in the database.

Jim
  • 809
  • 1
  • 7
  • 18
0

You could use Flash on the web page. On 2.3 you could use the audio tag. See HTML5 <audio> tag on Android .

Community
  • 1
  • 1
Ed Burnette
  • 1,198
  • 8
  • 13
  • since writing this i have found out the file type that is recorded is .amr and when i try to play this with a content type of audio/amr i just get a quicktime player appear with a question mark in it. – James Apr 30 '11 at 12:24
  • There are different kinds of amr files. In any case you'd be better off with mp3 or ogg audio. Mp3 is good for long clips, ogg for short and low latency clips. – Ed Burnette May 01 '11 at 18:51
  • Yes but the android sound recorder records in .amr files. And i cant use a converter as the plan is to have them uploaded straight from the phone to the database then out through the website – James May 01 '11 at 20:40
0

easiest method is to use nowadays is HTML5 tag..
html5 audio
<audio src="horse.ogg" controls="controls"> Your browser does not support the audio element. </audio>

using google mp3 player - replace mp3_file_url with the actual url of the file... although im not sure if it runs .amr files
<embed type="application/x-shockwave-flash" flashvars="audioUrl=MP3_FILE_URL" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="400" height="27" quality="best"></embed>

EDIT:

quicktime - change WIDTH and HEIGHT in both cases.. and choose false/true in the params you want <OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="_WIDTH_" height="_HEIGHT_"codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
<param name='src' value="apr.amr">
<param name='autoplay' value="true/false">
<param name='controller' value="true/false">
<param name='loop' value="true/false">
<EMBED src="apr.amr" width="_WIDTH_" height="_HEIGHT_" autoplay="true" controller="true" loop="true" pluginspage='http://www.apple.com/quicktime/download/'>
</EMBED>
</OBJECT>

Tomasz Golinski
  • 743
  • 5
  • 25
  • These are excellent methods but aren't supported by all browsers... Instead, Quicktime is the best solution in my opinion. – matteodv Apr 30 '11 at 18:08
  • No audio playback is currently supported by *all* browsers. Quicktime requires a plugin, just as Flash does, however, the latter is much more widely installed. – Thilo Apr 30 '11 at 18:20
  • quicktime aren't supported by all browsers either.. you have to download plugins for almost every browser to make it work. – Tomasz Golinski Apr 30 '11 at 18:30
  • Hi thanks for the answer, concerning the quicktime response. When i use the code above i get the same thing happen in firefox (quicktime logo with a question mark) and in chrome is downloads the file – James Apr 30 '11 at 22:41
0

If you want to embed a video on your webpage, you can read this support article on Apple's website and this is a list of tag attributes... Hope this can help! :)

matteodv
  • 3,992
  • 5
  • 39
  • 73