0
float vocalStartMarker = 1.0;
    float vocalEndMarker = 3.0;
    NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSURL *audioFileInput =[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/idea_honey_bunny.mp3", [[NSBundle mainBundle] resourcePath]]];



       NSURL *audioFileOutput =[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/idea_honey_bunny.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;

    if (!audioFileInput || !audioFileOutput)
    {
        return NO;
    }

    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
    AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                            presetName:AVAssetExportPresetAppleM4A];

    if (exportSession == nil)
    {
        return NO;
    }

    CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 1)), 1);
    CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 1)), 1);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    exportSession.outputURL = audioFileOutput;
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = exportTimeRange;

    [exportSession exportAsynchronouslyWithCompletionHandler:^
     {
         if (AVAssetExportSessionStatusCompleted == exportSession.status)
         {
             NSLog(@"It worked!");
         }
         else if (AVAssetExportSessionStatusFailed == exportSession.status)
         {
             // It failed...
         }
     }];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileOutput error:&error];
    //  audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil)
        NSLog(@"%@",error);
    else
        [audioPlayer play];
    return YES;
}

音声ファイルのトリミングを試みたいのですが、これにコンパイラが入力されていません。} else if (AVAssetExportSessionStatusFailed == exportSession.status) { // 失敗しました... } }]; ブロックとファイルはトリムされません

4

2 に答える 2

0

exportAsynchronouslyWithCompletionHandlerメソッドは、新しいアセットをエクスポートするのに時間がかかります。したがって、準備ができていないアセットを使用してオーディオ プレーヤーを作成します。

このようにコードを変更してみてください

[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     if (AVAssetExportSessionStatusCompleted == exportSession.status)
     {
        NSLog(@"It worked!");
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileOutput error:&error];
        if (audioPlayer == nil) {
            NSLog(@"%@",error);
        }
        else {
            [audioPlayer play];
        }
     }
     else if (AVAssetExportSessionStatusFailed == exportSession.status)
     {
         // It failed...
     }
 }];

また、ムラリが上で言ったように、出力ファイルのパスを変更します

于 2013-01-24T11:38:12.540 に答える
0

audioFileOutput と audioFileInput に同じパスを指定しました...そのため、失敗のケースになります。また、ファイルが入力パスに存在することを確認してください...OutputFile パスを変更して確認してください..

于 2013-01-24T11:34:59.353 に答える