用 MediaPlayer Framework 中的 MPMoviePlayerController 構造一個最簡單的 M3U8 播放器。
創建一個空項目,然後改寫 AppDelegate:
#import
@class PlayerViewController;
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) PlayerViewController *vc;
@end
#import "AppDelegate.h"
#import "PlayerViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[PlayerViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = self.vc;
[self.window makeKeyAndVisible];
return YES;
}
...
@end
其他默認函數我就略了。
#import
@interface PlayerViewController : UIViewController
@end
在 viewDidLoad 中初始化 MPMoviePlayerController,並指定一個播放地址。這裡我寫死了地址,是為了演示。
#import "PlayerViewController.h"
#import
@interface PlayerViewController ()
@property (strong, nonatomic) MPMoviePlayerController *streamPlayer;
@end
@implementation PlayerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *streamURL = [NSURL URLWithString:@"http://www.thumbafon.com/code_examples/video/segment_example/prog_index.m3u8"];
self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
[self.streamPlayer.view setFrame:self.view.bounds];
self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview: self.streamPlayer.view];
[self.streamPlayer play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Run It !
-
轉載請注明來自:http://blog.csdn.net/prevention