java - 如何从基于 java 的服务获取声音

标签 java web-services apache-flex blazeds

我有一个基于 Java 的服务器端和一个使用 Spring BlazeDS Integration 的 Flex 客户端。它工作正常,但我最近想从服务器端获取声音。

我关注了这个BlazeDS mapping doc ,它说当Java返回一个Byte[]时,它将被转换为我想要的ByteArray。所以我通过ByteArrayOutputStream处理MP3文件,将其转换为Byte[]并将其返回给前端,但是Actionscript获取的值变成了空值。

public Byte[] sayHello() {
    Byte[] ba = null;

    try {
        FileInputStream fis = new FileInputStream(
                "D:/e/Ryan Adams - I Wish You Were Here.mp3");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) > 0) {
            baos.write(buffer, 0, bytesRead);
        }

        byte[] byteArray = baos.toByteArray();
        ba = new Byte[byteArray.length];

        for (int i = 0; i < byteArray.length; i++) {
            ba[i] = Byte.valueOf(byteArray[i]);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ba;
}

ActionScript 代码:

<s:RemoteObject id="ro" destination="helloWorldService" fault="handleFault(event)">
    <s:channelSet>
        <s:ChannelSet>
            <s:AMFChannel uri="/flexspring/messagebroker/amf"/>
        </s:ChannelSet>
    </s:channelSet>
</s:RemoteObject>

...

private function loaded():void {
    var bArr:ByteArray = ro.sayHello() as ByteArray;
    l.text = "" + (bArr == null);
}

...

<s:Label id="l" text=""/>

它说“真实”。有谁知道问题出在哪里吗?

最佳答案

您的代码的问题在于 BlazeDS 上的所有 Flex 调用都是异步的。因此,ro.SomeMethod() 不会立即返回,而是将其排队,然后根据需要进行回调。

这是一个有效的示例请注意,我从未通过 BlazeDS 连接发送 byte[],但我不明白为什么它不起作用 --- 正如 J_A_X 所建议的那样,您可能想要流式传输声音,而不是一次发送整个声音。

无论如何 - 这是示例:

public function loaded():void
{
   var token:AsyncToken = ro.sayHello();
   token.addResponder(new mx.rpc.Responder(result, fault));
   // ...Code continues to execute...
}

public function result(event:ResultEvent):void
{
   // The byte[] is in event.result
   var bArr:ByteArray = event.result as ByteArray;
}

public function fault(event:FaultEvent):void 
{
   // Something went wrong (maybe the server on the other side went AWOL) 
}

关于java - 如何从基于 java 的服务获取声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408604/

相关文章:

java - 无法从 Java WebService 连接到 MS Access 数据库

web-services - 安全 Web 服务 : REST over HTTPS vs SOAP + WS-Security. 哪个更好?

apache-flex - Flex 弹出管理器 : How can I detect the existence of a modal popup?

java - 小程序不显示图像

java - 请放心解析消息时出错 : "Invalid UTF-8 middle byte 0x20"

Java - 更改计时器运行时的持续时间

java - 将唯一引用设置为 null 是否意味着它及其子线程被垃圾收集?

java - 从 wsdl url 创建 Web 服务客户端

android - 在 flex 中以编程方式获取 SIM 号码

java - 如何提高我的 Flex/Java 堆栈的生产力?