dongyong1942 2015-08-01 22:53
浏览 86

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

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!

  • 写回答

1条回答 默认 最新

  • duannaiying9662 2015-08-03 16:12
    关注

    Turns out my code wasn't the problem, it works perfectly fine! So feel free to use it. Turns out my problem was different. See this post: Unable to play MP4 video file from mainBundle

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度