I am working on a project that uploads data to a mySQL database from an iOS app through PHP and AFNetworking. I have code that successfully records and saves an audio file to a folder on my server and saves the relative path in the database. As a test, I download them from the folder they were saved to and I can play them on my computer, however when I try to play a sound in a working app it does not play back at all. I have also tried saving the files as different audio formats but I don't know what the problem is. I'm not sure if it's to do with my code or not. The following is my PHP code:
if ((($_FILES["audio"]["type"] == "audio/mp3")
|| ($_FILES["audio"]["type"] == "audio/mp4")
|| ($_FILES["audio"]["type"] == "audio/m4a")
|| ($_FILES["audio"]["type"] == "audio/wav")
|| ($_FILES["audio"]["type"] == "audio/caf")
|| ($_FILES["audio"]["type"] == "audio/mpeg3")
|| ($_FILES["audio"]["type"] == "audio/mpeg"))
&& ($_FILES["audio"]["size"] < 1000000))
{
if ($_FILES["audio"]["error"] > 0)
{
echo "Return Code: " . $_FILES["audio"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["audio"]["name"] . "<br />";
echo "Type: " . $_FILES["audio"]["type"] . "<br />";
echo "Size: " . ($_FILES["audio"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["audio"]["tmp_name"] . "<br />";
if (file_exists("audio/" . $_FILES["audio"]["name"]))
{
echo $_FILES["audio"]["name"] . " already exists. ";
}
else
{
$audio_name = $_FILES["audio"]["name"];
$audio_path = "audio/$audio_name";
move_uploaded_file($_FILES["audio"]["tmp_name"], $audio_path);
$sql = "INSERT INTO audiotable (audiopath)
VALUES ('$audio_path')";
$result = mysql_query($sql);
if($result)
{
echo 'Upload successfully';
}
else
{
echo 'ERROR: Upload Failed';
}
}
}
}
else
{
echo "Invalid file";
}
This is the AVPlayer and its settings:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],@"MyAudioMemo.caf", nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
And this is the upload code:
NSData *data = [NSData dataWithContentsOfURL:recorder.url];
int randomAudio = arc4random() % 9999999;
NSString *audioName=[NSString stringWithFormat:@"%d-audio.caf",randomAudio];
[manager POST:@"http://mywebsite.com/upload.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:audioData name:@"audio" fileName:audioName mimeType:@"audio/caf"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
Please let me know if you have any suggestions or if you know what the problem is, as I have no clue. Thanks in advance!