我有一个从 JSON 响应中获得的 URL,当我在浏览器中点击该 URL 时,文件就会被下载,现在我想知道如何播放这样的 URL(即下载 URL)。我一直在浏览很多帖子,但没有一个他们可以帮助我。
我得到的网址类型: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);
试试这个:
//download your audio file
NSString *urlString = @"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
NSURL *url = [NSURL fileURLWithPath:urlString];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
// get the file from directory
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
{
soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
}
// play audio file
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
您应该异步下载音频文件。
谢谢 Dipankar Das 的帮助。 我在这里发布整个解决方案。 这绝对对我有用。
NSString *audio=@"http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1.mp3";
NSURL *url = [NSURL URLWithString:audio];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
// get the file from directory
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *soundPath = [documentsDirectory stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
{
soundUrl = [NSURL fileURLWithPath:soundPath isDirectory:NO];
}
// plau audio file
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
[_audioPlayer setVolume:5.0];
尝试使用AVPlayer或AVPlayerViewController,可以使用直接URL播放mp3文件
即使使用本地文件,您也会先将其下载到设备,然后从本地 URL 播放
不要忘记,由于自动释放,对象 AVPlayer 必须作为类的属性而不是本地对象,因此在类中声明你的播放器变量并在函数中设置,而不仅仅是在函数中本地设置并尝试播放...
[更新]
更多详细信息如何操作 -> 如何在 Swift 中使用 AVPlayerViewController (AVKit) 播放视频
这里有错误
NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
NSURL *url = [NSURL fileURLWithPath:audio];
我假设它是一个远程 url,你不能将其视为文件 url。所以你必须使用
NSURL *url = [NSURL urlWithString:audio];
只需使用 AVPlayer 而不是 AVAudioPlayer
@property (nonatomic, strong) AVPlayer *player;
AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:@"https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a"]];
// Create player item
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[self.player seekToTime:kCMTimeZero];
[self.player play];