2016-04-15 146 views
0

我有一個iOS流應用程序。它適用於我的流式網址。我如何使用AudioStreamer .m3u8文件的URL?無法傳輸.m3U8文件

預先感謝您

+0

當你已經有'AVPlayer'來播放你現場的音頻/視頻流時,爲什麼你需要'AudioStreamer'?使用'AVPlayer'來流式傳輸音頻。 –

回答

0

嘗試解析M3U8文件,並得到實際的URL的資源,然後通過這個URL AudioStreamer。

Here - >https://github.com/Achillesun/M3U8Paser你可以找到解析播放列表的M3U8Parser庫。

下面是M3U8格式的示例:

#EXTM3U 
#EXT-X-TARGETDURATION:10 

#EXT-X-MEDIA-SEQUENCE:1 

#EXTINF:10, 
http://video.example.com/segment0.ts 
#EXTINF:10, 
http://video.example.com/segment1.ts 
#EXTINF:10, 
http://video.example.com/segment2.ts 
#EXT-X-ENDLIST 
1

爲什麼你需要使用AudioStreamer上課的時候你可以用AVPlayer輕鬆地做到這一點,雖然AudioStreamer使用AVPlayer內部。

以下代碼使用AVPlayer播放實時流式音頻,您可以更改源URL以嵌入您的流。

不要忘記添加AVFoundation.framework在Linked Frameworks and Libraries部分下項目 - >靶>鏈接的框架和庫

#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController() 
{ 
    AVPlayerItem * mPlayerItem; 
} 

@property (readwrite, retain, setter=setPlayer:, getter=player) AVPlayer* mPlayer; 
@property (strong) AVPlayerItem *mPlayerItem; 

@end 

@implementation ViewController 

@synthesize mPlayer; 
@synthesize mPlayerItem; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // *** Initialise player and register observer *** 
    NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]; 

    self.mPlayerItem = [AVPlayerItem playerItemWithURL:url]; 

    self.mPlayer = [AVPlayer playerWithPlayerItem:self.mPlayerItem]; 

    // *** Add Observer on AVPlayerItem to observer progress & status *** 
    [self.mPlayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil]; 

    [self.mPlayer play]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
         change:(NSDictionary *)change context:(void *)context 
{ 
    if (object == self.mPlayerItem && [keyPath isEqualToString:@"status"]) 
    { 
     if (self.mPlayer.currentItem.status == AVPlayerItemStatusFailed) 
     { 
      NSLog(@"------player item failed:%@",self.mPlayer.currentItem.error); 
     } 
     else if (self.mPlayer.currentItem.status == AVPlayerStatusReadyToPlay) 
     { 
      NSLog(@"Play"); 
      [self.mPlayer play]; 
     } 
     else if (self.mPlayer.currentItem.status == AVPlayerStatusFailed) 
     { 
      // something went wrong. player.error should contain some information 
      NSLog(@"Unable to play."); 
      NSLog(@"%@",self.mPlayer.error); 
     } 
     else if (self.mPlayer.currentItem.status == AVPlayerItemStatusUnknown) 
     { 
      NSLog(@"AVPlayer Unknown"); 
     } 
    } 
} 

我希望它可以幫助你理解,還有很多的東西,你可以用AVPlayer在給定的代碼。快樂編碼:)

+0

是的,它爲我工作... –