开发者

Uploading an audio recording to mysql and playing in website

开发者 https://www.devze.com 2023-03-01 14:48 出处:网络
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 a

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);
?>


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;


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;


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/


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)


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.


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


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! :)


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>

0

精彩评论

暂无评论...
验证码 换一张
取 消