从Windows Phone 7到App Engine的声音数据的字节数组

时间:2011-04-24 03:01:32

标签: google-app-engine servlets windows-phone-7 audio

我正在尝试将一个字节的声音数据从Windows Phone 7发布到App Engine Java servlet。在某个地方,数据被搞砸了,当我尝试播放音频时,它不起作用。以下是一些代码:

WP7:

public static void SendEmail(byte[] data, String to, String subject, String filename)
    {
        //WebClient wc = new WebClient();
        //wc.Headers[HttpRequestHeader.ContentType] = "application/binary";
        String url = "http://someserver/test?to=" + to + "&subject=" + subject + "&filename=" + filename;
        //wc.OpenReadAsync(new Uri(url), "POST");

        var webClient = new WebClient();
        webClient.OpenWriteAsync(new Uri(url), "POST", data);
        webClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webClient_OpenWriteCompleted);
    }

    static void webClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
    {
        object[] objArr = e.UserState as object[];
        byte[] fileContent = e.UserState as byte[];

        Stream outputStream = e.Result;
        outputStream.Write(fileContent, 0, fileContent.Length);
        outputStream.Flush();
        outputStream.Close();
    }

App Engine Servlet:

InputStream in = req.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = in.read();
while (next > -1) {
  bos.write(next);
  next = in.read();
}       
bos.flush();
attachmentData = bos.toByteArray();

Wp7音频代码:

microphone.GetData(buffer);

stream.Write(buffer, 0, buffer.Length);
//then I save the stream.toArray() to an object and use it later for playback on the   phone

用于创建音频的Java代码(音频作为电子邮件中的附件发送):

MimeBodyPart attachmentPart = new MimeBodyPart();
        attachmentPart.setFileName(filename + ".wav");
        attachmentPart.setDisposition(Part.ATTACHMENT);
        DataSource src = new ByteArrayDataSource(attachmentData, "audio/x-wav");
        DataHandler handler = new DataHandler(src);
        attachmentPart.setDataHandler(handler);
        mp.addBodyPart(attachmentPart);

2 个答案:

答案 0 :(得分:2)

您需要了解自己的endianness。我对Google App Engine了解不多,但我猜你的servlet是Java代码而Windows Phone 7代码是C#?既然你在app引擎servlet上读取整数(4个字节),并在WP7端写入字节,我的第一个猜测是你用一个字节序写入字节并在另一个字节序中读取。如果是这种情况,那么当您在WP7端写入字节时(WP7具有BitConverter类吗?)或者在另一侧“切换”字节以确保正确的字节顺序时,您将不得不更改字节顺序。

另外,作为澄清,“你的工作”是什么意思?它播放的音频是否只是乱码?还是沉默?等

调试字节序的一步是在WP7端写一个整数(以字节的形式),并在App Engine端读取该整数。它是相同的整数吗?如果没有,您将遇到字节序问题。

答案 1 :(得分:0)

您是否已将wave标头添加到文件中?来自麦克风的原始字节本身不是波形文件。您需要添加适当的RIFF标头,其中包含信息的数量,每个样本的字节数,采样率,编码和其他一些位。我在CodeProject.com上放了一篇包含你需要的代码的文章。它在录音机中。还可以查看评论区域,因为那里有一个流媒体记录器的参考。