gpt4 book ai didi

Download Audio file from URL and play it in iOS App(从URL下载音频文件并在iOS应用程序中播放)

转载 作者:bug小助手 更新时间:2023-10-25 17:07:23 25 4
gpt4 key购买 nike



I have an URL which I get from JSON response, When I hit the URL in browser the file is downloaded, Now am wondering how can I play such URL which is a Download URL.I've been looking around many post But none of them could help me.

我有一个URL,我是从JSON响应中得到的,当我在浏览器中点击URL时,文件就被下载了,现在我想知道如何才能播放这样的URL,这是一个下载URL。我已经看了很多帖子,但没有一个能帮助我。



type of url am getting : http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1

我获取的URL类型:http://www.smartivr.in/sounds/voicemail/download/f77245d9-e7ee-4424-850c-6a45022d0a54_1



Below is the code I tried.

下面是我尝试过的代码。



  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);

更多回答

You should try streaming the audio file from the url instead. Edit: What @Marek said.

相反,您应该尝试从url中流式传输音频文件。编辑:@Marek说的话。

@Ramon but it is a download url.

@Ramon,但这是一个下载URL。

@Shikha Sharma Do you want to play audio from downloaded filePath or from your online url?

@Shikha Sharma您想从下载的文件路径或您的在线URL播放音频吗?

@DipankarDas i just need to play the audio , be it anyway.

@DipankarDas我只需要播放音频,无论如何。

First of all the URL is a remote URL so you have to use the [NSURL URLWithString... initializer and generally do not load data from a remote URL with a synchronous method like dataWithContentsOfURL. Use URLSession which works asynchronously.

首先,URL是一个远程URL,因此您必须使用[NSURL URLWithString...初始值设定项,通常不会使用dataWithContent sOfURL这样的同步方法从远程URL加载数据。使用异步工作的URLSession。

优秀答案推荐

Try this:

试试这个:



    //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];


You should download your audio file asynchronously.

您应该异步下载您的音频文件。



try use AVPlayer or AVPlayerViewController, you can use direct URL to play mp3 files

尝试使用AVPlayer或AVPlayerViewController,您可以使用直接URL播放mp3文件



even with local file as you will first download it to device, then play from local URL

即使是本地文件,您也会先将其下载到设备,然后从本地URL播放



do not forget that object AVPlayer has to be as property of your Class not local object because of auto-releasing, so declare your player variable in class and set in function not just locally in function and try to play ...

不要忘记,由于自动释放,对象AVPlayer必须作为你的类的属性而不是本地对象,所以在类中声明你的播放器变量,并在函数中设置函数,而不仅仅是在函数中本地设置,然后尝试玩…



[update]

[更新]



more details how to do it -> How to play video with AVPlayerViewController (AVKit) in Swift

更多详细操作->如何在SWIFT中使用AVPlayerViewController(AVKit)播放视频



Thank You Dipankar Das for you help.
I am posting the whole solution here.
which worked for me absolutely.

感谢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];


Here is the mistake

以下是错误所在



NSString *audio=[NSString stringWithFormat:@"%@.mp3",cell.url];
NSURL *url = [NSURL fileURLWithPath:audio];


I assume that it is a remote url and you cannot treat it as a file url. So you must use

我假设它是一个远程url,您不能将其视为文件url。所以你必须使用



NSURL  *url = [NSURL urlWithString:audio];


Just use AVPlayer instead of AVAudioPlayer

只需使用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];

更多回答

but i can't hear it, i applied same code, though volume button is working, otherwise it was not showing. but it's not audible

但我听不到,我用了同样的代码,虽然音量键起作用了,否则它不会显示。但它听不见

:( what could be the reason, that it's not audible in my device

:(可能是什么原因,我的设备听不到

you can check on your simulator.

你可以检查一下你的模拟器。

add this [audioPlayer setVolume:1.0]; for sound.

为声音添加此[AudioPlayer setVolume:1.0];。

hey i got it's solution

嘿,我找到解决方案了

That's what i wanna know. how to exactly download it and then play. cuz i tried so many methods and none of them worked. I'll be thankful, if you care to provide me some code.

这就是我想知道的。如何准确下载并播放。因为我试了那么多方法,但都不管用。如果你愿意为我提供一些代码,我将不胜感激。

And where is a problem? you should at least describe what exactly and which step is not working

问题出在哪里?您至少应该描述哪些步骤不起作用,哪些步骤不起作用

i have a mp3 URL which i want to play in avplayer . i have tried every possible way out there but it just dose not play the song . anyone one knows any method of paying song from given url ?

我有一个mp3URL,我想在avplay中播放。我试过了所有可能的方法,但就是不能奏效。有谁知道从给定的url付费歌曲的方法吗?

i have a mp3 URL which i want to play in avplayer . i have tried every possible way out there but it just dose not play the song . anyone one knows any method of paying song from given url ?

我有一个mp3URL,我想在avplay中播放。我试过了所有可能的方法,但就是不能奏效。有谁知道从给定的url付费歌曲的方法吗?

Do not use the code in this answer to download a remote file. Use NSURLSession.

请勿使用本答案中的代码下载远程文件。使用NSURLSession。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com