将录制的音频从iOS应用保存到mySQL数据库

时间:2015-08-01 22:53:26

标签: php ios mysql audio

我正在开发一个项目,通过PHP和AFNetworking将数据从iOS应用程序上传到mySQL数据库。我有成功记录并将音频文件保存到服务器上的文件夹的代码,并保存数据库中的相对路径。作为测试,我从他们保存的文件夹下载它们,我可以在我的计算机上播放它们,但是当我尝试在工作应用程序中播放声音时它根本不会播放。我也尝试将文件保存为不同的音频格式,但我不知道问题是什么。我不确定是否要处理我的代码。以下是我的PHP代码:

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";
  }

这是AVPlayer及其设置:

    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];

这是上传代码:

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);
    }];
    }

如果您有任何建议或者您知道问题是什么,请告诉我,因为我不知道。提前致谢!

1 个答案:

答案 0 :(得分:0)

原来我的代码不是问题,它工作得很好!所以随意使用它。原来我的问题不同了。 看这篇文章: Unable to play MP4 video file from mainBundle