开发者

byte array of sound data from Windows Phone 7 to App Engine

I am trying to post a bytes of sound data from Windows Phone 7 to an App Engine Java servlet. Somewhere along the way the data gets screwed up and when I try to play the audio it doesn't work. Here is some of the code:

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;
     开发者_如何学JAVA   //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 audio code:

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 code for creating the audio (audio is sent as an attachment on an email):

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);


You need to be aware of your endianness. I don't really know much about Google App Engine, but I'm guess that your servlet is Java code and the Windows Phone 7 code is C#? Since you are reading ints (4 bytes) on the app engine servlet, and writing bytes on the WP7 side, my first guess is that you are writing the bytes in one endianness and reading in another. If that is the case, you will either have to change the endianness when you're writing the bytes on the WP7 side (does WP7 have the BitConverter class?), or "switch" bytes on the other side to ensure the correct endianness.

Also, as a clarification, what do you mean by "doesn't work"? Does it play audio that's just garbled? Or is it silent? Etc.

One step to debug the endianness, is write a single integer (in the form of bytes) on the WP7 side, and read that integer on the App Engine side. Is it the same integer? If not, you are having an endianness issue.


Did you add the wave header to your file? The raw bytes from the microphone are not a wave file by themselves. You ned to add teh proper RIFF header that among other things contains information the number of channels, bytes per sample, sample rate, encoding, and some other bits. I put an article on CodeProject.com that contains the code you need. It's in a voice recorder. Also check out the comments area as there is a reference to a streaming recorder there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜