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

本文关键字:parseObject 下载 parseFile android PARSE Xamarin | 更新日期: 2023-09-27 18:33:12

我之前发过一篇文章,询问如何将.3gpp音频文件发送到解析云:Xamarin C# Android - 将.3gpp音频转换为字节并发送到parseObject

我已经成功地做到了这一点,在parse的数据管理器上,我可以单击文件的链接并成功播放从我的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。此对象包含两个子对象:-上传文件的用户的用户名(字符串(-名为"file"的解析文件(具有以下两个子级(---SaveTheFile(包含音频文件名称的字符串,由用户输入,末尾添加.3gpp扩展名,例如"myAudioFile.3gpp"---数据(包含音频文件的字节数(

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

我已经检查了解析网站上的文档,但我还没有设法做到这一点:(请原谅我的伪查询语法(SELECT (audio files( 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 (); 
}

任何帮助将不胜感激!即使是在正确的方向上一点也会很棒!谢谢!

编辑--

我实际上在以下行上收到查询错误(由于我的声誉,我无法发布图像 - 我无法访问我的主堆栈溢出帐户:/(图片链接如下:

第一个错误:https://i.stack.imgur.com/PZBJr.png 第二个错误:https://i.stack.imgur.com/UkHvX.png有什么想法吗?解析文档对此含糊不清。

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

此行将返回结果集合

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
}