簡體   English   中英

將音頻從服務器流式傳輸到iPhone

[英]Streaming audio from server to iPhone

我正在尋找將我服務器上的一些音頻文件流式傳輸到我正在編寫的iPhone客戶端應用程序。 即使在壓縮之后,一些音頻文件的大小也可能相當大。 我的問題是,是否有一個Cocoa框架可以幫助我緩沖音頻,以便用戶可以使用它而其余部分被放下管道? 如果沒有,任何人都可以指出我正確的方向,以了解更多關於使用Objective-C / Cocoa實現這樣的事情所需的技術嗎?

從服務器< - >客戶端基礎設施緩沖和壓縮音頻的權威資源將是理想的。

Brad提到了我寫的一篇帖子,用於通過HTTP傳輸音頻。 就是這個: 流媒體和播放MP3 我不是故意成為鏈接到他自己的博客的人,但它確實解決了你的問題。

關於解決問題的難度,您會注意到我已經對該帖子進行了4次更新(比其他任何方式都做得更多),修復了線程錯誤,內存泄漏和網絡緩沖問題。 有很多東西要准確無誤。

MPMoviePlayerController可以播放音頻文件。 如果使用內容URL初始化對象(例如,這可以是服務器上的Mp3文件),它將流式傳輸文件並進行回放。

初始化如下所示:

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

MPMoviePlayerController* yourPlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theURL];

從那里你可以彈出一個播放視圖,它將顯示一個用於擦除內容的滑塊,它將填充滑塊作為內容緩沖區。

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

或者您可以讓它播放並創建自己的UI:

[yourPlayerController.moviePlayer play];

除非您告訴應用程序在info.plist文件中的后台運行,否則此播放方法將在屏幕鎖定時切斷音頻。

我現在知道你希望已經解決了你的問題。 但只是因為。 如果你想要一個直接的解決方案,你總是可以得到嵌入代碼或任何其他鏈接,並從中制作一個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

不幸的是,你會在你面前做一些工作。 去年夏天,我不得不為iPhone項目做這件事。 您需要的大部分內容都在AudioToolbox框架中的音頻隊列服務中。

使用音頻隊列服務有點棘手,您必須實現一堆回調來告訴它如何在數據包離開網絡后對其進行處理。

有人,我認為Matt Gallagar在博客上有一些示例代碼沒問題,Apple的核心音頻郵件列表上有很多東西。

我確實發現我最終不需要在NSURL Connection上進行中繼來執行網絡部分,因為我的didrecievedata委托方法沒有經常觸發。

對不起,我不能在這里插入幾行代碼作為示例,但是我寫的這個類大約有400行。

您可以使用AVFoundation框架中的AVPlayer類從本地文件或遠程服務器流式傳輸音頻。 AVPlayer類將通知您播放的准備情況或緩沖更多數據的需要。

但是,如果您想要更多控制,您肯定應該使用音頻隊列服務。 使用音頻隊列服務有點困難,但是一旦掌握了它,你就能夠以你想要的方式微調音頻播放過程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM