Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

javascript - Safari: unable to dynamically load video from blob url

i'm trying to implement a dynamic loading for an html5 video tag.

when a user picks a video file via the <input type="file"> element, i want to load it dynamically to a <video> element, and append it to body.

the following code works on Chrome but not on Safari:

function load_video(file) { // this came from <input type="file"> change event
    var reader = new FileReader();
    reader.onload = function(event) {
        var blob = new Blob([event.target.result]);
        window.URL = window.URL || window.webkitURL;
        var blobURL = window.URL.createObjectURL(blob);
        $('body').append('<video controls width="320" src="' + blobURL + '" onloadedmetadata="alert('loaded meta data!')"></video>');
    }
}

now, if i'll replace src="' + blobURL + '" with a local filepath, say- /media/videos/vid1.mp4, the video loads in Safari as well, but I need it to load the video from blobURL.

any suggestions?

thanks alot.

UPDATE:

as Rod says, unfortunately it's a known bug in Safari (not supported by it's media backend).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know if the solution applies to video as well as audio, but I had the same issue with Safari audio and I discovered that the solution was to specify the content type when constructing the blob:

var blob = new Blob([arrayBuffer], {type: 'audio/mpeg'});
url = webkitURL.createObjectURL(blob);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...