2010-03-04 4 views
2

일부 사이트에서 mp3 파일을 다운로드하고 CoreData 모델 AudioMp3에 저장하고 재생하고 싶습니다.iPhone에서 mp3를 다운로드하고 저장하고 재생하십시오.

아래의 기능은 작동하지 않지만 우선 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; 
} 

답변

2

솔루션이었다 AVAudioPlayer 및 방법 (ID)를 사용하는 initWithData : (을 NSData *) 데이터 오류 : (NSError **) outError입니다. 저장 및로드가 원활하게 작동합니다.

관련 문제