gpt4 book ai didi

ios - 无法流式传输 .m3U8 文件

转载 作者:行者123 更新时间:2023-11-29 01:04:02 26 4
gpt4 key购买 nike

我有一个 iOS 流媒体应用程序。它非常适合我的流媒体网址。如何将 AudioStreamer 用于 .m3u8 文件 URL?

提前致谢

最佳答案

虽然 AudioStreamer 使用 AVPlayer 可以很容易地使用 AVPlayer ,但为什么需要使用 AudioStreamer 类> 内部。

以下代码使用 AVPlayer 播放实时流音频,您可以更改源 URL 以嵌入您的流。

Don't forget to add AVFoundation.framework in your Linked Frameworks and Libraries section under Project->Target->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 做很多事情。快乐编码:)

关于ios - 无法流式传输 .m3U8 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com