简体   繁体   English

无法流式传输.m3U8文件

[英]Unable to stream a .m3U8 file

I have an iOS streaming app. 我有一个iOS流应用程序。 It works fine for my streaming urls. 它对我的流URL正常工作。 How can I use AudioStreamer for .m3u8 file url? 如何将AudioStreamer用于.m3u8文件url?

Thank you in advance 先感谢您

Why you need to use AudioStreamer class when you can do it with AVPlayer easily, Though AudioStreamer uses AVPlayer internally. 为什么你需要使用AudioStreamer类时,你可以用做AVPlayer容易,虽然AudioStreamer使用AVPlayer内部。

Following code plays live streaming audio using AVPlayer , you can change source URL to embed your stream. 以下代码使用AVPlayer播放实时流音频,您可以更改源URL来嵌入流。

Don't forget to add AVFoundation.framework in your Linked Frameworks and Libraries section under Project->Target->Linked Frameworks and Libraries 不要忘记在“项目”->“目标”->“链接的框架和库”下的“ 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");
        }
    }
}

I hope it helps you to understand, there are lot of stuffs you can do with AVPlayer in given code. 我希望它能帮助您理解,在给定的代码中, AVPlayer可以做很多事情。 Happy coding :) 快乐的编码:)

Try and parse the m3u8 file and get the actual URL to the resource and then pass that URL to AudioStreamer. 尝试解析m3u8文件并获取资源的实际URL,然后将该URL传递给AudioStreamer。

Here -> https://github.com/Achillesun/M3U8Paser you can find M3U8Parser lib that parse the playlist. 在这里-> https://github.com/Achillesun/M3U8Paser,您可以找到解析播放列表的M3U8Parser库。

Below is an example of m3u8 format: 以下是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM