Appcelerator - 发送部分大视频

时间:2016-02-26 08:47:39

标签: video appcelerator appcelerator-titanium

我希望通过我的应用发送大型视频。我的想法是发送部分视频,因为如果连接失败,那么我可以继续发送其余的部分。我正在使用300MB视频。

选项1

我可以使用Ti.BLOB对象吗?在Android中,我遇到了使用Blob对象处理视频的问题。 - >我试过这个,这条消息出现了:

*** ASL MESSAGE SIZE (8653314 bytes) EXCEEDED MAXIMIMUM SIZE (2048000 bytes) ***

所以这个选项不起作用。

选项2

我找到了appcelerator的zip / unzip模块,但是我不能将zip分成几部分。

最好的方法是什么?有什么想法吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

使用套接字

在Titanium中尝试使用FTP二进制数据文件上传器

https://gist.github.com/abada/d762f2e1151903505be9

// FTP binary files via FTP using Sockets. 

var ftp_host = '1.2.3.4'; // the IP address of your FTP server
var ftp_port = 21;
var ftp_username = 'username';
var ftp_password = 'yourftppassword';
var ftp_home = '/home/folder'; // Your FTP home folder. Or where ever you want to 'CWD' to.

var socket_control;   // main control socket
var socket_data;      // for read/write
var open_data = false;


function connectFTP(){
  Ti.API.info('Connecting FTP Socket.. ');
  var socket = Ti.Network.Socket.createTCP({
      host: ftp_host, 
      port: ftp_port,
      connected: function (e) {
          Ti.API.info('Socket opened!');
          socket_control = e.socket;
          Ti.Stream.pump(e.socket, readCallback, 1024, true);

      },
          error: function (e) {
          Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
      },
  });
  socket.connect();

}

function readCallback(e) {
    if (e.bytesProcessed == -1)
    {
        // Error / EOF on socket. Do any cleanup here.
        console.log('readCallback error');
    }
    try {
        if(e.buffer) {
            var responseCode = e.buffer.toString().substr(0,3);
            // http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

            var received = e.buffer.toString();
            Ti.API.info('Received: ' + received);
            Ti.API.info('Status: ' + responseCode);
            // Ti.API.info(e);
            switch(responseCode) {
              case "220":
                Ti.API.info("FTP: Logging in");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'USER ' + ftp_username + '\n'
                }), function(_e){
                  // console.log('callback after USER...');
                  // console.log(_e);
                });
                break;
              case "331":
                Ti.API.info("FTP: Sending credentials");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'PASS ' + ftp_password + '\n'
                }), function(_e){
                  // console.log('callback after PASS...');
                  // console.log(_e);
                });
                break;
              case "230":
                Ti.API.info("FTP: Log in successful");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'CWD ' + ftp_home + '\n'
                }), function(_e){
                  // console.log('callback after login CWD ...');
                  // console.log(_e);
                });
                break;
              case "225":
                Ti.API.info("FTP: Data Connection Opened");

                break;
              case "226":
                Ti.API.info("FTP: Closing Data Connection");

                break;        
              case "257":
                Ti.API.info("FTP: Pathname created...");

                break;
              case "221":
                Ti.API.info("FTP: Disconnected");
                break;
              case "227":
                Ti.API.info("FTP: Passive mode entered : open_data:"+open_data);

                if(open_data){ // Trap this when we want it and not after other 'PASV' commands
                  console.log('preflight STOR PASV response: ');
                  // Parse the response.. 
                  var msg = e.buffer.toString();
                  msg = msg.split('(')[1].split(')')[0].split(',');
                  if(msg.length==6){
                    // looks ok.. 
                    var m6 = msg.pop()*1; // forces int
                    var m5 = msg.pop()*1;
                    var remote_ip = msg.join('.');
                    console.log('remote ip : '+remote_ip);
                    var remote_port = Math.round((m5 * 256) + m6); // Magic port calculation! 
                    console.log('remote port : '+remote_port);

                    // OK.. now set up the data socket.. 
                    var socket = Ti.Network.Socket.createTCP({
                        host: remote_ip, 
                        port: remote_port,
                        connected: function (e) {
                            Ti.API.info('DATA Socket opened!');
                            // saving this 
                            socket_data = e.socket;
                            Ti.Stream.pump(e.socket, readDataCallback, 1024, true);
                            // set binary transfer mode TYPE I
                            Ti.Stream.write(socket_control, Ti.createBuffer({
                              value: 'TYPE I\n'
                            }), function(_e){
                              console.log('callback after setting TYPE I ...');
                              console.log(_e);
                              if(_e.success){
                                  // Binary transfer mode is now ready
                                  // Tell the server the name of the file
                                  // this does not have to be the actual name of the local file
                                  var remote_filename = 'test.jpg';
                                  console.log('send STOR '+remote_filename+' to control socket...');
                                  // now send the STOR to the control socket.. hopefully get a 150 back...
                                  Ti.Stream.write(socket_control, Ti.createBuffer({
                                    value: 'STOR '+remote_filename+'\n'
                                  }), function(_e){
                                    //console.log('callback after pre-STOR PASV ...');
                                    //console.log(_e);
                                  });
                              }
                            });                            
                        },
                            error: function (e) {
                            Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
                        },
                    });
                    console.log('connecting DATA socket');
                    socket.connect();
                  }
                }
                break;
              case "150":

                console.log('DATA connection initiated on control socket!... writing file contents to socket_data');
                // by default the data connection is in ASCII mode. 
                // so, we called 'TYPE I' beforehand to set it to BINARY mode.
                // Now get the file we want to send... (from wherever is is). Need to be a
                var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'test.jpg');
                var file_data = file.read(); // verbose for clarity
                var file_buffer = Ti.createBuffer(); // create empty buffer
                // Create a file stream object with the file_data
                var file_stream = Ti.Stream.createStream({ source : file_data, mode : Titanium.Stream.MODE_READ});
                // Add it to the buffer
                var content_size = file_buffer.append(Ti.Stream.readAll(file_stream));
                console.log('File Size : ' + content_size ); // append returns the bytes appended

                console.log('WRITE FILE to socket now .. ');
                // write to socket_data :
                Ti.Stream.write(socket_data, file_buffer, function(_e){
                  console.log('callback after socket_data write.. ');
                  console.log(_e);
                  if(_e.success){
                    // WOO HOOOOOO!!!!!! It worked!! 
                    console.log('FTP : file uploaded OK!  closing connection..');
                    socket_data.close();
                    // Nullify stuff
                    file_buffer = null;
                    file_stream = null;
                    file_data = null;
                    file = null;

                    open_data = false; // Our PASV trap

                  } else {
                    console.log('FTP ERROR ');
                  }
                });
                break;
              case "250":
                Ti.API.info("FTP: Changed active directory to " + ftp_home);
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'PASV\n'
                }), function(_e){
                  console.log('callback after PASV ...');
                  console.log(_e);
                });
                break;
              case "530":
                Ti.API.info("FTP: Login failed, disconnecting");
                break;
              default:
                Ti.API.info("FTP: " + e.buffer.toString());
                break;
            }

        } else {
            Ti.API.error('Error: read callback called with no buffer!');
        }
    } catch (ex) {
        Ti.API.error(ex);
    }
}

function readDataCallback(e) {
    if (e.bytesProcessed == -1)
    {
        // Error / EOF on socket. Do any cleanup here.
        console.log('readDataCallback error');
    }
    try {
        if(e.buffer) {
            var responseCode = e.buffer.toString().substr(0,3);

            var received = e.buffer.toString();
            Ti.API.info('DATA Received: ' + received);
            Ti.API.info('DATA Status: ' + responseCode);
            Ti.API.info(e);

        } else {
            Ti.API.error('Error: readDataCallback called with no buffer!');
        }
    } catch (ex) {
        Ti.API.error(ex);
    }
}

// Set up some UI and buttons 

var win = Ti.UI.createWindow({ backgroundColor: "#fff" });
var buttonConnect = Ti.UI.createButton({ title: "Connect", width: 280, height: 40, top: 40, left: 20 });
var buttonWrite = Ti.UI.createButton({ title: "Write File", width: 280, height: 40, top: 100, left: 20 });
var buttonDisconnect = Ti.UI.createButton({ title: "Disconnect", width: 280, height: 40, top: 160, left: 20 });

buttonConnect.addEventListener("click", function(_event) {
  // Set up connection
    connectFTP();
});

buttonWrite.addEventListener("click", function(_event) {

  open_data = true;// will open second datasocket after the PASV before the STOR 
  // Send the initial PASV command, so we can get the data host:port to use
  Ti.Stream.write(socket_control, Ti.createBuffer({
    value: 'PASV\n'
  }), function(_e){
    //console.log(_e);
  });


});

buttonDisconnect.addEventListener("click", function(_event) {



});

win.add(buttonConnect);
win.add(buttonWrite);
win.add(buttonDisconnect);
win.open();

答案 1 :(得分:1)

使用Ti.Buffer和Ti.Stream对上传进行分块。我在这里写了博客:

http://lowb1rd.github.io/002-chunked-uploads-with-titanium.html