2015-06-10 54 views
1

在watchOS 2中,Apple包含了一项新功能,允许应用程序记录短的音频文件,但是,如何在我现有的WatchKit项目中调用这种新方法?如何录制音频并使用WatchOS2进行播放?

- (void)presentAudioRecordingControllerWithOutputURL:(NSURL * nonnull)URL 
               preset:(WKAudioRecordingPreset)preset 
            maximumDuration:(NSTimeInterval)maximumDuration 
             actionTitle:(NSString * nullable)actionTitle 
              completion:(void (^ nonnull)(BOOL didSave, 
                   NSError * nullable error))completion; 

我的问题不是如何调用该方法,是Xcode编译器说,找不到这个函数。我是否必须包含任何其他框架?难道是因为我的WatchKit项目是使用以前版本的WatchOS创建的?

谢谢!

+0

谢谢,但我的问题是关于新的WatchOS版本。有人在我问之后回答。 – EnriMR

+2

@EntriMR复制仅在问题之间创建网络。两个问题的答案都是相关的,所以当每个问题被问到时都没有关系。 – AstroCB

回答

2

1)您需要创建一个文件URL来存储录制的输出。

NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask,YES); 
NSString *path = [[filePaths firstObject] stringByAppendingPathComponent:@"rec.m4a"]; 
NSURL *fileUrl = [NSURL fileURLWithPath:path]; 

您可以指定的扩展.wav格式,.mp4和.m4a的。

2)调用方法如下:

[self presentAudioRecordingControllerWithOutputURL:fileUrl 
              preset:WKAudioRecordingPresetWideBandSpeech 
            maximumDuration:5.0 
             actionTitle:@"Some Title" 
             completion:^(BOOL didSave, NSError * __nullable error) { 

              NSLog(@"didSave:%d, error:%@", didSave, error); 
             }]; 

可以选择除了预设上述

  • WKAudioRecordingPresetNarrowBandSpeech
  • WKAudioRecordingPresetHighQualityAudio

在斯威夫特:

self.presentAudioRecordingControllerWithOutputURL(
    self.recFileURL(), 
    preset: WKAudioRecordingPreset.WideBandSpeech, 
    maximumDuration: 5.0, 
    actionTitle: "SomeTitle") { (didSave, error) -> Void in 

     print("didSave:\(didSave), error:\(error)") 
} 

可以按如下方式播放录音文件:

self.presentMediaPlayerControllerWithURL(
    fileURL, 
    options: nil) { (didPlayToEnd, endTime, error) -> Void in 

     print("didPlayToEnd:\(didPlayToEnd), endTime:\(endTime), error:\(error)") 
} 

您可以查看详细规范here

+0

我在编写要调用的方法时收到错误消息。我应该加入一些图书馆吗?我的项目是用以前版本的WatchOS – EnriMR

+0

@ shu223创建的......你可以解释如何使用objective-c来记录和播放音频 – Priya