iOS 用 Xcode
私が抱えている問題は、AVMutableComposition の再生にあります。コンポジションを作成してから、for ループ内に AVURLAssets を追加しています。同様に可変配列にオブジェクトを追加し、後でそれを再生しようとするのと同じように、すべてを追加していますが、再生は行われません。これが私のコードです:
AVMutableComposition *composition = [AVMutableComposition composition];
int count;
count = array.count;
for (int y=0;y<count;y++){
NSURL *url;
url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],[array objectAtIndex:y]]];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];
//calculate times
NSNumber *time = [soundArray1 objectAtIndex:1];
double timenow = [time doubleValue];
double insertTime = (240*y);
CMTime douTime = CMTimeMake(240, timenow);
CMTime staTime = CMTimeMake(0, 1);
CMTimeRange editRange = CMTimeRangeMake(staTime,douTime);
CMTime insTime = CMTimeMake(insertTime, timenow);
NSError *editError;
[composition insertTimeRange:editRange ofAsset:sourceAsset atTime:insTime error:&editError];
}
AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];
[mp play];
私はそれを完全に間違っていると思いますが、これを後ではなくループ内に追加し、if 内で NSRunLoop を開いて確認しました (少し悪いですが、テストするだけでした!):
AVMutableComposition *composition = [AVMutableComposition composition];
int count;
count = array.count;
for (int y=0;y<count;y++){
NSURL *url;
url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],[array objectAtIndex:y]]];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];
//calculate times
NSNumber *time = [soundArray1 objectAtIndex:1];
double timenow = [time doubleValue];
double insertTime = (240*y);
CMTime douTime = CMTimeMake(240, timenow);
CMTime staTime = CMTimeMake(0, 1);
CMTimeRange editRange = CMTimeRangeMake(staTime,douTime);
CMTime insTime = CMTimeMake(insertTime, timenow);
NSError *editError;
[composition insertTimeRange:editRange ofAsset:sourceAsset atTime:insTime error:&editError];
if(y==3){
AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];
[mp play];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20]];
}
}
ここでは、配列に 4 つの要素を指定し、4 回目の実行でコンポジションを再生するようにしました。それは機能し、4 つのオーディオ ファイルを完璧な順序で再生しました。
このループではなく、このループの後に作曲を再生するにはどうすればよいですか? システムを誤って使用していませんか?
編集: 開いている RunLoop コードを最後に貼り付けることで、再生されます。このようにする必要がありますか、それとももっと良い方法がありますか?