我可以从iOS上传MP3文件到我的服务器吗?

时间:2022-12-21 21:17:37

I was googling this question but nothing useful or current came up. I'd like to know if (and if, how) you can select an MP3 file (from itunes?) and upload the contents to one of my own servers on iOS (iphone & ipad app).

我正在谷歌搜索这个问题,但没有任何有用或当前出现。我想知道你是否(以及如果,如何)选择一个MP3文件(来自itunes?)并将内容上传到我自己的iOS服务器(iphone和ipad app)上。

One of my clients is asking me if it's possible to do this, and I havn't found the answer yet.

我的一位客户问我是否可以这样做,但我还没有找到答案。

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

The short answer would be YES.

简短的回答是肯定的。

Here is a working solution for me. But you need to use a third party library. Then this is what you need to do:

这对我来说是一个有效的解决方案。但是您需要使用第三方库。那么这就是你需要做的:

  1. Create a temp folder either in the NSDocuments directory or a temp directory.
  2. 在NSDocuments目录或临时目录中创建临时文件夹。

  3. Use MPMediaQuery to load the music files.
  4. 使用MPMediaQuery加载音乐文件。

  5. The object that you will get from the MPMediaQuery is an MPMediaItem. With this you can get the asset URL of the media item.
  6. 您将从MPMediaQuery获得的对象是MPMediaItem。通过此,您可以获取媒体项的资产URL。

Code:

NSString *assetURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];

get the extension of with the asset URL

获取资产URL的扩展名

NSString *extension = [TSLibraryImport extensionForAssetURL:assetURL];

set a location URL (This will be the location where the mp3 music data will be imported).

设置位置URL(这将是导入mp3音乐数据的位置)。

NSString *locationURL = [[NSURL fileURLWithPath:[path stringByAppendingPathComponent:musicTitleYouWant]] URLByAppendingPathExtension:extension];

Now you can import the contents of the mp3 from to the directory you set earlier.

现在,您可以将mp3的内容导入到之前设置的目录中。

TSLibraryImport *libraryImport = [[TSLibraryImport alloc] init];

[libraryImport importAsset:assetURL toURL:locationURL completionBlock:^(TSLibraryImport *libraryImport)
{
    if(libraryImport.status == AVAssetExportSessionStatusCompleted)
    {
         //Once complete the file will be store on the location URL you set earlier. Now you can get the file from that location and convert it to NSData and send it to the server. There are plenty of ways to do that.
     }

     else
     {
         //Here means import failed. :(
     }
}];

Hope this helps. :)

希望这可以帮助。 :)

#1


1  

The short answer would be YES.

简短的回答是肯定的。

Here is a working solution for me. But you need to use a third party library. Then this is what you need to do:

这对我来说是一个有效的解决方案。但是您需要使用第三方库。那么这就是你需要做的:

  1. Create a temp folder either in the NSDocuments directory or a temp directory.
  2. 在NSDocuments目录或临时目录中创建临时文件夹。

  3. Use MPMediaQuery to load the music files.
  4. 使用MPMediaQuery加载音乐文件。

  5. The object that you will get from the MPMediaQuery is an MPMediaItem. With this you can get the asset URL of the media item.
  6. 您将从MPMediaQuery获得的对象是MPMediaItem。通过此,您可以获取媒体项的资产URL。

Code:

NSString *assetURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];

get the extension of with the asset URL

获取资产URL的扩展名

NSString *extension = [TSLibraryImport extensionForAssetURL:assetURL];

set a location URL (This will be the location where the mp3 music data will be imported).

设置位置URL(这将是导入mp3音乐数据的位置)。

NSString *locationURL = [[NSURL fileURLWithPath:[path stringByAppendingPathComponent:musicTitleYouWant]] URLByAppendingPathExtension:extension];

Now you can import the contents of the mp3 from to the directory you set earlier.

现在,您可以将mp3的内容导入到之前设置的目录中。

TSLibraryImport *libraryImport = [[TSLibraryImport alloc] init];

[libraryImport importAsset:assetURL toURL:locationURL completionBlock:^(TSLibraryImport *libraryImport)
{
    if(libraryImport.status == AVAssetExportSessionStatusCompleted)
    {
         //Once complete the file will be store on the location URL you set earlier. Now you can get the file from that location and convert it to NSData and send it to the server. There are plenty of ways to do that.
     }

     else
     {
         //Here means import failed. :(
     }
}];

Hope this helps. :)

希望这可以帮助。 :)