【问题标题】:Streaming audio from server to iPhone将音频从服务器流式传输到 iPhone
【发布时间】:2010-09-27 22:43:18
【问题描述】:

我希望将我服务器上的一些音频文件流式传输到我正在编写的 iPhone 客户端应用程序。一些音频文件的大小可能相当大,即使经过压缩也是如此。我的问题是,是否有一个 Cocoa 框架可以帮助我缓冲音频,以便用户可以使用它,而其余部分则通过管道传输?如果没有,谁能指出我正确的方向,以了解更多关于使用 Objective-C/Cocoa 实现这一目标所需的技术?

关于缓冲和压缩来自服务器的音频的权威资源客户端基础设施将是理想的。

【问题讨论】:

    标签: iphone objective-c cocoa audio


    【解决方案1】:

    我现在知道您希望已经解决了您的问题。但只是以防万一。如果您想要一个直接的解决方案,您可以随时获取嵌入代码或任何其他链接,并从中制作一个 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

    @interface ViewController : UIViewController {
    
    NSString *videoURL;
    
    }
    
    @property (nonatomic, strong) NSString *videoURL;
    
        (IBAction) tv;
    

    在 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]; }
    

    然后在 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]; }
    

    现在,在我的示例中,我提到了视频流,但您明白了,您可以流式传输或播放您想要的内容。只需使用UIWebView

    【讨论】:

    • 请编辑您的答案并将代码格式化为可读的内容:-)
    【解决方案2】:

    MPMoviePlayerController 可以播放音频文件。如果您使用内容 url 初始化对象(例如,这可以是服务器上的 Mp3 文件),它将流式传输文件并播放。

    初始化如下:

    NSURL *theURL = [NSURL urlWithString:@"http://yourdomain.com/yourmediafile.mp3"];
    
    MPMoviePlayerController* yourPlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
    

    您可以从那里弹出一个播放视图,该视图将显示一个用于浏览内容的滑块,并且它将作为内容缓冲区填充滑块栏。

    [yourPlayerController shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
    [self presentMoviePlayerViewControllerAnimated:yourPlayerController];
    

    或者你可以让它播放并创建你自己的用户界面:

    [yourPlayerController.moviePlayer play];
    

    除非您在 info.plist 文件中告诉应用程序在后台运行,否则这种播放方法会在屏幕锁定时切断音频。

    【讨论】:

      【解决方案3】:

      您可以使用 AVFoundation 框架中的 AVPlayer 类从本地文件或远程服务器流式传输音频。 AVPlayer 类将通知您准备好播放或需要缓冲更多数据。

      但是,如果您想要更多控制权,您绝对应该使用 Audio Queue 服务。使用音频队列服务有点困难,但一旦掌握了它,您就可以按照自己的方式微调音频播放过程。

      【讨论】:

        【解决方案4】:

        Brad 提到了一篇我写的通过 HTTP 流式传输音频的帖子。就是这个:Streaming and playing an MP3。我并不是要成为链接到他自己博客的人,但它确实可以解决您的问题。

        关于问题的解决难度,您会注意到我对该帖子进行了 4 次更新(比我对其他任何内容所做的更新都要多),修复了线程错误、内存泄漏和网络缓冲问题.有很多东西要准确无误。

        【讨论】:

        • 马特,我是您网站的忠实粉丝。您是少数几个高度重视确保我在阅读时保持活跃的精选提要之一。非常感谢您回馈您的见解。
        • 嘿@MattGallagher,do you think it's possible to stream audio the other way around? (从 iOS 设备到另一台机器?)
        • @Moshe 运行流媒体服务器与您需要的功能一样困难(可能非常困难)。通过 HTTP 提供纯 MP3 文件非常简单,可以处理大多数普通情况。在录制时提供实时流非常困难。
        【解决方案5】:

        不幸的是,您将面临一些工作。去年夏天我不得不为一个 iPhone 项目做这个。您需要的大部分内容都在 AudioToolbox 框架中的音频队列服务中。

        使用音频队列服务有点棘手,您必须实现一堆回调来告诉它如何在数据包离开网络后对其进行处理。

        有人,我认为是 Matt Gallagar,在博客上提供了一些示例代码,还不错,Apple 的 Core Audio 邮件列表中有很多东西。

        我确实发现我最终不需要中继 NSURL Connection 来执行网络部分,因为我的 didrecievedata 委托方法没有足够频繁地触发。

        很抱歉,我不能在这里只写几行代码作为示例,但我为此编写的类大约有 400 行。

        【讨论】:

          猜你喜欢
          • 2013-05-11
          • 1970-01-01
          • 1970-01-01
          • 2012-01-22
          • 2012-10-02
          • 2012-01-24
          • 1970-01-01
          • 1970-01-01
          • 2014-01-19
          相关资源
          最近更新 更多