Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vmanot/media
A high-level framework for manipulating media in Swift.
https://github.com/vmanot/media
Last synced: about 1 month ago
JSON representation
A high-level framework for manipulating media in Swift.
- Host: GitHub
- URL: https://github.com/vmanot/media
- Owner: vmanot
- Created: 2022-09-07T07:26:58.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-01T18:09:27.000Z (5 months ago)
- Last Synced: 2025-01-07T13:10:59.675Z (about 1 month ago)
- Language: Swift
- Size: 71.3 KB
- Stars: 8
- Watchers: 4
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Media
**Media** makes it stupid simple to work with media capture & playback in Swift.
# Installation
### Swift Package Manager
1. Open your Swift project in Xcode.
2. Go to `File` -> `Add Package Dependency`.
3. In the search bar, enter [this URL](https://github.com/vmanot/Media/).
4. Choose the version you'd like to install.
5. Click `Add Package`.# Usage
### Import the framework
```diff
+ import Media
```### Display a camera view in SwiftUI
```swift
import SwiftUI
import Mediastruct MyCameraView: View {
@State private var capturedImage: UIImage? = nil
var body: some View {
CameraViewReader { (cameraProxy: CameraViewProxy) in
CameraView(camera: .back, mirrored: false)
.safeAreaInset(edge: .bottom) {
captureButton(camera: cameraProxy) { image in
self.capturedImage = image
}
}
}
}
@ViewBuilder
private func captureButton(camera: CameraViewProxy, onCapture: @escaping (UIImage) -> Void) -> some View {
Button {
Task { @MainActor in
let image: UIImage = try! await camera.capturePhoto()
onCapture(image)
}
} label: {
Label {
Text("Capture Photo")
} icon: {
Image(systemName: .cameraFill)
}
.font(.title2)
.controlSize(.large)
.padding(.small)
}
.buttonStyle(.borderedProminent)
}
}
```### Play audio from a file
Play audio using `AudioPlayer`.
```swift
import Mediastruct MyAudioTask {
@MainActor
func playAudio(forFile fileURL: URL) {
Task {
// This line will suspend until the audio has finished playing!
try await AudioPlayer().play(.url(fileURL))
}
}
}
```### Record audio
Record audio using `AudioRecorder`.
```swift
import Mediaclass MyClass {
// ... the rest of your code@MainActor
let recorder = AudioRecorder()@MainActor
func startRecording() async {
do {
try await recorder.record()
} catch {
print(error)
}
}@MainActor
func stopRecording() async {
do {
try await recorder.stop()
} catch {
print(error)
}
}// ... the rest of your code
}
```### Convert audio file format
```swift
let wavFileURL: URL = try await MediaAssetLocation.url(myOriginalFileURL).convert(to: .wav)
```# License
**Media** is licensed under the MIT License.