将 mp3 数据保存在 iPhone 上的文件中

发布于 2024-10-21 03:17:50 字数 160 浏览 1 评论 0原文

我正在开发一个 iPhone 应用程序,我需要在 iPhone 上下载并保存 mp3 媒体并播放它, 我完成了下载工作,并将数据放入 NSMutableData 变量中,但是如何将这些数据保存为 mp3 文件

任何建议:)

I'm developing an iPhone application and i need to download and Save mp3 media on the iPhone and play it,
I fished the downloading work and i put the data in NSMutableData variable But How can i save these data as mp3 file

any advice :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-10-28 03:17:50

如果您想将其保存到应用程序的 Documents 目录并使用 .mp3 文件扩展名,您可以使用以下命令:

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, 
                                                              YES) lastObject]
                         stringByAppendingPathComponent:@"MyFileName.mp3"];
[[NSFileManager defaultManager] createFileAtPath:mp3FileName 
                                        contents:mp3Data 
                                      attributes:nil];

其中 mp3Data 是您的 NSMutableData。我使用与上面类似的代码来保存 PDF 文档、自定义文件格式、jpg 和各种类型。我还没有理由尝试 mp3,但没有理由不尝试。

要将文件读回 NSData 以发送到 AVAudioPlayer:

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, 
                                                          YES) lastObject]
                          stringByAppendingPathComponent:@"MyFileName.mp3"];
NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
avAudioPlayer.data = mp3Data;

以上代码未经测试,从来没有理由在我的应用程序中使用声音。

If you want to save it to your applications Documents directory with a .mp3 file extension you can use the following:

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, 
                                                              YES) lastObject]
                         stringByAppendingPathComponent:@"MyFileName.mp3"];
[[NSFileManager defaultManager] createFileAtPath:mp3FileName 
                                        contents:mp3Data 
                                      attributes:nil];

Where mp3Data is your NSMutableData. I use code similar to the above for saving PDF documents, custom file formats, jpgs and all sorts. I've not had cause to try mp3 yet but there's no reason why it shouldn't.

To read the file back into NSData to send to AVAudioPlayer:

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, 
                                                          YES) lastObject]
                          stringByAppendingPathComponent:@"MyFileName.mp3"];
NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
avAudioPlayer.data = mp3Data;

Above para of code untested, never had cause to use sounds in my apps.

地狱即天堂 2024-10-28 03:17:50

这是正确的解决方案附加在@Diziet评论

'//准备数据保存在文件中
NSData *fileData=[[NSData alloc]initWithData:(NSData *)receivedData];

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:@"MyFileName.mp3"];
//create the file
[[NSFileManager defaultManager] createFileAtPath:mp3FileName contents:receivedData attributes:nil];

//load the file for playing
NSString *FileNamePath = [[NSString alloc] initWithString:@"MyFileName.mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath ];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
player.delegate=self;
[player play];'

here is the correct soulution appending up on @Diziet comment

'//prepare data to save in file
NSData *fileData=[[NSData alloc]initWithData:(NSData *)receivedData];

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:@"MyFileName.mp3"];
//create the file
[[NSFileManager defaultManager] createFileAtPath:mp3FileName contents:receivedData attributes:nil];

//load the file for playing
NSString *FileNamePath = [[NSString alloc] initWithString:@"MyFileName.mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath ];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
player.delegate=self;
[player play];'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文