在 iPhone 上下载、保存和播放 mp3
我想从某个网站下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是使用 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.