在设备上测试应用程序时 MPMoviePlayer 声音下降

发布于 2024-10-11 00:32:07 字数 2460 浏览 3 评论 0原文

我正在构建一个应用程序,其中包含带有电影链接的 XML 提要,我希望在用户单击图像时播放该电影。

为此,我使用 MPMoviePlayerViewController。到目前为止,在模拟器上测试该应用程序已经给了我预期的结果,但是,当我在 iPhone 上测试该应用程序时,播放器可以正常播放,但没有声音。

我在互联网上查看了一些文章,告诉我该应用程序的性能与模拟器为您提供的性能有很大不同。因此,我用 Instruments 进行了“泄漏”测试,它告诉我,每当我开始播放电影时,一些字节就会丢失或泄漏。难道这和声音突然减弱有关系吗?如果是这样,我该如何解决?

这是我第一次在 iPhone 上测试应用程序,所以看到应用程序的表现如此糟糕,我感到非常震惊。这是电影播放器​​部分的代码。

#pragma mark Video Controls
-(void)playVideo:(id)sender{
    moviePlaying=TRUE;
    MPMoviePlayerViewController *playerViewController;
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[data objectForKey:@"[att]url"]]];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:[playerViewController moviePlayer]];
    MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] init] autorelease];
    player = [playerViewController moviePlayer];

    [self.view addSubview:playerViewController.view];
    player.controlStyle = MPMovieControlStyleDefault;
    player.shouldAutoplay = YES;
    [player setFullscreen:YES animated:YES];
} 

- (void)moviePlayBackDidExitFullscreen:(NSNotification*)notification{
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    moviePlaying=FALSE;
    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
#pragma mark VideoControls End

我希望你能阐明这个问题。

提前致谢,

/Brinck10

I'm building an application containing a XML feed with a link to a movie, which I would like to play whenever the user clicks on an image.

For that purpose I'm using the MPMoviePlayerViewController. Testing the app on the simulator has given me the expected results so far, however, when I test the app on an iPhone, the player plays correctly but with no sound.

I've looked on the internet where a few articles told me that the app's performance differs a lot from the performance the simulator gives you. Therefore I made a "leak" test with Instruments, which told me that whenever I started playing a movie, some bytes got dropped or leaked. Could that have something to do with the sudden drop of sound? If so, how would I solve it?

It's my first experience of testing apps on iPhone, so I got pretty shocked seeing how badly the app performed. Here's the code for the movieplayer-part.

#pragma mark Video Controls
-(void)playVideo:(id)sender{
    moviePlaying=TRUE;
    MPMoviePlayerViewController *playerViewController;
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[data objectForKey:@"[att]url"]]];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:[playerViewController moviePlayer]];
    MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] init] autorelease];
    player = [playerViewController moviePlayer];

    [self.view addSubview:playerViewController.view];
    player.controlStyle = MPMovieControlStyleDefault;
    player.shouldAutoplay = YES;
    [player setFullscreen:YES animated:YES];
} 

- (void)moviePlayBackDidExitFullscreen:(NSNotification*)notification{
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerViewController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    moviePlaying=FALSE;
    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}
#pragma mark VideoControls End

I hope you can light up the problem.

Thanks in advance,

/Brinck10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-10-18 00:32:07

你在这里遇到了一些非常严重的困惑。您不应同时创建 MPMoviePlayerViewController 和 MPMoviePlayerController。使用其中之一。这些行没有任何意义:

MPMoviePlayerController *player = 
    [[[MPMoviePlayerController alloc] init] autorelease];
player = [playerViewController moviePlayer];

您正在创建一个 MPMoviePlayerController 并将其分配给播放器,但随后将其丢弃,将其替换为现有 MPMoviePlayerViewController 的 moviePlayer。

我们退后十码并弃踢吧。播放电影/流声音/任何内容的简单方法是使用 MPMoviePlayerViewController。它是一个视图控制器,因此只需将其推到要使用 UIViewController 的位置即可。最简单的是以模态方式显示它:

NSURL* m = // whatever;
MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];
[self presentModalViewController:mpvc animated:YES];
[mpvc release];

这将创建 MPMoviePlayerController 及其视图,并仅在全屏模式下为您呈现整个 shebang。简单又容易。当用户点击“完成”时,模式视图甚至会被关闭。

使用 MPMoviePlayerController 更复杂。它不是 UIViewController。但它有一个观点。如果您希望用户看到任何内容,您的工作就是抓取该视图,给它一个框架,然后将其粘贴到您的界面中:

NSURL* m = // whatever;
MPMoviePlayerController* mp = 
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // property with retain policy
[mp release];
self.mpc.view.frame = CGRectMake(10, 10, 300, 230);
[self.view addSubview:self.mpc.view];

这使得电影视图成为您界面的一部分。如果电影是真实的电影,则用户可以自由切换是否进入全屏模式。如果电影只是声音,用户就没有办法做到这一点;只有一个停止/运行按钮和一个滑块。

无论哪种方式,故事中的某个地方都会有一个 MPMoviePlayerController,因此您可以注册它的通知、告诉它播放/停止等。但不要混淆或融合这两种方法。这可能是您这里所有问题的原因。

You've got some very serious confusion here. You should not be creating both an MPMoviePlayerViewController and an MPMoviePlayerController. Use one or the other. And these lines make no sense:

MPMoviePlayerController *player = 
    [[[MPMoviePlayerController alloc] init] autorelease];
player = [playerViewController moviePlayer];

You are creating a MPMoviePlayerController and assigning it to player but then throwing it away, replacing it your existing MPMoviePlayerViewController's moviePlayer.

Let's drop back ten yards and punt. The easy way to play a movie / stream sound / whatever is to use MPMoviePlayerViewController. It is a view controller, so just shove it where you would use a UIViewController. Simplest is to show it modally:

NSURL* m = // whatever;
MPMoviePlayerViewController* mpvc = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: m];
[self presentModalViewController:mpvc animated:YES];
[mpvc release];

This creates the MPMoviePlayerController and its view and presents the whole shebang for you in fullscreen mode only. Simple and easy. The modal view is even dismissed for you when the user taps Done.

Using MPMoviePlayerController is more involved. It is NOT a UIViewController. But it has a view. Your job, if you want the user to see anything, is to grab that view, give it a frame, and stick it into your interface:

NSURL* m = // whatever;
MPMoviePlayerController* mp = 
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // property with retain policy
[mp release];
self.mpc.view.frame = CGRectMake(10, 10, 300, 230);
[self.view addSubview:self.mpc.view];

This makes the movie view part of your interface. If the movie is a real movie, it leaves the user free to toggle into fullscreen mode or not. If the movie is just a sound, the user doesn't have a way to do that; there's just a stop/go button and a slider.

Either way there is an MPMoviePlayerController somewhere in the story so you can register for its notifications, tell it to play/stop, etc. But don't confuse or meld the two approaches. That's probably the cause of all your issues here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文