我有一個我從JSON響應中獲得的URL,當我在瀏覽器中點擊該URL時,該文件被下載,現在我想知道如何播放這樣的URL,這是一個下載網址。我一直在找很多帖子但是他們沒有人能幫助我。從URL下載音頻文件並在iOS中播放App
類型的網址我得到:http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1
下面是我試過的代碼。
NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
NSLog(@"url:%@",audio);
NSURL *url = [NSURL fileURLWithPath:audio];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:&error];
[_audioPlayer play];
NSLog(@"error %@", error);
您應該嘗試從網址中流式傳輸音頻文件。編輯:@Marek說什麼。 – Ramon
@Ramon,但它是一個下載網址。 –
@Shikha Sharma你想從下載的文件路徑或從您的在線網址播放音頻? –