在 iPhone 上下载、保存和播放 mp3

发布于 2024-08-23 23:00:28 字数 1687 浏览 5 评论 0原文

我想从某个网站下载 mp3 文件,将其保存到我的 CoreData 模型 AudioMp3 中,然后播放。

下面的函数可以工作,但首先,效率低下,因为它必须首先将 mp3 保存到文件,其次,它在接下来的调用次数中重复播放相同的 mp3。我认为我保存到数据库也不正确,其中 AudioMp3.mp3 被声明为二进制:

- (void) playAndSaveMp3: (NSString*) mp3 {

NSURL *urlFromString = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@.mp3",@"http://www.somsite.com/mp3/",mp3]];

NSData *data = [NSData dataWithContentsOfURL:urlFromString];

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

AudioMp3 *audioMp3 = (AudioMp3 *)[NSEntityDescription insertNewObjectForEntityForName:@"AudioMp3"
                                                                inManagedObjectContext:managedObjectContext];
[audioMp3 setCreationDate:[NSDate date]];

[audioMp3 setMp3Name:mp3];


//[audioMp3 setMp3:data];

// Save to database
NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
    NSLog(@"Error in saving new notification. Error: %@", error);
}

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/sound.mp3"];

[data writeToFile:resourcePath atomically:NO];

//declare a system sound id
SystemSoundID soundID;

// Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:resourcePath isDirectory:NO];

// Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);

// Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

I would like to download an mp3 file from some site, save it to my CoreData model, AudioMp3, and play it.

The function below sort of works, but firstly, is inefficient in that it has to save the mp3 to file first, and secondly it plays a repeat of the same mp3 the following times it is called. I don't think my save to database is right either, where AudioMp3.mp3 is declared as binary:

- (void) playAndSaveMp3: (NSString*) mp3 {

NSURL *urlFromString = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@.mp3",@"http://www.somsite.com/mp3/",mp3]];

NSData *data = [NSData dataWithContentsOfURL:urlFromString];

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

AudioMp3 *audioMp3 = (AudioMp3 *)[NSEntityDescription insertNewObjectForEntityForName:@"AudioMp3"
                                                                inManagedObjectContext:managedObjectContext];
[audioMp3 setCreationDate:[NSDate date]];

[audioMp3 setMp3Name:mp3];


//[audioMp3 setMp3:data];

// Save to database
NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
    NSLog(@"Error in saving new notification. Error: %@", error);
}

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/sound.mp3"];

[data writeToFile:resourcePath atomically:NO];

//declare a system sound id
SystemSoundID soundID;

// Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:resourcePath isDirectory:NO];

// Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);

// Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

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

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

发布评论

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

评论(1

ゞ记忆︶ㄣ 2024-08-30 23:00:28

解决方案是使用 AVAudioPlayer 和方法 (id)initWithData:(NSData *)data error:(NSError **)outError。无缝保存和加载工作。

The solution was to use AVAudioPlayer and the method (id)initWithData:(NSData *)data error:(NSError **)outError. Saving and loading work seamlessly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文