android - 如何在android中发送smack中的音频文件

标签 android audio smack asmack

如何在 android 中像 whatsapp 一样发送音频文件 最好的方法是什么?

还有如何发送视频和照片? 正在遵循最好的方式

在android asmack中发送文件的最佳方式是什么

public void send()
{

configureProviderManager(connection);

FileTransferNegotiator.setServiceEnabled(connection, true);
FileTransferManager manager = new FileTransferManager(connection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("xxxxx@jabber.ccc.de/Smack");

File file = new File("/sdcard/DCIM/Camera/1385869353956.jpg");
try {
    Log.d("file sending",file.getAbsolutePath()+" "+file.getName());
    configureProviderManager(connection);
   transfer.sendFile(file, "test_file");
} catch (XMPPException e) {
   e.printStackTrace();
}

while(!transfer.isDone()) {
       if(transfer.getStatus().equals(Status.error)) {
          System.out.println("ERROR!!! " + transfer.getError());
       } else if (transfer.getStatus().equals(Status.cancelled)
                        || transfer.getStatus().equals(Status.refused)) {
          System.out.println("Cancelled!!! " + transfer.getError());
       }
       try {
          Thread.sleep(1000L);
       } catch (InterruptedException e) {
          e.printStackTrace();
       }
    }

    if(transfer.getStatus().equals(Status.refused))
             System.out.println("refused  " + transfer.getError());
    else if( transfer.getStatus().equals(Status.error))
         System.out.println(" error " + transfer.getError());
    else if(transfer.getStatus().equals(Status.cancelled))
       System.out.println(" cancelled  " + transfer.getError());
    else
       System.out.println("Success");


}

最佳答案

您可以根据您的要求进行更改。

public class MyFileTransfer  extends SmackTest<XMPPTCPConnection> {

    public static void main(String args[]) throws Exception {
        SmackTest<XMPPTCPConnection> test = new MyFileTransfer();
        test.runTest();
    }

    private static final byte[] dataToSend = StringUtils.randomString(1024 * 4 * 3).getBytes();
    private static byte[] dataReceived;

    @Override
    protected void runTestSubclass() throws SmackException, IOException,
            XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
        FileTransferNegotiator.IBB_ONLY = true;
        XMPPTCPConnectionConfiguration.Builder conf = XMPPTCPConnectionConfiguration.builder();
        conf.setServiceName(SERV);
        conf.setUsernameAndPassword(USER, PASS);
        conf.setSecurityMode(SecurityMode.disabled);
        conf.setCompressionEnabled(true);
        TLSUtils.acceptAllCertificates(conf);
        conf.setResource("sender");

        connection = new XMPPTCPConnection(conf.build());
        connection.connect();
        connection.login();

        conf.setResource("receiver");
        XMPPTCPConnection connection2 = new XMPPTCPConnection(conf.build());
        connection2.connect();
        connection2.login();

        FileTransferManager ftm1 = FileTransferManager.getInstanceFor(connection);
        FileTransferManager ftm2 = FileTransferManager.getInstanceFor(connection2);

        ftm2.addFileTransferListener(new FileTransferListener() {
            @Override
            public void fileTransferRequest(FileTransferRequest request) {
                IncomingFileTransfer ift = request.accept();
                try {
                    InputStream is = ift.recieveFile();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    int nRead;
                    byte[] buf = new byte[1024];
                    while ((nRead = is.read(buf,  0, buf.length)) != -1) {
                        os.write(buf, 0, nRead);
                    }
                    os.flush();
                    dataReceived = os.toByteArray();
                } catch (SmackException | IOException | XMPPErrorException e) {
                    e.printStackTrace();
                }
                if (Arrays.equals(dataToSend, dataReceived)) {
                    System.out.println("Received data matches send data. \\o/");
                } else {
                    System.err.println("Recieved data DOES NOT match send data. :(");
                }
            }
        });

        OutgoingFileTransfer oft = ftm1.createOutgoingFileTransfer(XmppStringUtils.completeJidFrom(USER, SERV, "receiver"));
        oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting");
        outerloop: while (!oft.isDone()) {
            switch (oft.getStatus()) {
                case error:
                    System.out.println("Filetransfer error: " + oft.getError());
                    break outerloop;
                default:
                    System.out.println("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress());
                    break;
            }
            Thread.sleep(1000);
        }

        connection.disconnect();
        connection2.disconnect();
        Thread.sleep(1000);
    }
}

关于android - 如何在android中发送smack中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178471/

相关文章:

java - 无需重新登录 asmack 即可更改 Activity

java - 在不同的线程中发出 Volley 请求

android - 包 com.google.api.client.extensions.android.http 不存在

java - 使用 StringBuilder 格式化多个 EditText,而不是多次复制代码

android - Android:如何合成钢琴声音?

javascript - Js 未捕获类型错误 : Cannot read property of undefined but still working

android - 使用 aSmack、Eclipse 和增强的调试器调试 XMPP Android 客户端。

java - TabHost 单击时启动外部浏览器

regex - Grep 获取某个范围内的数字 - 检查 WAV 质量

java - Android - Smack 存在和 "is typing"功能不工作