Ich möchte eine Funktion implementieren, mit der der Benutzer eine Audiodatei (.caf) trimmen kann, die er zuvor aufgenommen hat. Der Aufnahmeteil funktioniert bereits, aber wie kann ich eine Trimm-Funktion hinzufügen, die der in der Voicememos App ähnelt? Gibt es eine API für den Audiotrimmer, den Apple verwendet? Jede Hilfe wäre toll ...Trim Audio mit iOS
Antwort
Wie wäre es mit der AVFoundation? Importieren Sie die Audiodatei in einen AVAsset (Komposition usw.), und exportieren Sie sie - unter Angabe bevorzugter Zeit + Dauer - in eine Datei.
Ich schrieb eine Aktie Funktion vor einer Zeit, die ein Asset in eine Datei exportiert, können Sie auch einen Audiomix angeben. Wie unten exportiert es die gesamte Datei, aber Sie könnten NTFimeRange zu exporteur.timeRange hinzufügen und los gehts. Ich habe das zwar noch nicht getestet, sollte aber funktionieren (?). Eine andere Alternative könnte darin bestehen, Zeitbereiche anzupassen, wenn die AVAsset + -Spuren erstellt werden. Der Exporteur behandelt natürlich nur m4a (AAC). Entschuldigung, wenn das nicht das war, was du wolltest.
-(void)exportAsset:(AVAsset*)asset toFile:(NSString*)filename overwrite:(BOOL)overwrite withMix:(AVAudioMix*)mix {
//NSArray* availablePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
AVAssetExportSession* exporter = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
if (exporter == nil) {
DLog(@"Failed creating exporter!");
return;
}
DLog(@"Created exporter! %@", exporter);
// Set output file type
DLog(@"Supported file types: %@", exporter.supportedFileTypes);
for (NSString* filetype in exporter.supportedFileTypes) {
if ([filetype isEqualToString:AVFileTypeAppleM4A]) {
exporter.outputFileType = AVFileTypeAppleM4A;
break;
}
}
if (exporter.outputFileType == nil) {
DLog(@"Needed output file type not found? (%@)", AVFileTypeAppleM4A);
return;
}
// Set outputURL
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* parentDir = [NSString stringWithFormat:@"%@/", [paths objectAtIndex:0]];
NSString* outPath = [NSString stringWithFormat:@"%@%@", parentDir, filename];
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:outPath]) {
DLog(@"%@ already exists!", outPath);
if (!overwrite) {
DLog(@"Not overwriting, uh oh!");
return;
}
else {
// Overwrite
DLog(@"Overwrite! (delete first)");
NSError* error = nil;
if (![manager removeItemAtPath:outPath error:&error]) {
DLog(@"Failed removing %@, error: %@", outPath, error.description);
return;
}
else {
DLog(@"Removed %@", outPath);
}
}
}
NSURL* const outUrl = [NSURL fileURLWithPath:outPath];
exporter.outputURL = outUrl;
// Specify a time range in case only part of file should be exported
//exporter.timeRange =
if (mix != nil)
exporter.audioMix = mix; // important
DLog(@"Starting export! (%@)", exporter.outputURL);
[exporter exportAsynchronouslyWithCompletionHandler:^(void) {
// Export ended for some reason. Check in status
NSString* message;
switch (exporter.status) {
case AVAssetExportSessionStatusFailed:
message = [NSString stringWithFormat:@"Export failed. Error: %@", exporter.error.description];
DLog(@"%@", message);
[self showAlert:message];
break;
case AVAssetExportSessionStatusCompleted: {
/*if (playfileWhenExportFinished) {
DLog(@"playfileWhenExportFinished!");
[self playfileAfterExport:exporter.outputURL];
playfileWhenExportFinished = NO;
}*/
message = [NSString stringWithFormat:@"Export completed: %@", filename];
DLog(@"%@", message);
[self showAlert:message];
break;
}
case AVAssetExportSessionStatusCancelled:
message = [NSString stringWithFormat:@"Export cancelled!"];
DLog(@"%@", message);
[self showAlert:message];
break;
default:
DLog(@"Export unhandled status: %d", exporter.status);
break;
}
}];
}
Die obige Antwort von @Jonny ist korrekt. Hier habe ich die Verwendung von AudioMixer hinzugefügt, um den Fade-in-Effekt beim Audiotrimmen hinzuzufügen.
Ausgang:. Audio Anlage mit einer 10 Sekunden Fade auf 20 Sekunden getrimmt in Die Trimmung im Codeausschnitt oben gesetzt wird erfolgt an der 30 Sekunden Markierung des Vermögenswerts und damit die Spurdauer sollte sein mindestens 50 Sekunden.
- (BOOL)exportAssettoFilePath:(NSString *)filePath {
NSString *inputFilePath = <inputFilePath>;
NSURL *videoToTrimURL = [NSURL fileURLWithPath:inputFilePath];
AVAsset *avAsset = [AVAsset assetWithURL:videoToTrimURL];
// we need the audio asset to be at least 50 seconds long for this snippet
CMTime assetTime = [avAsset duration];
Float64 duration = CMTimeGetSeconds(assetTime);
if (duration < 50.0) return NO;
// get the first audio track
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
if ([tracks count] == 0) return NO;
AVAssetTrack *track = [tracks objectAtIndex:0];
// create the export session
// no need for a retain here, the session will be retained by the
// completion handler since it is referenced there
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:avAsset
presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession) return NO;
// create trim time range - 20 seconds starting from 30 seconds into the asset
CMTime startTime = CMTimeMake(30, 1);
CMTime stopTime = CMTimeMake(50, 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
// create fade in time range - 10 seconds starting at the beginning of trimmed asset
CMTime startFadeInTime = startTime;
CMTime endFadeInTime = CMTimeMake(40, 1);
CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime,
endFadeInTime);
// setup audio mix
AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
AVMutableAudioMixInputParameters *exportAudioMixInputParameters =
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0
timeRange:fadeInTimeRange];
exportAudioMix.inputParameters = [NSArray
arrayWithObject:exportAudioMixInputParameters];
// configure export session output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:filePath]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
exportSession.timeRange = exportTimeRange; // trim time range
//exportSession.audioMix = exportAudioMix; // fade in audio mix
// perform the export
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed == exportSession.status) {
// a failure may happen because of an event out of your control
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"AVAssetExportSessionStatusFailed");
} else {
NSLog(@"Export Session Status: %ld", (long)exportSession.status);
}
}];
return YES;}
Dank
Weitere Details:
https://developer.apple.com/library/ios/qa/qa1730/_index.html
- 1. iOS: Audio Recording Dateiformat
- 2. IOS Audio Compression
- 3. IOS-System-Sounds mit benutzerdefinierten Audio-Sessions
- 4. iOS 7: Einfache Audio-Steuerung mit AVAudioPlayer?
- 5. Trim Ergebnis mit RegEx
- 6. Trim mit PHP
- 7. mysql concat mit trim
- 8. Trim Problem mit VBA
- 9. Record Audio in Stereo iOS
- 10. Audio Export fehl iOS Swift
- 11. Einblenden, Effekt ausblenden in Audio Mit AVAssetExportSession in IOS
- 12. Audio Spectrum Effekt iOS/Android
- 13. Wiedergabepuffer von Audio-Werten - iOS
- 14. Trim-String mit spezifischem Zeichensatz
- 15. IOS AVFoundation Audio/Video-out of sync
- 16. iOS: Wiedergeben von Audio im unbeaufsichtigten Modus
- 17. iOS-natives MPVolumeView-Audio-Routenmenü schließen
- 18. Hintergrund Audio für iOS Video App
- 19. Audio Session "Ducking" in iOS 4 ...?
- 20. iOS 4: Fernbedienungen für Hintergrund-Audio
- 21. iOS AudioQueues: Störungen im Audio-float-stream
- 22. Dateityp (Audio oder Video) in iOS
- 23. iOS: AVAssetWriterInput nicht Audio in Videoaufnahme speichern
- 24. iOS Audio Unit - Erstellen von Stereosinuswellen
- 25. Codename ein Audio-Playback-Absturz auf iOS
- 26. HTMLAudioElement Audio nicht unter iOS (Ionic App)
- 27. Playback und Aufnahme gleichzeitig mit Core Audio in iOS
- 28. iOS - Mischen Sie zwei Video in einzelne Videodatei mit Audio
- 29. iOS - Aufnahme Audio mit AVAudioRecorder scheitern ohne Fehler
- 30. Jquery überprüfen Eingabe Trim
Aber dieser Ansatz erlaubt nur den Ton im m4a-Format gespeichert werden, was wenn man gerne eine mp3 oder eine caf-datei trimmt und die fo behält rmat? – tommys
Trim mp3 funktioniert auch, Ausgabe gespeichert in M4a Format Nach 2 Jahren, sparen Sie immer noch mein Leben :) –