1

各セルが異なるビデオを持つ MPMoviePlayerController を保持する UICollectionView を構築しようとしています。ユーザーが各ビデオをその場で再生できるようにしたい。

MPMoviePlayerController の複数のインスタンスを再生できないことはわかっています。Apple のドキュメントについて漠然としているのは、それらをインスタンス化してビューに配置できるかどうかです。私が読んだ方法では、実際にそれを行うことができます。しかし、それがうまくいかなかったので、最終的にあきらめて別のテクニックを試しました。

代わりに、選択イベントを使用して、コレクション セル内でビデオをインスタンス化し、配置し、自動的に再生しようとしました。コントロールとして、同じ UIViewController 内のコレクション ビューの外に追加のビューを配置しました。実行すると、外側のビューが正常に読み込まれました。ビデオがインスタンス化され、最初のフレームが表示されました。また、コレクション ビューのすべてのセルも正しく表示されました。ただし、コレクション セルの 1 つを選択すると、外側のビューのビデオは完全に消え、コレクション ビューのビデオはインスタンス化されましたが表示されませんでした。各ビューの背景を色分けし、セルを選択すると、ビデオの背景が表示されましたが、実際のビデオは表示されなかったため、インスタンス化されたことがわかります。非常に紛らわしいです。

では、コレクション ビュー内にビデオを配置することさえできるのでしょうか?

ビューコントローラーのコードは次のとおりです。

#import "testViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "tvCollectionCell.h"

@interface testViewController ()

@property (strong) IBOutlet UIView *containerView;
@property (strong) MPMoviePlayerController *videoPlayer;

@end

@implementation testViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    self.videoPlayer = [[MPMoviePlayerController alloc]
                        initWithContentURL: [NSURL fileURLWithPath:
                                             [[NSBundle mainBundle]
                                              pathForResource:@"testvideo" ofType:@"mp4"]]];

    CGRect videoFrame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);

    [[self.videoPlayer view] setFrame:videoFrame];

    self.videoPlayer.shouldAutoplay = FALSE;
    self.videoPlayer.repeatMode = TRUE;
    [self.videoPlayer prepareToPlay];
    [self.videoPlayer view].backgroundColor = [UIColor orangeColor];

    [self.containerView addSubview:[self.videoPlayer view]];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    tvCollectionCell *tvCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tvCell" forIndexPath:indexPath];
    tvCell.backgroundColor = [UIColor purpleColor];

    return tvCell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    tvCollectionCell *tvCell = (tvCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];

    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
                                      initWithContentURL: [NSURL fileURLWithPath:
                                                           [[NSBundle mainBundle]
                                                            pathForResource:@"testvideo" ofType:@"mp4"]]];

    CGRect videoFrame = CGRectMake(0, 0, tvCell.frame.size.width, tvCell.frame.size.height);

    [[movie view] setFrame:videoFrame];

    movie.shouldAutoplay = TRUE;
    movie.repeatMode = TRUE;
    [movie prepareToPlay];
    [movie view].backgroundColor = [UIColor blueColor];

    [tvCell addSubview:[movie view]];

}

@end

助けてくれてありがとう!

4

1 に答える 1

0

すべてのビデオへの参照があることを確認していますか? MPMoviePlayerController 別名 videoPlayer の唯一のプロパティが上書きされている可能性があります。配列を割り当てて、所有している各ビデオへの参照を保持し、その参照を取得してそのビデオ コントローラーを再生することができます。

于 2013-04-28T10:53:50.133 に答える