ios - 使用 AVMutableComposition iPhone
<p><p>我正在使用下面的代码,按顺序流式传输两个视频。但它没有在模拟器中显示任何视频,它完全是空白的。 </p>
<p>另外,我该如何通过这两个视频进行搜索。比如,如果一个视频是 2 分钟,第二个是 3 分钟。现在我需要获取这些视频的总时间并通过它们进行搜索。当我将 slider 滑动到 4 分钟时,第二个视频应该从第 2 分钟开始播放。</p>
<p>有可能吗?</p>
<pre><code>- (void)viewDidLoad
{
;
// Do any additional setup after loading the view, typically from a nib.
NSURL *url1 = ;
NSURL *url2 = ;
NSDictionary *options = forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVMutableComposition *composition = [ init];
asset1 = [ initWithURL:url1 options:options];
AVURLAsset * asset2 = [initWithURL:url2 options:options];
CMTime insertionPoint = kCMTimeZero;
NSError * error = nil;
composition = ;
if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset1.duration)
ofAsset:asset1
atTime:insertionPoint
error:&error])
{
NSLog(@"error: %@",error);
}
insertionPoint = CMTimeAdd(insertionPoint, asset1.duration);
if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset2.duration)
ofAsset:asset2
atTime:insertionPoint
error:&error])
{
NSLog(@"error: %@",error);
}
AVPlayerItem * item = [ initWithAsset:composition];
player = ;
AVPlayerLayer * layer = ;
;
[[ layer] addSublayer:layer];
;
}
</code></pre>
<p>谁能告诉我我的代码有什么错误?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>模拟器无法显示视频。内置的 UIImagePickerController 和任何视频 Controller 都不会工作。它没有实现,在 iOS 模拟器上大多显示为黑色或红色。您必须在 iOS 目标上进行调试。有时调试将无法正常工作。使用 NSLog() 代替。这将始终有效(即,如果您使用“发布”代码在没有调试信息的情况下进行编译)</p>
<p>您可以使用播放器进行搜索:</p>
<p>如果 mp 是您的媒体播放器:</p>
<pre><code>;
CMTime position = mp.currentTime;
// maybe replace something
];
;
;
</code></pre>
<p>总结:<br/>
编辑:使用合成和播放器项目<br/>
寻找:使用播放器</p>
<p>这是一个简短的正式示例,说明如何执行此操作(并且已经是线程安全的):</p>
<pre><code>AVMutableComposition *_composition = ;
// iterate though all files
// And build mutable composition
for (int i = 0; i < filesCount; i++) {
AVURLAsset* sourceAsset = nil;
NSURL* movieURL = ];
sourceAsset = ;
// calculate time
CMTimeRange editRange = CMTimeRangeMake(CMTimeMake(0, 600), sourceAsset.duration);
NSError *editError;
BOOL result = [_composition insertTimeRange:editRange
ofAsset:sourceAsset
atTime:_composition.duration
error:&editError];
dispatch_sync(dispatch_get_main_queue(), ^{
// maybe you need a progress bar
self.loaderBar.progress = (float) i / filesCount;
;
});
}
// make the composition threadsafe if you need it later
self.composition = [ autorelease];
// Player wants mainthread?
dispatch_sync(dispatch_get_main_queue(), ^{
mp = initWithAsset:self.composition] autorelease]];
self.observer = [mp addPeriodicTimeObserverForInterval:CMTimeMake(60, 600) queue:nil usingBlock:^(CMTime time){
// this is our callback block to set the progressbar
if (mp.status == AVPlayerStatusReadyToPlay) {
float actualTime = time.value / time.timescale;
// avoid division by zero
if (time.value > 0.) {
CMTime length = mp.currentItem.asset.duration;
float lengthTime = length.value / length.timescale;
if (lengthTime) {
self.progressBar.value = actualTime / lengthTime;
} else {
self.progressBar.value = 0.0f;
}
}];
});
// the last task must be on mainthread again
dispatch_sync(dispatch_get_main_queue(), ^{
// create our playerLayer
self.playerLayer = ;
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame = .layer.bounds;
// insert into our view (make it visible)
[.layer insertSublayer:self.playerLayer atIndex:0];
});
// and now do the playback, maybe mp is global (self.mp)
// this depends on your needs
;
});
</code></pre>
<p>我希望这会有所帮助。</p></p>
<p style="font-size: 20px;">关于ios - 使用 AVMutableComposition iPhone,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10383493/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10383493/
</a>
</p>
页:
[1]