iOS 中流畅的视频循环

发布于 2024-12-11 05:17:07 字数 334 浏览 1 评论 0原文

谁能建议一种方法,可以在 iOS 中实现完全流畅、无缝的视频剪辑循环?我尝试了两种方法,这两种方法都会在视频循环时产生一个小暂停

1) AVPlayerLayer ,其中playerItemDidReachEnd通知设置为seekToTime:kCMTimeZero

我更喜欢使用AVPlayerLayer(出于其他原因),但此方法会产生大约循环之间有一秒钟。

2) MPMoviePlayerController 与 setRepeatMode:MPMovieRepeatModeOne

这样会导致停顿较小,但仍然不完美。

我不知道从这里该去哪里。有人能建议一个灵魂吗?

Can anyone suggest a method by which you can achieve a completely smooth and seamless looping of a video clip in iOS? I have tried two methods, both of which produce a small pause when the video loops

1) AVPlayerLayer with the playerItemDidReachEnd notification setting off seekToTime:kCMTimeZero

I prefer to use an AVPlayerLayer (for other reasons), but this method produces a noticeable pause of around a second between loops.

2) MPMoviePlayerController with setRepeatMode:MPMovieRepeatModeOne

This results in a smaller pause, but it is still not perfect.

I'm not sure where to go from here. Can anyone suggest a soultion?

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

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

发布评论

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

评论(5

⒈起吃苦の倖褔 2024-12-18 05:17:07

我同意@SamBrodkin 的发现。

[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(myMovieFinishedCallback:)
    name: MPMoviePlayerPlaybackStateDidChangeNotification
    object: m_player];

-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
    NSLog( @"myMovieFinishedCallback: %@", aNotification );
    MPMoviePlayerController *movieController = aNotification.object;
    NSLog( @"player.playbackState = %d", movieController.playbackState );
}

为我解决了 iOS 5 上的非循环问题。

I can concur @SamBrodkin's findings.

[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(myMovieFinishedCallback:)
    name: MPMoviePlayerPlaybackStateDidChangeNotification
    object: m_player];

and

-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
    NSLog( @"myMovieFinishedCallback: %@", aNotification );
    MPMoviePlayerController *movieController = aNotification.object;
    NSLog( @"player.playbackState = %d", movieController.playbackState );
}

fixed the non-looping issue on iOS 5 for me too.

梓梦 2024-12-18 05:17:07

我刚刚在运行 iOS 5.1.1、基本 SDK iOS 5.1 的 iPad 3 上运行此功能

设置电影播放器​​时,将重复模式设置为 MPMovieRepeatModeNone
然后添加通知

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer];

然后设置您的选择器以在电影播放完毕时进行过滤

- (void)moviePlayerDidFinish:(NSNotification *)note {
    if (note.object == self.moviePlayer) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            [self.moviePlayer play];
        }
    }
}

Apple 在从 iOS 4 更改为 iOS 5 时对 MPMoviePlayerController 处理加载电影文件的方式进行了一些重大更改,所以我不知道此方法在什么情况下是否有效他们发布了 iOS 6

I just got this working on my iPad 3 running iOS 5.1.1, base SDK iOS 5.1

When setting up the movie player, set the repeat mode to MPMovieRepeatModeNone
then add the notification

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer];

Then set up your selector to filter when the movie finishes playing

- (void)moviePlayerDidFinish:(NSNotification *)note {
    if (note.object == self.moviePlayer) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            [self.moviePlayer play];
        }
    }
}

Apple made some large changes to how the MPMoviePlayerController handles loading movie files when they changed from iOS 4 to iOS 5, so I do not know if this method will work when they release iOS 6

比忠 2024-12-18 05:17:07

要查看显示背景视频(来自动画 GIF)的无缝循环以及一对前景角色动画(带有 Alpha 通道)之间切换的示例,请查看 ios 上的无缝视频循环。我过去尝试过使用 AVPlayer,但不得不放弃 AVPlayer 相关的解决方案,因为它们效果不够好。另请参阅这个问题 iphone-smooth-transition-from-one-视频到另一个

To see an example that shows seamless looping of a background video (from an animated GIF) and switching between a pair of foreground character animations (with alpha channel), have a look at seamless-video-looping-on-ios. I tried to use AVPlayer in the past and had to give up on AVPlayer related solutions as they did not work well enough. See this SO question also iphone-smooth-transition-from-one-video-to-another.

木森分化 2024-12-18 05:17:07

我调查了与原始海报报告的相同问题(循环中的小停顿破坏了无缝性)。幸运的是,我有另一个没有这种行为的视频样本,直到后来才找到解释/不同:

音轨。

我怀疑声音初始化非常慢(路由?)。

删除音轨对我来说是最简单的解决方案(不需要声音),但我必须进一步挖掘(音频混合选项并测试已在此线程中发布的解决方案)。

埃里克

I investigated the same problem as reported by the original poster (small pause in the loop that was breaking the seamlessness). By luck I had another video sample that didn't had this behavior and found the explanation/different only later:

The sound track.

I suspect a very slow sound initialisation (routing?).

Removing the sound track was the easiest solution for me (no sound needed) but I will have to dig further (audio mixing options and testing the solution that have been posted in this thread).

Eric

菩提树下叶撕阳。 2024-12-18 05:17:07

为了避免视频倒带时出现间隙,在合成中使用同一资源的多个副本对我来说效果很好。

AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
for (int i = 0; i < 100; i++) { // Insert some copies.
    [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];
}
AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];

To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me.

AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
for (int i = 0; i < 100; i++) { // Insert some copies.
    [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];
}
AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文