0

我正在尝试通过运行从视频文件中提取元ffprobe数据nativeProcess。下面的代码就像一个文件一样工作,但在尝试循环一系列文件时会导致错误。

我知道问题的原因是 Air 试图nativeProcess在旧的完成之前开始一个新的。我知道这与听音乐有关air.NativeProcessExitEvent.EXIT。我只是无法让它工作。任何建议将不胜感激。

function fileOpen(){
    
  var directory = air.File.userDirectory;
  
  try
  {
      directory.browseForDirectory("Select Directory");
      directory.addEventListener(air.Event.SELECT, directorySelected);
  }
  catch (error)
  {
      air.trace("Failed:", error.message)
  }
  
  function directorySelected(event) 
  {
      directory = event.target ;
      var files = directory.getDirectoryListing();
      for(i=0; i < files.length; i++){
        getMetadata(files[0].nativePath)
                    //wait here for nativeProcess to finish
        }
      }
  } 

function getMetadata(filePathIn){
    
         
        if(air.NativeProcess.isSupported)
        {
            
        }
        else
        {
            air.trace("NativeProcess not supported.");
        }
    
        fileIn = filePathIn.toString()

        var nativeProcessStartupInfo = new air.NativeProcessStartupInfo();
        var file = air.File.applicationStorageDirectory.resolvePath("ffprobe");
        nativeProcessStartupInfo.executable = file;

        args = new air.Vector["<String>"]();
        args.push("-sexagesimal","-show_format","-loglevel","quiet","-show_streams","-print_format","json",filePathIn)
        nativeProcessStartupInfo.arguments = args;

        process = new air.NativeProcess();
        process.start(nativeProcessStartupInfo); 
        process.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
        process.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
        process.addEventListener(air.NativeProcessExitEvent.EXIT, onExit);
        process.addEventListener(air.IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
        process.addEventListener(air.IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
        
        
    }

    function onOutputData()
    {
      var fileMetadataJSON = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
      air.trace(fileMetadataJSON)
    }
    
    function onErrorData(event)
    {
        air.trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable)); 
    }
    
    function onExit(event)
    {
        air.trace("Process exited with ", event.exitCode);
    
    }
    
    function onIOError(event)
    {
         air.trace(event.toString());
    }
    
4

1 回答 1

0

这里有一个大纲,应该让你知道该怎么做:

  • 在您的directorySelected()方法中,将局部变量files(数组)提升为成员变量。这将使选择的文件列表可用,以便其他方法可以访问它。
  • 删除方法中的for循环directorySelected()。与其运行循环并尝试在此处运行本机进程,不如调用一个新方法,让我们调用它dequeueFileAndRunNativeProcess()
  • 这个新dequeueFileAndRunNativeProcess()方法应该检查数组的长度files,如果大于0,它应该是数组pop()中的第一个文件files并在其上执行本机进程。
  • 在您的onExit()方法中,再次调用该dequeueFileAndRunNativeProcess()方法。

这里的想法是将运行本机进程的代码移动到一个可以从两个地方触发的方法中:一次在用户浏览完文件后立即执行,然后在本机进程完成工作后再次执行。当files阵列中没有更多文件时,该过程结束。

于 2012-12-28T19:34:01.373 回答