使用 MPMoviePlayerViewController 在 iOS 4 中流式传输视频?

发布于 2024-11-17 17:41:05 字数 294 浏览 4 评论 0原文

我想在 iPhone 中通过 http 流传输 *.m3U8 视频文件。在 iOS 3.2 之前,它曾经在 iPhone 上完美播放。
但随着 MoviePlayer API 的最新更改,我无法在 iOS4 设备上流式传输此文件。

我现在使用 MPMoviePlayerViewController 对象来播放此文件,但每次都会收到错误“错误代码=2。文件无法播放”。
错误描述非常不足,Apple 的 MPMoviePlayerViewController 示例代码已过时。
请帮助我解决问题或提供一些示例代码或工作解决方案的参考。

I want to stream a *.m3U8 video file over http streaming in iPhone.prior to iOS 3.2,it used to play flawlessly on iPhone.
But with latest changes to the MoviePlayer APIs, I am unable to stream this file on iOS4 devices.

I am now using MPMoviePlayerViewController object to play this file, but every time I get the error "Error code=2. File could not be played".
Error description is very insufficient and Apple's sample code for MPMoviePlayerViewController is obsolete.
Please help me out with the solution or give some reference to the sample code or working solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-11-24 17:41:05

控制器:

#import <AVFoundation/AVFoundation.h>

@property (nonatomic, retain) AVPlayer *avPlayer;
@property (nonatomic, retain) IBOutlet PlayerView *playerView;
@property (nonatomic, retain) AVPlayerItem *playerItem;

// Use a HTTP URL, example: http://192.168.2.103/video.m3u8
-(void) setUpPlayer:(NSURL*) url {
    AVURLAsset *anAssest= [[AVURLAsset alloc]initWithURL:url options:nil];
    [anAssest loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
        NSError *error = nil;
        AVKeyValueStatus status = [anAssest statusOfValueForKey:@"tracks" error:&error];
        if (status == AVKeyValueStatusLoaded) {
            self.playerItem = [AVPlayerItem playerItemWithAsset:anAssest];
            self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
            self.playerView.player=self.avPlayer;
            [self.avPlayer play];
        } else {
            NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
        }
    }];
}

视图:

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
@interface PlayerView : UIView
@property (nonatomic, retain) AVPlayer* player;
@end

#import "PlayerView.h"
@implementation PlayerView
+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer*)[self layer] player];
}
- (void)setPlayer:(AVPlayer*)player {
    [(AVPlayerLayer*)[self layer] setPlayer:player];
}
@end

类似这样的东西 :P 您应该使用 Apple 的 mediafilesegmenter 命令创建 m3u8,但它可能无论如何都会起作用。

The controller:

#import <AVFoundation/AVFoundation.h>

@property (nonatomic, retain) AVPlayer *avPlayer;
@property (nonatomic, retain) IBOutlet PlayerView *playerView;
@property (nonatomic, retain) AVPlayerItem *playerItem;

// Use a HTTP URL, example: http://192.168.2.103/video.m3u8
-(void) setUpPlayer:(NSURL*) url {
    AVURLAsset *anAssest= [[AVURLAsset alloc]initWithURL:url options:nil];
    [anAssest loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
        NSError *error = nil;
        AVKeyValueStatus status = [anAssest statusOfValueForKey:@"tracks" error:&error];
        if (status == AVKeyValueStatusLoaded) {
            self.playerItem = [AVPlayerItem playerItemWithAsset:anAssest];
            self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
            self.playerView.player=self.avPlayer;
            [self.avPlayer play];
        } else {
            NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
        }
    }];
}

The view:

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
@interface PlayerView : UIView
@property (nonatomic, retain) AVPlayer* player;
@end

#import "PlayerView.h"
@implementation PlayerView
+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer*)[self layer] player];
}
- (void)setPlayer:(AVPlayer*)player {
    [(AVPlayerLayer*)[self layer] setPlayer:player];
}
@end

Something like that :P You should create the m3u8 with Apple's mediafilesegmenter command, but probably it will work anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文