Xamarin C #android和PARSE - 在parseObject中下载parseFile

时间:2014-12-05 13:00:21

标签: c# android audio parse-platform xamarin

我之前发过帖子,询问如何将.3gpp音频文件发送到解析云: Xamarin C# Android - converting .3gpp audio to bytes & sending to parseObject

我已成功完成此操作,在解析数据管理器上,我可以单击该文件的链接并成功播放从我的Android设备发送的声音。 以下是将数据上传到云端的代码:

async Task sendToCloud(string filename)
    {

        ParseClient.Initialize ("Censored Key", "Censored Key");
        string LoadPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
        string savetheFile = sName + ".3gpp";
        string tempUserName;
        LoadPath += savetheFile;
        Console.WriteLine ("loadPath:  " + LoadPath);
        try
        {

            byte[] data = File.ReadAllBytes(LoadPath);
            ParseFile file = new ParseFile(savetheFile, data);
            await file.SaveAsync();

            var auidoParseObject = new ParseObject("AudioWithData");
            //Console.WriteLine(ParseUser.getUserName());
            if (ParseUser.CurrentUser != null)
            {
                tempUserName = ParseUser.CurrentUser.Username.ToString();
            }

            else{
                tempUserName = "Anonymous";
            }
            //tempUserName = ParseUser.CurrentUser.Username.ToString();
            Console.WriteLine("PARSE USERNAME: " + tempUserName);

                auidoParseObject["userName"] = tempUserName;


            auidoParseObject["userName"] = tempUserName;
            auidoParseObject["file"] = file;
            await auidoParseObject.SaveAsync();

        }

        catch (Exception e) 
        {
            Console.WriteLine("Failed to await audio object! {0}" + e);
        }

    }

正如您所看到的,我发送了一个名为" AudioWithData"的ParseObject。 该对象包含两个子节点: - 上传文件的用户的用户名(字符串) -the parseFile调用" file" (有以下两个孩子) --- SaveTheFile(包含音频文件名称的字符串,由用户输入,最后添加.3gpp扩展名,例如" myAudioFile.3gpp" ---数据(包含音频文件的字节)

我需要能够将文件下载到我的Android设备上,然后通过媒体播放器对象播放。

我已经检查了解析网站上的文档,但我还没设法做到这一点: (请原谅我的伪查询语法) SELECT(音频文件)FROM(parseObject)WHERE(用户名=当前用户) 然后,我最终希望将所有这些文件放入列表视图中,当用户单击该文件时,它会播放音频。

我尝试过以下但我真的不知道自己在做什么......

async Task RetrieveSound(string filename)
    {
        ParseClient.Initialize ("Censored key", "Censored key");
        Console.WriteLine ("Hit RetrieveSound, filename = " + filename);
        string username;
        var auidoParseObject = new ParseObject("AudioWithData");
        if (ParseUser.CurrentUser != null) {
            username = ParseUser.CurrentUser.Username.ToString ();
        } else {
            username = "Anonymous";
        }
        string cloudFileName;
        Console.WriteLine ("username set to: " + username);


        var HoldThefile = auidoParseObject.Get<ParseFile>("audio");

        //fgher
        var query = from audioParseObject in ParseObject.GetQuery("userName")
                where audioParseObject.Get<String>("userName") == username
            select file;
        IEnumerable<ParseFile> results = await query.FindAsync();

        Console.WriteLine ("passed the query");
        //wfojh

        byte[] data = await new HttpClient().GetByteArrayAsync(results.Url);
        Console.WriteLine ("putting in player...");
        _player.SetDataSourceAsync (data);
        _player.Prepare;
        _player.Start (); 
}

任何帮助都会非常受欢迎!即使是正确方向的一点也会很棒! 谢谢!

编辑 -

我实际上在以下行中收到了查询错误 (由于我的声誉,我无法发布图片 - 我无法访问我的主stackOverflow帐户:/) 这里链接图像:

第一个错误:http://i.stack.imgur.com/PZBJr.png   第二个错误:http://i.stack.imgur.com/UkHvX.png 有任何想法吗?解析文档对此很模糊。

1 个答案:

答案 0 :(得分:0)

此行将返回结果集合

IEnumerable<ParseFile> results = await query.FindAsync();

您需要使用foreach进行迭代,或者只选择第一个

// for testing, just pick the first one
if (results.Count > 0) {

  var result = results[0];

  byte[] data = await new HttpClient().GetByteArrayAsync(result.Url);

  File.WriteAllBytes(some_path_to_a_temp_file, data);

  // at this point, you can just initialize your player with the audio file path and play as normal

}