如何在android中发送音频文件

Posted

技术标签:

【中文标题】如何在android中发送音频文件【英文标题】:how to send audio files in smack in android 【发布时间】:2014-04-20 04:32:02 【问题描述】:

如何在 android 中发送audio files 就像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");



【问题讨论】:

你能发送文件吗? 【参考方案1】:

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

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中录制后将音频文件发送到服务器

如何在android中录制音频m4a格式的音频文件?

如何在 Android 中播放音频文件?

如何在android中处理音频(.3gp)文件[关闭]

流式传输实时音频

如何在 iOS 10 的 iMessage 应用程序中发送带有图像和标题的音频文件?