diff --git a/README.md b/README.md index 24b5011..2359532 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,20 @@ import OggDecoder ```swift let decoder = OGGDecoder() let oggFile = oggFileURL() + +// Using completion handler decoder.decode(oggFile) { (savedWavUrl: URL?) in // Do whatever you want with URL // If convert was fail, returned url is nil } + +// Using async +Task { + let outputUrl = await decoder.decode(oggFile) + + var wavAudioUrl: URL! + let result = await decoder.decode(oggFile, into: wavAudioUrl) +} ``` Available methods: @@ -46,7 +56,8 @@ decoder.decode(URL) -> URL? decoder.decode(URL, completion: (URL?) -> Void) decoder.decode(URL, into: URL) -> bool decoder.decode(URL, into: URL, completion: (Bool) -> Void) - +decoder.decode(URL) async -> URL? +decoder.decode(URL, into: URL) async -> Bool ``` ## License diff --git a/Sources/OggDecoderObjC/include/OGGDecoder.h b/Sources/OggDecoderObjC/include/OGGDecoder.h index 1dca249..f2b5081 100644 --- a/Sources/OggDecoderObjC/include/OGGDecoder.h +++ b/Sources/OggDecoderObjC/include/OGGDecoder.h @@ -17,6 +17,16 @@ NS_ASSUME_NONNULL_BEGIN -(void)decode:(NSURL*)oggFile into:(NSURL*)outputFile completion:(void (^)(BOOL))completion; -(nullable NSURL*)decode:(NSURL*)oggFile; -(void)decode:(NSURL*)oggFile completion:(void (^)(NSURL* _Nullable outputFile))completion; + +// async/await exposure for Swift +#if __has_attribute(availability) +-(void)decode:(NSURL *)oggFile completion:(void (^)(NSURL * _Nullable))completionHandler NS_SWIFT_ASYNC_NAME(decode(oggFile:)) API_AVAILABLE(ios(15.0), macos(12.0)); +-(void)decode:(NSURL *)oggFile into:(NSURL *)outputFile completion:(void (^)(BOOL))completionHandler NS_SWIFT_ASYNC_NAME(decode(oggFile:into:)) API_AVAILABLE(ios(15.0), macos(12.0)); +#else +-(void)decode:(NSURL *)oggFile completion:(void (^)(NSURL * _Nullable))completionHandler NS_SWIFT_ASYNC_NAME(decode(oggFile:)); +-(void)decode:(NSURL *)oggFile into:(NSURL *)outputFile completion:(void (^)(BOOL))completionHandler NS_SWIFT_ASYNC_NAME(decode(oggFile:into:)); +#endif + @end NS_ASSUME_NONNULL_END diff --git a/Tests/OggDecoderTests/OggDecoderTests.swift b/Tests/OggDecoderTests/OggDecoderTests.swift index cc7cb39..c83f7fb 100644 --- a/Tests/OggDecoderTests/OggDecoderTests.swift +++ b/Tests/OggDecoderTests/OggDecoderTests.swift @@ -114,6 +114,33 @@ final class OggDecoderTests: XCTestCase { waitForExpectations(timeout: 60) } + @available(iOS 15.0, macOS 12.0, *) + func test_decodeOggAudio_asyncAwait_returnsTrue() async throws { + let oggAudioUrl = try fetchFileUrlFromTestBundle(filename: "TestResources/Chopin-polonaise-in-a-military", ext: "ogg") + let result = await decoder.decode(oggAudioUrl, into: wavAudioUrl) + XCTAssertTrue(result) + } + + @available(iOS 15.0, macOS 12.0, *) + func test_decodeOgaAudio_asyncAwait_returnsTrue() async throws { + let ogaAudioUrl = try fetchFileUrlFromTestBundle(filename: "TestResources/Chopin_-_Polonaise_Op._53", ext: "oga") + let result = await decoder.decode(ogaAudioUrl, into: wavAudioUrl) + XCTAssertTrue(result) + } + + @available(iOS 15.0, macOS 12.0, *) + func test_decodeOggAudio_asyncAwait_returnsFileUrl() async throws { + let oggAudioUrl = try fetchFileUrlFromTestBundle(filename: "TestResources/Chopin-polonaise-in-a-military", ext: "ogg") + let outputUrl = await decoder.decode(oggAudioUrl) + XCTAssertNotNil(outputUrl) + } + + @available(iOS 15.0, macOS 12.0, *) + func test_decodeOgaAudio_asyncAwait_returnsFileUrl() async throws { + let ogaAudioUrl = try fetchFileUrlFromTestBundle(filename: "TestResources/Chopin_-_Polonaise_Op._53", ext: "oga") + let outputUrl = await decoder.decode(ogaAudioUrl) + XCTAssertNotNil(outputUrl) + } private func fetchFileUrlFromTestBundle(filename: String, ext: String) throws -> URL { let bundle = Bundle.module