简体   繁体   English

在Parse.com上保存检索声音

[英]Saving an retrieving sound on Parse.com

I am trying to download some short sound file on Parse.com in an iOS application. 我正在尝试在iOS应用程序中的Parse.com上下载一些短声音文件。 The file has previously been saved using the following code: 该文件以前已使用以下代码保存:

    NSData *soundData = [NSData dataWithContentsOfURL:myURL];
    parse_Sound = [PFFile fileWithName:@"XXYYZZ"
                                  data:soundData];
    [parse_Sound saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!succeeded) {
                NSLog(@"sound-upload NG”);
        } else {
                NSLog(@"sound-upload OK");
            }];
        }
    }];

Seeing the message on the debugging console, it appearently works. 在调试控制台上看到消息后,它似乎可以工作了。 Now what kind of code do I need to run to retrieve(download) the sound file? 现在,我需要运行哪种代码来检索(下载)声音文件? I have browsed the net, but found nothing clear and working. 我已经浏览了网络,但发现没有任何内容可以正常工作。

You need to keep a reference to that file somewhere (ideally in a column of the PFObject it belongs to). 您需要在某个位置保留对该文件的引用(理想情况是在其所属的PFObject的列中)。

If you don't keep a reference you're out of luck and you can't retrieve already uploaded files that have no association to any object. 如果不保留引用,那么您将很不走运,并且无法检索与任何对象都没有关联的已上传文件。

I suggest you read through the Parse documentation for files on iOS https://www.parse.com/docs/ios_guide#files/iOS 我建议您通读iOS上文件的解析文档https://www.parse.com/docs/ios_guide#files/iOS

To get data back from the server you need to need to run a query asking for that object but you haven't associated the uploaded file with a column in any Class yet. 要从服务器取回数据,您需要运行查询以查询该对象,但尚未将上载的文件与任何类中的列相关联。 Uploading a PFFile is iOS is a two step process: 在iOS上上传PFFile的过程分为两个步骤:

1) Upload the PFFile to the server 1)将PFFile上传到服务器

2) In the callback associated the PFFile with a column in a data object 2)在回调中,将PFFile与数据对象中的列相关联

NSData *soundData = [NSData dataWithContentsOfURL:myURL];
    parse_Sound = [PFFile fileWithName:@"XXYYZZ"
                                  data:soundData];
    [parse_Sound saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!succeeded) {
                NSLog(@"sound-upload NG”);
        } else {
                NSLog(@"sound-upload OK");

                PFObject *soundStuff = [PFObject objectWithClassName:@"Sounds"];

                soundStuff[@"soundFile"] = parse_Sound;
                [soundStuff saveInBackground];

            }];
        }
    }];

Now to get the data back you would run a query on the Sounds class that will have the sound data in the soundFile column: 现在,要取回数据,您可以在Sounds类上运行查询,该查询将在soundFile列中包含声音数据:

PFQuery *query = [PFQuery queryWithClassName:@"Sounds"];
[query whereKey:@"someKey" equalTo:@"someValue"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded.

    // Do something with the found objects
    for (PFObject *object in objects) {
        NSLog(@"%@", object.objectId);
        PFFile *soundFile = object[@"soundFile"];
        NSData *soundData = [soundFile getData];

    }
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

I haven't tested any of this code but it at least demonstrates the steps needed and should get you started. 我没有测试过任何代码,但它至少演示了所需的步骤,应该让您入门。

Here are the examples from the documentation : 以下是文档中的示例:

PFObject *jobApplication = [PFObject objectWithClassName:@"JobApplication"]
jobApplication[@"applicantName"] = @"Joe Smith";
jobApplication[@"applicantResumeFile"] = file;
[jobApplication saveInBackground];

Then to get the data back: 然后取回数据:

PFFile *applicantResume = anotherApplication[@"applicantResumeFile"];
NSData *resumeData = [applicantResume getData];

Notice that file is being associated with the applicantResumeFile column of the JobApplication class so that the file data can be retrieved in queries. 请注意, fileJobApplication类的applicantResumeFile列相关联,以便可以在查询中检索文件数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM