简体   繁体   English

rails + WebRTC录音+ Carrierwave + fog + S3 + Ajax错误

[英]rails + WebRTC audio recording + Carrierwave + fog + S3 + Ajax error

I want to collect HTML5 audio recordings from a user and store them in S3. 我想从用户那里收集HTML5录音并将它们存储在S3中。 I am using a javasscript resource for in-browser audio and video recording with WebRTC called MediaStreamRecorder.js to collect the audio recording. 我正在使用javasscript资源进行浏览器内的音频和视频录制,使用名为MediaStreamRecorder.js的 WebRTC来收集录音。 I have added Carrierwave and Fog, and verified that I can successfully upload audio files to S3. 我添加了Carrierwave和Fog,并验证我可以成功将音频文件上传到S3。 I have also successfully used MediaStreamRecorder.js to collect the audio Blob and play it back in an audio tag. 我还成功地使用MediaStreamRecorder.js来收集音频Blob并在音频标签中播放。 My original idea was to add the blob URL directly as value of a hidden form input and get the audio to the controller and Carrierwave through a form submission, the same way you can use "remote_file_url" to submit a link to a remote file rather than uploading a local file. 我最初的想法是直接添加blob URL作为隐藏表单输入的值,并通过表单提交将音频提供给控制器和Carrierwave,就像使用“remote_file_url”提交链接到远程文件而不是上传本地文件。

That failed. 那失败了。 Evidently, blob URLs cannot be handled this way. 显然,blob URL无法以这种方式处理。

I found this blog post explaining how I can submit a file directly to Carrierwave via Javascript. 我发现这篇博客文章解释了我如何通过Javascript直接向Carrierwave提交文件。 I tried to implement this but failed. 我试图实现这个但失败了。 I am using Chrome. 我正在使用Chrome。

I have an uploader called "Recording": 我有一个名为“Recording”的上传器:

class RecordingUploader < CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

I have a model "Recitation": 我有一个模型“背诵”:

class Recitation < ActiveRecord::Base
    belongs_to :lesson
    mount_uploader :recording, RecordingUploader
end

The routes reflect that Recitation is nested in a model called Lesson: 路线反映Recitation嵌套在名为Lesson的模型中:

resources :lessons do
    resources :parts
    resources :recitations
end

My new and create method in the Recitation controller: 我在Recitation控制器中的new和create方法:

def new
    @lesson = Lesson.find(params[:lesson_id])
    @recitation = Recitation.new
end

def create
    @lesson = Lesson.find(params[:lesson_id])
    @recitation = @lesson.recitations.new(recitation_params)
    if @recitation.save
      redirect_to thanks_path
    else
      flash[:error] = "There was a problem saving your recording."
      render :new
    end
end

And finally my attempt to pass the blob to the controller via AJAX. 最后我尝试通过AJAX将blob传递给控制器​​。

function onMediaSuccess(stream) {  
    mediaRecorder = new MediaStreamRecorder(stream);
    mediaRecorder.mimeType = 'audio/ogg';
    mediaRecorder.ondataavailable = function(blob) {  
        var formData = new FormData();
        formData.append('recitation[recording]', blob);
        $.ajax({
            url: "/lessons/#{@lesson.id}/recitations",
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            type: 'PUT'
        });
    };
}

In the JS console I get a "404 (Not Found)" error. 在JS控制台中,我收到“404(Not Found)”错误。 The trouble seems to be in some AJAX related lines in jquery.js, but I am guessing. 问题似乎是jquery.js中的一些AJAX相关行,但我猜。 Would appreciate any insight. 非常感谢任何见解。

I think your issue is related to the url used in the AJAX lines: 我认为您的问题与AJAX行中使用的url有关:

 $.ajax({
        url: "/lessons/#{@lesson.id}/recitations",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'PUT'
    });

You need to specify the the id of the recitation that you're trying to update. 您需要指定您尝试更新的朗诵的ID。 Try the following to see if it works: 请尝试以下操作以查看其是否有效:

$.ajax({
    url: "/lessons/#{@lesson.id}/recitations/#{@recitation.id}",
    data: formData,
    cache: false,
    contentType: false,
    processData: false,
    type: 'PUT'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM