iPhone SdK:添加音轨后 AVPlayer 将无法播放作品

发布于 2024-10-20 10:44:02 字数 1594 浏览 8 评论 0原文

将音轨(使用 AVAudioRecorder 录制)添加到合成后,AVPlayer 将无法播放它。 AVPlayer.status = 1 且 AVPlayer.error = null。

我看过 WWDC10 AVEdit 演示代码,它非常相似。会不会是音频格式不兼容?

原始作品已包含视频资产中的视频和音频轨道。

删除新添加的音轨后,播放器仍然无法播放 删除原始音轨后,播放器将播放。

我还发现很难获得可以指出问题的调试信息。如果有人有建议,我们将不胜感激。

这是我添加音轨的代码,

非常欢迎任何帮助

Jean-Pierre

-(void)editAudioViewController:(EditAudioViewController *)editAudioViewController didEditAudio:(NSURL *)url
{
[self dismissModalViewControllerAnimated:TRUE];

BOOL result;
NSError * error = nil;

if (url)
{
    AVURLAsset * asset = [AVURLAsset URLAssetWithURL:url options:nil] ;

    CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero , asset.duration);
    if (CMTIME_COMPARE_INLINE(CMTimeRangeGetEnd(audioTimeRange), >, [composition duration]))
        audioTimeRange.duration = CMTimeSubtract([composition duration], audioTimeRange.start);

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                    preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *audioTrack = [asset compatibleTrackForCompositionTrack:compositionAudioTrack];

    result = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioTimeRange.duration) ofTrack:audioTrack atTime:audioTimeRange.start error:&error];

    if (!result)
    {
        NSLog(@"%@", [error description]);
    }

    [mp replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];
    [mp seekToTime:kCMTimeZero];

}
}

After adding an audio track (recorded with AVAudioRecorder) to a composition AVPlayer will not play it. AVPlayer.status = 1 and AVPlayer.error = null.

I have looked at the WWDC10 AVEdit demo code and it's pretty much similar. Could it be an audio format incompatibility?

the original composition allready contains a video and audio track from a video asset.

after removing the newly added audio track the player still does not play
after removing the original audio track the player will play.

I'm also finding it hard to get debug information that would point me to the problem. If anyone has a suggestion it would be greatly appreciated.

Here is my code for adding the audio track

any help is most welcome

Jean-Pierre

-(void)editAudioViewController:(EditAudioViewController *)editAudioViewController didEditAudio:(NSURL *)url
{
[self dismissModalViewControllerAnimated:TRUE];

BOOL result;
NSError * error = nil;

if (url)
{
    AVURLAsset * asset = [AVURLAsset URLAssetWithURL:url options:nil] ;

    CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero , asset.duration);
    if (CMTIME_COMPARE_INLINE(CMTimeRangeGetEnd(audioTimeRange), >, [composition duration]))
        audioTimeRange.duration = CMTimeSubtract([composition duration], audioTimeRange.start);

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                    preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *audioTrack = [asset compatibleTrackForCompositionTrack:compositionAudioTrack];

    result = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioTimeRange.duration) ofTrack:audioTrack atTime:audioTimeRange.start error:&error];

    if (!result)
    {
        NSLog(@"%@", [error description]);
    }

    [mp replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];
    [mp seekToTime:kCMTimeZero];

}
}

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

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

发布评论

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

评论(1

我ぃ本無心為│何有愛 2024-10-27 10:44:02

我的录音机代码中有两个单独的会话,一个用于录音,一个用于播放。
我切换到单个录制/播放会话,这以某种方式解决了问题:

audioSession = [AVAudioSession sharedInstance];

if (!audioSession.inputIsAvailable)
{
    NSLog(@"No input available");
}

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

NSError * error = nil;

[audioSession setActive:TRUE error:&error];

if (error)
{
    NSLog(@"%@", [error description]);
}

I had two separate sessions in my audio recorder code one for recording and one for playback.
I switched to a single record/play session and this somehow fixed the problem:

audioSession = [AVAudioSession sharedInstance];

if (!audioSession.inputIsAvailable)
{
    NSLog(@"No input available");
}

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

NSError * error = nil;

[audioSession setActive:TRUE error:&error];

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