我正在开发音乐应用程序,我需要将转换后的歌曲存储到“文档目录”以供以后使用。由于某种原因,它不是从存储的目录URL播放,但我也没有收到任何错误。我在目录中检查并发现其中存在相同的存储歌曲。
之前在目录中成功播放了相同的歌曲网址。
我的代码,用于在从mp3转换为m4a后存储歌曲。
{
NSURL *assetURL = [NSURL URLWithString:_assertURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
/* approach 1: export just the song itself
*/
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: songAsset
presetName: AVAssetExportPresetAppleM4A];
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *exportFile = [myDocumentsM4aDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.m4a",_title]];
// end of approach 1
// set up export (hang on to exportURL so convert to PCM can find it)
[self myDeleteFile:exportFile];
//[exportURL release];
_exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = _exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to view
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"AVAssetExportSessionStatusCompleted");
//Final document url
NSURL *audioUrl = _exportURL;
NSLog(@"Audio Url=%@",audioUrl);
break;
}
case AVAssetExportSessionStatusUnknown: {
NSLog (@"AVAssetExportSessionStatusUnknown");
break;
}
case AVAssetExportSessionStatusExporting: {
NSLog (@"AVAssetExportSessionStatusExporting");
break;
}
case AVAssetExportSessionStatusCancelled: {
NSLog (@"AVAssetExportSessionStatusCancelled");
break;
}
case AVAssetExportSessionStatusWaiting: {
NSLog (@"AVAssetExportSessionStatusWaiting");
break;
}
default: {
NSLog (@"didn't get export status");
break;
}
}
}];
}
删除当前路径中可用的现有文件:
- (void)myDeleteFile:(NSString*)path{
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSError *deleteErr = nil;
[[NSFileManager defaultManager] removeItemAtPath:path error:&deleteErr];
if (deleteErr) {
NSLog (@"Can't delete %@: %@", path, deleteErr);
}
}
}
这是用于创建m4a目录的代码:
NSString* myDocumentsM4aDirectory(){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSError *error = nil;
NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/m4aFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
//Create folde
return dataPath;
}
如何找出这个问题永久播放本地存储的歌曲?
编辑:
//以下函数调用播放歌曲
- (void)playSong
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_exportURL] options:nil];
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:asset.URL error:&error];
if(error == nil)
{
[player play]
}else
{
NSLog (@"Error during playing: %@", error);
}
}
谢谢,
答案: 我找到了没有从文档播放的歌曲的解决方案,因为我之前将整个断言URL存储到核心数据。文件ID“xxxxxx-xxxxxxx-xxxxxxx-xxxxxxxxx”将在每次发布时保持不变。
所以,我正在根据之前附加到URL的标题和扩展名获取断言路径。现在它的工作正常。
此回答有助于解决此问题:Play local audio file with AVAudioPlayer
答案 0 :(得分:0)
You can play song directly from document directory
url = [NSURL fileURLWithPath:strFilePath];
NSError *error;
if(mp3player)
{
mp3player = nil;
}
mp3player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
mp3player.delegate = self;
[mp3player setVolume:1.0];
mp3player.numberOfLoops = 0;
[mp3player prepareToPlay];
[mp3player play];
注意:
在.h文件中定义“mp3player”。