objective-c 将音频从服务器流式传输到 iPhone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/384900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Streaming audio from server to iPhone
提问by Coocoo4Cocoa
I'm looking to stream some audio files I have on my server to an iPhone-client application I'm writing. Some of the audio files can be rather large in size, even after compression. My question is, is there a Cocoa framework that helps me with buffering the audio so it becomes available to the user while the rest is being brought down the pipes? If not, can anyone point me in the right direction to learn more about the technologies required to make such a thing happen using Objective-C/Cocoa?
我希望将服务器上的一些音频文件流式传输到我正在编写的 iPhone 客户端应用程序。某些音频文件的大小可能相当大,即使经过压缩也是如此。我的问题是,是否有一个 Cocoa 框架可以帮助我缓冲音频,以便用户可以使用它,而其余的则被删除?如果没有,有人能指出我正确的方向,以了解更多关于使用 Objective-C/Cocoa 实现这种事情所需的技术吗?
A definitive resource on buffering and compression of audio from a server<->client infrastructure would be ideal.
从服务器<->客户端基础设施缓冲和压缩音频的权威资源将是理想的。
回答by Matt Gallagher
Brad mentioned a post that I wrote to stream audio via HTTP. That was this one: Streaming and playing an MP3. I don't mean to be the person that links to his own blog but it does kind of address your question.
Brad 提到了我写的一篇通过 HTTP 流式传输音频的帖子。那就是这个:流式传输和播放 MP3。我并不是要成为链接到他自己博客的人,但它确实解决了您的问题。
In regards to how hard the problem is to solve, you'll notice I've made 4 updates to that post (way more than I've done for anything else), fixing threading errors, memory leaks and network buffering issues. There is a lot to get precisely correct.
关于问题解决的难度,您会注意到我对该帖子进行了 4 次更新(比我对其他任何内容所做的更新都要多),修复了线程错误、内存泄漏和网络缓冲问题。有很多东西要准确正确。
回答by Hivebrain
MPMoviePlayerController can play audio files. If you initialize the object with a content url (this can be an Mp3 file on a server, for instance) it will stream the file and play it back.
MPMoviePlayerController 可以播放音频文件。如果您使用内容 url 初始化对象(例如,这可以是服务器上的 Mp3 文件),它将流式传输文件并播放它。
The initialization looks like this:
初始化看起来像这样:
NSURL *theURL = [NSURL urlWithString:@"http://yourdomain.com/yourmediafile.mp3"];
MPMoviePlayerController* yourPlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
From there you can pop up a playback view that will show a slider for scrubbing through the content, and it will fill in the slider bar as the content buffers.
从那里您可以弹出一个播放视图,该视图将显示一个用于浏览内容的滑块,并且它将填充滑块作为内容缓冲区。
[yourPlayerController shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
[self presentMoviePlayerViewControllerAnimated:yourPlayerController];
Or you could just make it play and create your own UI:
或者你可以让它播放并创建你自己的用户界面:
[yourPlayerController.moviePlayer play];
This method of playback will cut off the audio when the screen locks unless you tell the app to run in the background in the info.plist file.
这种播放方法会在屏幕锁定时切断音频,除非您在 info.plist 文件中告诉应用程序在后台运行。
回答by Alan Serhan
I know by now that you hopefully have solved your problem. But just incase. If you want a straight forward solution, you can always get the embeded code or any other link and make an html file out of it. Like
我现在知道你希望已经解决了你的问题。但只是以防万一。如果你想要一个直接的解决方案,你总是可以得到嵌入的代码或任何其他链接,并从中制作一个 html 文件。喜欢
<html>
<body>
<script type="text/javascript">
var embedBaseUrl = 'http://www.tv.net/';
var embedChannelId = 13;
var embedSize = [300,250];
var embedAutoplay = true;
</script>
<script type="text/javascript"src="http://www.tv.net/assets/js/embed.js"></script>
</body>
</HTML>
ViewController.h
视图控制器.h
@interface ViewController : UIViewController {
NSString *videoURL;
}
@property (nonatomic, strong) NSString *videoURL;
(IBAction) tv;
And in the ViewController.m
在 ViewController.m 中
@synthesize videoURL;
(IBAction) tv {
self.videoURL = @"http://dl.dropbox.com/x/xxxxxxxx/HTML/tv.html";
VideoViewController *videoViewController = [[VideoViewController alloc] initWithNibName:nil bundle:nil];
videoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; videoViewController.videoURL = self.videoURL;
[self presentViewController:videoViewController animated:YES completion:nil]; }
And then in VideoViewController.h
然后在 VideoViewController.h
IBOutlet UIWebView *videoView;
NSString *videoURL; NSString *videoHTML;
}
@property(nonatomic, retain) IBOutlet UIWebView *videoView; @property(nonatomic, retain) NSString *videoURL; @property(nonatomic, retain) NSString *videoHTML;
(void) embedTV;
VideoViewController.m
@synthesize videoView, videoURL, videoHTML;
(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }
(void)embedTV {
videoHTML = [NSString stringWithFormat:@"\ \ \ \ iframe {position:absolute; top:50%%; margin-top:-130px;}\ body {background-color:#000; margin:0;}\ \ \ \ \ \ ", videoURL];
[videoView loadHTMLString:videoHTML baseURL:nil]; }
(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.
videoView.backgroundColor = [UIColor blackColor]; videoView.opaque = NO;
[self embedTV]; }
Now in my example I referred to video stream, but you get the point, you can stream or play what you want. Simply just use UIWebView.
现在在我的示例中,我提到了视频流,但您明白了,您可以流式传输或播放您想要的内容。只需使用UIWebView.
回答by Brad The App Guy
Unfortunatally, you are going to have a bit of work in front of you. I had to do this for an iPhone project this last summer. Most of what you need is in Audio Queue Services in the AudioToolbox framwork.
不幸的是,您将有一些工作摆在您面前。去年夏天我不得不为一个 iPhone 项目做这件事。您需要的大部分内容都在 AudioToolbox 框架中的音频队列服务中。
Working with audio queue services is kind of tricky, you will have to implement a bunch of callbacks to tell it how to processes your packets after they come off the network.
使用音频队列服务有点棘手,你必须实现一堆回调来告诉它在你的数据包离开网络后如何处理它们。
Someone, I think Matt Gallagar, had some sample code on a blog that was okay, and there is a bunch of stuff on Apple's Core Audio Mailing List.
有人,我认为 Matt Gallagar,在博客上有一些示例代码,很好,Apple 的 Core Audio 邮件列表上有很多东西。
I did find that I ended up needing to not relay on NSURL Connection to do the network part, because my didrecievedata delegate method was not firing often enough.
我确实发现我最终不需要中继 NSURL Connection 来完成网络部分,因为我的 didrecievedata 委托方法没有足够频繁地触发。
Sorry I could not just drop in a few lines of code as an example here, but the class I wrote to do this came in at around 400 lines.
抱歉,我不能在这里只用几行代码作为示例,但是我编写的用于执行此操作的类大约有 400 行。
回答by Kemal Taskin
You can use the AVPlayerclass from the AVFoundation framework to stream audio from local files or from a remote server. The AVPlayer class will notify you about readiness to play or about the need to buffer more data.
您可以使用AVFoundation 框架中的AVPlayer类从本地文件或远程服务器流式传输音频。AVPlayer 类将通知您准备好播放或需要缓冲更多数据。
However if you want more control you should definitely use Audio Queueservices. Using Audio Queue services is a little bit harder but once you get the hang of it you'll be able to fine-tune the audio playback process the way you want it.
但是,如果您想要更多控制,您绝对应该使用音频队列服务。使用音频队列服务有点困难,但是一旦你掌握了它,你就可以按照你想要的方式微调音频播放过程。

