我有一个iOS流媒体应用程序。它适用于我的流媒体URL。如何将AudioStreamer用于.m3u8文件url?
提前感谢
为什么你需要使用AudioStreamer
类,而你可以很容易地使用AVPlayer
,尽管AudioStreamer
在内部使用AVPlayer
。
以下代码使用AVPlayer
播放直播音频,您可以更改源URL以嵌入流。
不要忘记在项目->目标->链接框架和库下的
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
做很多事情。快乐编码:)
尝试解析m3u8文件,获取资源的实际URL,然后将该URL传递给AudioStreamer。
此处->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