当前位置: 首页 > 工具软件 > PVS.AVPlayer > 使用案例 >

AvPlayer与AVPlayerItem

司空俊雄
2023-12-01

1.需要倒入(AVFoundation.framework)he (CoreMedia.framework)


2.重写UIView

#import<UIKit/UIKit.h>

@interface MediaView :UIView

@end


#import"MediaView.h"

#import<AVFoundation/AVFoundation.h>


@implementation MediaView


+ (Class)layerClass//重写改变层,是为了把player加到layer上

{

   return [AVPlayerLayerclass];

    

}

@end


#import<UIKit/UIKit.h>

#import<AVFoundation/AVFoundation.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>


@property (strong,nonatomic) UIWindow *window;

@property (retain,nonatomic) AVPlayerItem *playerItem;

@property (retain,nonatomic) AVPlayer *player; //AVPlayer播放每一个AVPlayerItem


#import "AppDelegate.h"

#import "MediaView.h"

@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [_playerItem release];

    [_player release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    

    NSString *path = [[NSBundlemainBundle] pathForResource:@"bsm"ofType:@"mp4"];

    NSLog(@"%@",path);

    

    _playerItem = [[AVPlayerItemalloc]initWithURL:[NSURLfileURLWithPath: path]]; //本地的用fileURLWithPath

    

    _player = [[AVPlayeralloc]initWithPlayerItem:_playerItem];

    

    MediaView *mediaView = [[MediaViewalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    AVPlayerLayer *layer = (AVPlayerLayer*)[mediaViewlayer];

    [layer setPlayer:_player]; //把层与player关联起来(即把player加到layer中)

    [_player play];

    

    [self.windowaddSubview:mediaView];

    [mediaView release];

    

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}

@end
 类似资料: