Windows Phone中的音频流到Base64字符串问题

时间:2014-08-19 05:08:28

标签: c# base64 windows-phone-8.1

我正在创建Windows Phone 8.1应用程序,我创建了Audio Recorder模块,并将音频流转换为Base64String,但我得到的Base64String如下所示:

"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

有关确切的想法,请参阅下面的代码:

public void UpdateWavHeader()
{
    if (!stream.CanSeek) throw new Exception("Can't seek stream to update wav header");

    var oldPos = stream.Position;

    stream.Seek(4, SeekOrigin.Begin);
    stream.Write(BitConverter.GetBytes((int)stream.Length - 8), 0, 4);

    stream.Seek(40, SeekOrigin.Begin);
    stream.Write(BitConverter.GetBytes((int)stream.Length - 44), 0, 4);

    stream.Seek(oldPos, SeekOrigin.Begin);

    IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

    string isoVideoFileName = "DemoFile.aac";

    if (isf.FileExists(isoVideoFileName))
    {
        isf.DeleteFile(isoVideoFileName);
    }

    isoVideoFile = new IsolatedStorageFileStream(isoVideoFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
    isoVideoFile.Write(stream.ToArray(), 0, stream.ToArray().Length);

    byte[] chunk = new byte[isoVideoFile.Length];
    string base64String = Convert.ToBase64String(chunk);
}

1 个答案:

答案 0 :(得分:2)

在代码中查看这两行:

byte[] chunk = new byte[isoVideoFile.Length];
string base64String = Convert.ToBase64String(chunk);

您基本上编码了一个初始化为零的字节数组。

编辑:

假设您要对要写入.aac文件的流进行编码,这应该可以解决问题:

var chunk = stream.ToArray();

isoVideoFile = new IsolatedStorageFileStream(isoVideoFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
isoVideoFile.Write(chunk, 0, chunk.Length);

//byte[] chunk = new byte[isoVideoFile.Length];
string base64String = Convert.ToBase64String(chunk);