iPhone Dev:MediaPlayer 在后续播放中不播放视频(仅音频)
我正在开发一个 iPhone 应用程序,它使用 MediaPlayer 来播放几个不同的视频。 它对于第一个视频效果很好,但当我尝试播放另一个视频时,屏幕保持黑色并且仅播放音频。 有谁知道为什么会发生这种情况?
这是我的代码:
-(NSURL *)movieURL
{
NSBundle *bundle = [NSBundle mainBundle];
if (bundle)
{
NSString *moviePath = [bundle pathForResource:vidName ofType:@"mov"];
if (moviePath)
mMovieURL = [NSURL fileURLWithPath:moviePath];
if (vidName == @"Vid01")
vidName = @"Vid02";
else if (vidName == @"Vid02")
vidName = @"Vid03";
}
return mMovieURL;
}
- (void)onHitButton1 {
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
[mMoviePlayer play];
}
I am working on an iPhone application that uses a MediaPlayer to play a couple different videos. It works great for the first video but when I try to play another the screen stays black and only the audio plays. Does anyone have any idea why this might be happening?
Here is my code:
-(NSURL *)movieURL
{
NSBundle *bundle = [NSBundle mainBundle];
if (bundle)
{
NSString *moviePath = [bundle pathForResource:vidName ofType:@"mov"];
if (moviePath)
mMovieURL = [NSURL fileURLWithPath:moviePath];
if (vidName == @"Vid01")
vidName = @"Vid02";
else if (vidName == @"Vid02")
vidName = @"Vid03";
}
return mMovieURL;
}
- (void)onHitButton1 {
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
[mMoviePlayer play];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你真的要去看其他视频吗?
我注意到 3.0 中的一个错误,如果您尝试从同一文件播放视频两次,您会得到一些奇怪的播放伪像。 上面的代码看起来只会有一个指向单个文件的 URL。
我的解决方法是重命名我的文件,但这意味着您必须将其复制到文档目录才能更改名称...也许您可以查看 iPhone 文件系统是否支持返回到该文件的符号或硬链接原始文件位于资源目录中,以便您可以为电影播放器提供不同的名称。
我就这个问题提交了 RADAR,你也应该这样做。 只需整理一个超级简单的示例即可显示哪些内容不起作用。
Are you really going to other videos?
I've noticed a bug in 3.0 that if you try to play a video twice from the same file you get some weird playback artifacts. The code you have above looks like it would only ever have a URL pointing to a single file.
The workaround I had was to rename my file, but that means you have to copy it to the documents directory in order to change the name... perhaps you could see if the iPhone file system would support a symbolic or hard link back to the original file in the resource directory so you could feed the movie player different names.
I filed a RADAR on this issue, you should too. Just put together a super simple example showing what is not working.
vidname 不应该在资源路径之前而不是之后更改吗?
不管怎样,最好将其编写为接受可以将文件名传递给的 NSString。 你知道像吗
?
我也不喜欢你缺少 {}。 这使我更容易阅读/调试。 一般来说,这是一个好习惯。 花额外的时间把东西写得尽可能整洁,这样以后你就不会再头疼了。
vidName 是否在某处设置为 Vid01? 我没看到所以我必须问一下。 一般来说,这很令人困惑。 我一直在为 OS X 编写代码,所以我不确定是否有任何差异,但是当我在当前项目中加载地图时我会使用它:
看起来你写的比你需要的多得多。
Shouldn't vidname be changing before the resource path instead of after?
Either way, it would be better to write it to accept an NSString which you could pass the filename to. You know like
?
Also I didn't like your lack of {}'s. This makes it much easier to read/debug for me. It's a good habit to have in general. Taking the extra time to write things as neat as you can will save you headaches later.
Is vidName being set to Vid01 somewhere? I didn't see it so I have to ask. This is just confusing in general. I've been writing for OS X, so I'm not sure if there are any differences, but I use this when I'm loading maps in my current project:
Just seems like you're writing way more than you need to be.
我想到了。 我需要在调用第二个视频之前释放 MediaPlayer。
代码示例:
I figured it out. I needed to release the MediaPlayer before calling the second video.
Code example:
您还应该注意,播放问题可能只发生在模拟器中,而不是手机上(例如后续播放“闪烁”或不显示图像)。
You should also note that the playback issues may only occur in the simulator, not on the phone (such as subsequent playback 'flashing' or not displaying an image).