我有一个iOS流媒体应用。它适用于我的流媒体网址。如何将AudioStreamer用于.m3u8文件网址?
提前谢谢
答案 0 :(得分:1)
为什么您需要使用AudioStreamer
课程才能轻松使用AVPlayer
课程,尽管AudioStreamer
在内部使用AVPlayer
。
以下代码使用AVPlayer
播放实时流媒体音频,您可以更改源网址以嵌入您的信息流。
不要忘记在项目 - >目标 - >链接框架和库
下的Linked Frameworks and Libraries
部分添加AVFoundation.framework
#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
执行许多操作。快乐的编码:)
答案 1 :(得分:0)
尝试并解析m3u8文件并获取资源的实际URL,然后将该URL传递给AudioStreamer。
此处 - &gt; 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