Failed to execute 'createObjectURL' on 'URL':

后端 未结 8 1279
暗喜
暗喜 2020-11-27 10:52

Display Below error in Safari.

Failed to execute \'createObjectURL\' on \'URL\': No function was found that matched the signature provided.

相关标签:
8条回答
  • 2020-11-27 11:20

    UPDATE

    Consider avoiding createObjectURL() method, while browsers are disabling support for it. Just attach MediaStream object directly to the srcObject property of HTMLMediaElement e.g. <video> element.

    const mediaStream = new MediaStream();
    const video = document.getElementById('video-player');
    video.srcObject = mediaStream;
    

    However, if you need to work with MediaSource, Blob or File, you have to create a URL with URL.createObjectURL() and assign it to HTMLMediaElement.src.

    Read more details here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject


    Older Answer

    I experienced same error, when I passed to createObjectURL raw data:

    window.URL.createObjectURL(data)
    

    It has to be Blob, File or MediaSource object, not data itself. This worked for me:

    var binaryData = [];
    binaryData.push(data);
    window.URL.createObjectURL(new Blob(binaryData, {type: "application/zip"}))
    

    Check also the MDN for more info: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL

    0 讨论(0)
  • 2020-11-27 11:22

    I had the same error for the MediaStream. The solution is set a stream to the srcObject.

    From the docs:

    Important: If you still have code that relies on createObjectURL() to attach streams to media elements, you need to update your code to simply set srcObject to the MediaStream directly.

    0 讨论(0)
  • 2020-11-27 11:25

    I fixed it downloading the latest version from GgitHub GitHub url

    0 讨论(0)
  • 2020-11-27 11:32

    If you are using ajax, it is possible to add the options xhrFields: { responseType: 'blob' }:

    $.ajax({
      url: 'yourURL',
      type: 'POST',
      data: yourData,
      xhrFields: { responseType: 'blob' },
      success: function (data, textStatus, jqXHR) {
        let src = window.URL.createObjectURL(data);
      }
    });
    
    0 讨论(0)
  • 2020-11-27 11:35

    My code was broken because I was using a deprecated technique. It used to be this:

    video.src = window.URL.createObjectURL(localMediaStream);
    video.play();
    

    Then I replaced that with this:

    video.srcObject = localMediaStream;
    video.play();
    

    That worked beautifully.

    EDIT: Recently localMediaStream has been deprecated and replaced with MediaStream. The latest code looks like this:

    video.srcObject = new MediaStream();
    

    References:

    1. Deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
    2. Modern deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
    3. Modern technique: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream
    0 讨论(0)
  • 2020-11-27 11:36

    This error is caused because the function createObjectURL is deprecated for Google Chrome

    I changed this:

    video.src=vendorUrl.createObjectURL(stream);
    video.play();
    

    to this:

    video.srcObject=stream;
    video.play();
    

    This worked for me.

    0 讨论(0)
提交回复
热议问题