Tôi muốn thực hiện một tính năng cho phép người dùng cắt một tệp âm thanh (.caf) mà anh ta đã ghi lại một cách dữ dội. Phần ghi âm đã hoạt động, nhưng làm thế nào tôi có thể thêm một tính năng cắt tỉa tương tự như một trong ứng dụng Voicememos. Có một api cho âm thanh tông đơ táo sử dụng? Mọi trợ giúp sẽ tuyệt vời ...Cắt âm thanh với iOS
Trả lời
Làm thế nào để sử dụng AVFoundation? Nhập tệp âm thanh vào một AVAsset (thành phần vv), sau đó bạn có thể xuất nó - thiết lập thời gian + thời gian ưu tiên - thành một tệp.
Tôi đã viết một hàm cổ phiếu một thời gian trước đây để xuất một nội dung vào một tệp, bạn cũng có thể chỉ định một audiomix. Như dưới đây nó xuất khẩu tất cả các tập tin, nhưng bạn có thể thêm một NSTimeRange để exports.timeRange và có bạn đi. Tôi đã không thử nghiệm mà mặc dù, nhưng nên làm việc (?). Một lựa chọn khác có thể là điều chỉnh phạm vi thời gian khi tạo các bản nhạc AVAsset +. Tất nhiên nhà xuất khẩu chỉ xử lý m4a (AAC). Xin lỗi nếu đây không phải là những gì bạn muốn.
-(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;
}
}];
}
Câu trả lời ở trên của @Jonny là chính xác. Đây là tôi đang thêm việc sử dụng AudioMixer để thêm hiệu ứng Fade-in trong khi âm thanh cắt tỉa.
Output:. Tài sản âm thanh tỉa đến 20 giây với một phai 10 giây trong Các trang trí được thiết lập trong đoạn mã diễn ra tại 30 giây dấu ấn của tài sản và do đó thời gian theo dõi nên có ít nhất 50 giây.
- (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;}
Cảm ơn
Để biết thêm chi tiết:
https://developer.apple.com/library/ios/qa/qa1730/_index.html
- 1. iOS âm thanh Cắt
- 2. Âm thanh mô phỏng iOS
- 3. Mediaplayer Cắt âm thanh quá sớm
- 4. Ghi lại âm thanh iOS
- 5. Âm thanh hệ thống iOS với các phiên âm thanh tùy chỉnh
- 6. iOS: Định dạng tệp ghi âm âm thanh
- 7. iOS: Âm thanh và video từ Airplay
- 8. Nhận mảng byte âm thanh với API âm thanh HTML5?
- 9. Âm thanh khuếch đại với API âm thanh web
- 10. Phát âm thanh với HTML5
- 11. Phát Âm thanh với J2ME
- 12. Phát âm thanh với SoundPool
- 13. Âm thanh thông báo đẩy iOS tùy chỉnh
- 14. Đơn vị âm thanh iOS - Tạo sóng Sine Stereo
- 15. Phát âm thanh có các nút điều khiển trong iOS
- 16. Âm thanh âm thanh định tuyến iPhone
- 17. Xử lý âm thanh: Chơi với mức âm lượng
- 18. Đa nhiệm trên iOS cho ứng dụng Ghi âm thanh
- 19. Phát hiện thiết bị âm thanh đính kèm iOS
- 20. iOS 4: Điều khiển từ xa cho âm thanh nền
- 21. Cách giảm kích thước tệp âm thanh trong IOS
- 22. Âm thanh nền iOS dừng khi màn hình bị khóa
- 23. iOS: Cách bật Chỉ đường âm thanh trên Google Maps
- 24. Ghi và phát âm thanh đồng thời trong iOS
- 25. iOS: Thiết bị âm thanh Thao tác điều khiển từ xa AudioXuffer (tức là hiệu ứng âm thanh từ micrô)
- 26. Tôi làm cách nào để truy cập âm thanh iOS mặc định để đặt âm thanh đó làm âm thanh thông báo?
- 27. Tạo tệp âm thanh với âm 15 Khz
- 28. Âm thanh đa tuyến iOS6
- 29. Phát âm thanh và âm thanh thuật toán
- 30. Dòng âm thanh iOS từ máy chủ Với dựa trên cookie Auth
Nhưng phương pháp này chỉ cho phép âm thanh được lưu trong m4a định dạng, những gì nếu người ta muốn cắt một tập tin mp3 hoặc một quán cà phê và giữ cho rmat? – tommys
Cắt mp3 cũng hoạt động, ouput được lưu trữ ở định dạng m4a Sau 2 năm, bạn vẫn tiết kiệm được cuộc sống của mình :) –