为什么不在视图中初始化两个MPMoviePlayerController?

时间:2014-08-23 16:10:02

标签: ios mpmovieplayercontroller

系统:iOS 7.0

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *docPaths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *audioPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"test.mp4"];

if (YES == [fileManager fileExistsAtPath:audioPath])
{
    MPMoviePlayerController *moviePlayer = nil;

    //1
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
    [moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [moviePlayer setControlStyle: MPMovieControlStyleEmbedded];
    [moviePlayer.view setBackgroundColor:[UIColor clearColor]];
    [moviePlayer.view setFrame: CGRectMake(20, 240, 80, 80)];
    moviePlayer.shouldAutoplay = NO;
    [moviePlayer prepareToPlay];
    self.abcd = moviePlayer;
    [self.view addSubview: moviePlayer.view];

    //2
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
    [moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [moviePlayer setControlStyle: MPMovieControlStyleEmbedded];
    [moviePlayer.view setBackgroundColor:[UIColor clearColor]];
    [moviePlayer.view setFrame: CGRectMake(20, 40, 80, 80)];
    moviePlayer.shouldAutoplay = NO;
    [moviePlayer prepareToPlay];
    self.edf = moviePlayer;
    [self.view addSubview: moviePlayer.view];

}

当我初始化两个MPMoviePlayerControllers时,为什么它只显示一个?如果我只是初始化#1,它没问题,如果只是初始#2,它也可以,但是如果我同时初始化#1和#2,它只显示#2。为什么会这样?感谢。

2 个答案:

答案 0 :(得分:2)

你能详细说明你的意思吗?它只会"显示"一?您可以添加两个视图,但根据Apple的文档:

  

注意:虽然您可以创建多个MPMoviePlayerController对象并在界面中显示其视图,但一次只能有一个电影播放器​​可以播放其电影。

来源:https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

答案 1 :(得分:1)

编辑:看到肖恩的答案,这可能就是问题所在。

您需要创建两个不同的MPMoviePlayerController实例。你正在做的是创建一个名为moviePlayer的MPMoviePlayerController实例,然后在创建第二个时覆盖它。您想要创建两个唯一的实例,例如:

MPMoviePlayerController *moviePlayer1 = nil;
MPMoviePlayerController *moviePlayer2 = nil;

//1
moviePlayer1 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer1 setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer1 setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer1.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer1.view setFrame: CGRectMake(20, 240, 80, 80)];
moviePlayer1.shouldAutoplay = NO;
[moviePlayer1 prepareToPlay];
self.abcd = moviePlayer1;
[self.view addSubview: moviePlayer1.view];

//2
moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer2 setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer2 setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer2.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer2.view setFrame: CGRectMake(20, 40, 80, 80)];
moviePlayer2.shouldAutoplay = NO;
[moviePlayer2 prepareToPlay];
self.edf = moviePlayer2;
[self.view addSubview: moviePlayer2.view];

你正在做的另一个例子是:

NSString *string = nil; // first string is nil
string = @"red"; // then you create another string "red" and set it to string
string = @"blue"; // then you create another string "blue" and set it to string

无论你做什么,在这个字符串之后总是“蓝色”,“红色”基本消失。