在Chrome应用中,视频文件未保存其原始数据/尺寸

时间:2015-08-18 06:28:35

标签: google-chrome google-chrome-extension google-chrome-app

In appCtrl.js, for saving video file -

$('#save_file').click(function(e) {

  var config = {type: 'saveFile', suggestedName: chosenEntry.name};
  chrome.fileSystem.chooseEntry(config, function(writableEntry) {
      //blob content is the DataUrl 
      var blob = new Blob([$scope.blobContent], {type: 'video/mp4'});
          $scope.writeFileEntry(writableEntry, blob, function(e) {
             console.log('Write complete :)');
          });
      });
   });

   $scope.writeFileEntry = function(writableEntry, opt_blob, callback) {
        if (!writableEntry) {
           console.log('Nothing selected.');
            return;
        }

        writableEntry.createWriter(function(writer) {

          writer.onerror = $scope.errorHandler;
          writer.onwriteend = callback;

  // If we have data, write it to the file. Otherwise, just use the file we
            // loaded.
                if (opt_blob) {
                  writer.truncate(opt_blob.size);
                  $scope.waitForIO(writer, function() {
                    writer.seek(0);
                    writer.write(opt_blob);
                  });
                } 
                else {
                    chosenEntry.file(function(file) {
                        writer.truncate(file.fileSize);
                        waitForIO(writer, function() {
                          writer.seek(0);
                          writer.write(file);
                        });
                    });
                }
            }, $scope.errorHandler);
        }

        $scope.waitForIO = function(writer, callback) {
          // set a watchdog to avoid eventual locking:
          var start = Date.now();
          // wait for a few seconds
          var reentrant = function() {
            if (writer.readyState===writer.WRITING && Date.now()-start<4000)     {
              setTimeout(reentrant, 100);
              return;
            }
            if (writer.readyState===writer.WRITING) {
              console.error("Write operation taking too long, aborting!"+
                " (current writer readyState is "+writer.readyState+")");
              writer.abort();
            } 
            else {
              callback();
            }
          };
          setTimeout(reentrant, 100);
        };

在上面的代码中保存了视频文件但是当我试图在Window Media Player或VLC播放器中播放该保存的文件时,它提示我,因为Window媒体播放器无法播放该文件。播放器可能不支持该文件类型或者可能不支持用于压缩文件的编解码器。

作为我的第一个Chrome应用程序,请你指导我哪里出错了。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

将方法更改为像这样存储blob。

function dataURItoBlob(dataURI, callback) {
            // convert base64 to raw binary data held in a string
            // doesn't handle URLEncoded DataURIs
            var byteString = atob(dataURI.split(',')[1]);

            // separate out the mime component
            var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

            // write the bytes of the string to an ArrayBuffer
            var ab = new ArrayBuffer(byteString.length);
            var ia = new Uint8Array(ab);
            for (var i = 0; i < byteString.length; i++) {
                ia[i] = byteString.charCodeAt(i);
            }

            return new Blob([ab], {type: 'video/mp4'});
        };

处理点击。

 $('#save_file').click(function(e) {
                var config = {type: 'saveFile', suggestedName: chosenEntry.name};
                chrome.fileSystem.chooseEntry(config, function(writableEntry) {
                    var blob = dataURItoBlob($scope.blobContent);
                    $scope.writeFileEntry(writableEntry, blob, function(e) {
                      console.log('Write complete :)');
                    });
                });
            });