iPhone或iPad可以支持窗口内的原生视频播放,而不是全屏吗?

时间:2014-05-30 22:04:39

标签: iphone

我需要一个视频,能够在横向播放底部的描述,然后当手机切换方向时,它将全屏显示。这基本上是Youtube应用程序如何适用于大多数Androids,(以三星Galaxy S4为例)。但我只想播放已经在应用上的视频,没有在线,没有网络。事实上,我的应用会记录并保存自己的视频。

如果可以支持,那么我可以使用诸如某种视图之类的对象吗?

1 个答案:

答案 0 :(得分:0)

您可能会发现我用来显示视频的代码很有帮助。我从我的主视图控制器调用它。在displayWithVideo部分中,我为不同的方向和设备设置了视频大小。

有一种文本方法可以在视频下方显示文本,还有一些清理方法可以在旋转或滑动到下一个屏幕时从屏幕上删除对象。我有一段时间没有看过它,但它应该是iOS7准备就绪。

   //
    //  ShowVideo.m
    //  Words for SLPs
    //
    //  Created by John Scarry on 7/26/13.
    //  Copyright 2013 Learning Fundamentals, Inc. All rights reserved.
    //

    @import MediaPlayer;
    #import "ShowVideo.h"

    @implementation ShowVideo

    // Needs to know about the parentView so it can decide where to put the picts/buttons
    - (id)initWithParentView:(UIView *)parentview  {

        self = [super init];
        if (self) {
            _parentView = parentview;
        }
        return self;
    }

    - (void)displaywithVideo:(NSString *)videoName
                    withText:(NSString *)videoText
          withSwipeDirection:(NSString *)swipeDirection {

        [self removeAllObjectsFromParentView];

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        CGPoint midPoint;
        midPoint.x = self.parentView.bounds.origin.x + self.parentView.bounds.size.width/2;
        midPoint.y = self.parentView.bounds.origin.y + self.parentView.bounds.size.height/2;
        // Put the word and category on the screen    
        NSInteger xDirection = 1; // Put the new picture off the screen then transition in. 1 is rightToLeft, -1 is leftToRight
        if ([swipeDirection isEqualToString:@"leftToRight"]) xDirection = -1;

        //Pictures are rectangular - 400 x 300. Display 90% the width of the screen
        CGFloat pictRatio = .75; 

        CGSize windowSize =  self.parentView.bounds.size;
        CGFloat verticalOffset = 20;
        #pragma unused(verticalOffset)
        if ([[Utilities deviceType] isEqualToString:@"iPhone Retina4"]) verticalOffset = 100;

        CGFloat insetValue = 2;
        CGFloat pictWidth = 320;
        CGFloat pictHeight = pictWidth * pictRatio + (insetValue * 2); // 960/1280 or 498/664 on Picts for SLPs
        pictWidth = pictWidth + (insetValue * 2);
        UIEdgeInsets insets;
        insets.top = insets.bottom = insets.left = insets.right = insetValue;

        NSString *backGroundFrameName = @"ArticTypeFrame";
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            verticalOffset = 110;
            pictWidth = windowSize.width * .9f;
            pictHeight = pictWidth *pictRatio; // 960/1280 is best size
            insets.top = insets.bottom = insets.left = insets.right = 6;

            if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
                pictWidth = windowSize.width * .55f;
                pictHeight = pictWidth *pictRatio; // 960/1280
                verticalOffset = 30;
            }

        // iPhone
        } else {
            if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
                pictWidth = windowSize.width * .3f;
                pictHeight = pictWidth *pictRatio; // 960/1280
                verticalOffset = 10;
            } else {
                verticalOffset = 60;
            }

        }

        CGFloat finalX = midPoint.x - pictWidth/2.0f;
        self.videoFrame = CGRectMake(xDirection*(self.parentView.bounds.size.width + pictWidth), verticalOffset, pictWidth, pictHeight);
        //NSLog(@"The  pictWidth is %u and height %u", pictWidth, pictHeight);

        // Put the video on the screen.
        NSURL *videoURL = [[NSBundle mainBundle] URLForResource:videoName withExtension:@"m4v"];
        self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
        self.videoFrame = CGRectMake(finalX, verticalOffset, pictWidth, pictHeight);

        [self.videoPlayer.view setFrame:self.videoFrame];
        self.videoPlayer.controlStyle = MPMovieControlStyleNone;
        self.videoPlayer.shouldAutoplay = YES;
        [self.parentView addSubview:self.videoPlayer.view];
        [self.videoPlayer prepareToPlay];
        [self.videoPlayer play];

        // Add a button for replay and the frame
        self.pictButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.pictButton.opaque = NO;
        self.pictButton.contentEdgeInsets = insets;
        [self.pictButton setBackgroundImage:[UIImage imageNamed:backGroundFrameName] forState:UIControlStateNormal];
        self.pictButton.showsTouchWhenHighlighted = NO; //Don't want the glow effect on text

        [self.pictButton setFrame:self.videoFrame];
        [self.pictButton addTarget:self
                            action:@selector(videoTapped:)
                  forControlEvents:UIControlEventTouchDown];

        [self.parentView addSubview:self.pictButton];

        // The text is placed below the pict so needs to be put on the screen after the pict
        if ( [swipeDirection isEqualToString:@"rotated"] ) {
            [self.textView removeFromSuperview];
            self.textView = nil;
        }
        if (!self.textView) {
            [self createTextFrame];
            [self showTextWithText:videoText];
        } else {
            [self.textView loadHTMLString:@" " baseURL:nil];
        }
    }

    - (void)videoTapped:(UIButton *)buttonPressed {

        id <ShowVideoDelegate> SV_delegate = _delegate;
        [SV_delegate stopSounds];

        [self.videoPlayer prepareToPlay];
        [self.videoPlayer play];
    }

    #pragma mark - Put the text on the screen

    - (void)createTextFrame {

        CGFloat textHeight = 100;
        CGFloat textX = self.parentView.frame.origin.x + self.view.frame.size.width * .05f;
        CGFloat textY = self.videoFrame.origin.y + self.videoFrame.size.height;
        CGFloat textWidth = self.parentView.frame.size.width * .9f;

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
            textY = textY + 20;
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                textHeight = textHeight * 1.5f;
                textX = textX + 30;
            } else {
                textHeight = 80; // Full-size text block covers over the scoring
            }
        // Portrait
        } else {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                textHeight = textHeight * 1.5f;
                textY = textY + 20;
            } else {
                textHeight = textHeight * 1.4f;
                textWidth = self.parentView.frame.size.width;
                textX = 0;
            }
        }
        textWidth = ceilf(textWidth); // Need an integer or else there is a | at the end of the textView
        CGRect textFrame = CGRectMake(textX, textY, textWidth, textHeight);
        self.textView = [[UIWebView alloc] initWithFrame:textFrame];
        self.textView.opaque = YES;
        self.textView.userInteractionEnabled = NO;

        [self.parentView insertSubview:self.textView atIndex:0];
    }

    - (void)showTextWithText:(NSString *)textToDisplay {

        NSInteger textFontSize = 14;
        NSString *textFontStyle = [NSString stringWithFormat:@"normal"];

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            textFontStyle = [NSString stringWithFormat:@"bold"];
            textFontSize = 30;
        }
        NSString *formattedText = nil;
        if ( [Globals sharedInstance].showText ) {
            formattedText = [NSString stringWithFormat:@"<body><style type='text/css'>span.s1 {color:#808080}span.s2 {color: #880000}span.s3 {color: #005500}</style><p style='font-family:sans-serif;text-align:center;font-size:%u;font-weight:%@;'>%@</p></body>", textFontSize, textFontStyle, textToDisplay];
        }
        [self.textView loadHTMLString:formattedText baseURL:nil];
    }

    - (void)removeAllObjectsFromParentView {
        [self.textView removeFromSuperview];
        [self.pictButton removeFromSuperview];

        self.videoPlayer = nil;
        self.textView = nil;
    }

    - (void)didReceiveMemoryWarning {
        self.videoPlayer = nil;
        self.textView = nil;

        [super didReceiveMemoryWarning];
    }

    @end

这是.h文件。

    //
//  ShowVideo.h
//  Words for SLPs
//
//  Created by John Scarry on 7/26/2013.
//  Copyright 2013 Learning Fundamentals, Inc. All rights reserved.
//

#import "Word.h"

@protocol ShowVideoDelegate <NSObject>
@required
- (void)stopSounds;
@end

@interface ShowVideo : UIViewController

@property (nonatomic, strong) Word *word;
@property (nonatomic, strong) UIView *parentView;

@property (nonatomic, strong) UIImage *videoToDisplay;
@property (nonatomic, strong) MPMoviePlayerController *videoPlayer;
@property (nonatomic) CGRect videoFrame;
@property (nonatomic, strong) UIButton *pictButton;

@property (nonatomic, strong) UIWebView *textView;

@property (nonatomic, weak) id <ShowVideoDelegate> delegate;

- (id)initWithParentView:(UIView *)parentview;

- (void)displaywithVideo:(NSString *)videoName
                withText:(NSString *)videoText
      withSwipeDirection:(NSString *)swipeDirection;

- (void)videoTapped:(UIButton *)buttonPressed;

- (void)createTextFrame;

- (void)showTextWithText:(NSString *)textToDisplay;

- (void)removeAllObjectsFromParentView;
@end