Skip to content

skiptools/skip-av

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SkipAV

Audio/Video functionality for Skip apps.

See what API is included here.

About

The SkipAV framework provides a small subset of the AVKit and AVFoundation frameworks as well as a SwiftUI.VideoPlayer component for Android based on the androidx.media3 package's ExoPlayer.

Dependencies

SkipAV depends on the skip transpiler plugin and the SkipUI package.

SkipAV is part of the core SkipStack and is not intended to be imported directly. The transpiler includes import skip.av.* in generated Kotlin for any Swift source that imports the AVKit or AVFoundation frameworks.

Contributing

We welcome contributions to SkipAV. The Skip product documentation includes helpful instructions and tips on local Skip library development.

Examples

import SwiftUI
import AVKit

struct PlayerView: View {
    @State var player = AVPlayer(playerItem: AVPlayerItem(url: URL(string: "https://skip.tools/assets/introduction.mov")!))
    @State var isPlaying: Bool = false

    var body: some View {
        VStack {
            Button {
                isPlaying ? player.pause() : player.play()
                isPlaying = !isPlaying
                player.seek(to: .zero)
            } label: {
                Image(systemName: isPlaying ? "stop" : "play")
                    .padding()
            }

            VideoPlayer(player: player)
        }
    }
}

This framework also supports the 'AVFoundation.AVAudioRecorder' and AVFoundation.AVAudioPlayer' APIs via Android's MediaRecorder and MediaPlayer. These APIs can be used for audio recording and playback.

import SwiftUI
import AVFoundation

struct AudioPlayground: View {
    @State var isRecording: Bool = false
    @State var errorMessage: String? = nil
    
    @State var audioRecorder: AVAudioRecorder?
    @State var audioPlayer: AVAudioPlayer?

    var body: some View {
        #if SKIP
        let context = androidx.compose.ui.platform.LocalContext.current
        #endif
        return VStack {
            Button(isRecording ? "Stop Recording" : "Start Recording") {
                self.isRecording ? self.stopRecording() : self.startRecording()
            }
            Button("Play Recording") {
                try? self.playRecording()
            }
            if let errorMessage {
                Text(errorMessage)
                    .foregroundColor(.red)
            }
        }
        .padding()
        #if SKIP
        .onAppear {
            requestAudioRecordingPermission(context: context)
        }
        #endif
    }

    var captureURL: URL {
        get {
            #if SKIP
            let context = ProcessInfo.processInfo.androidContext
            let file = java.io.File(context.filesDir, "recording.m4a")
            return URL(fileURLWithPath: file.absolutePath)
            #else
            return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
                .first!.appendingPathComponent("recording.m4a")
            #endif
        }
    }
    
    func startRecording() {
        do {
            #if !SKIP
            setupAudioSession()
            #endif
            self.audioRecorder = try AVAudioRecorder(url: captureURL, settings: [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 12000, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue])
        } catch {
            print(error.localizedDescription)
        }
        audioRecorder?.record()
        isRecording = true
    }
    
    func stopRecording() {
        isRecording = false
        audioRecorder?.stop()
    }
    
    func playRecording() throws {
        do {
            guard FileManager.default.fileExists(atPath: captureURL.path) else {
                errorMessage = "Recording file does not exist."
                return
            }
            audioPlayer = try AVAudioPlayer(contentsOf: captureURL)
            audioPlayer?.play()
            errorMessage = ""
        } catch {
            logger.error("Could not play audio: \(error.localizedDescription)")
            errorMessage = "Could not play audio: \(error.localizedDescription)"
        }
    }
    
    #if SKIP
    func requestAudioRecordingPermission(context: android.content.Context) {
        guard let activity = context as? android.app.Activity else {
            return
        }

        // You must also list these permissions in your Manifest.xml
        let permissions = listOf(android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        androidx.core.app.ActivityCompat.requestPermissions(activity, permissions.toTypedArray(), 1)
    }
    
    #else
    
    func setupAudioSession() {
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(.playAndRecord, mode: .default)
            try session.setActive(true)
        } catch {
            errorMessage = "Failed to setup audio session: \(error.localizedDescription)"
        }
    }
    #endif
}

API Support

The following table summarizes SkipAV's API support on Android. Anything not listed here is likely not supported. Note that in your iOS-only code - i.e. code within #if !SKIP blocks - you can use any Swift API you want. Additionally:

Support levels:

  • βœ… – Full
  • 🟒 – High
  • 🟑 – Medium
  • 🟠 – Low
SupportAPI
🟒
AVAudioPlayer
  • init(contentsOf url: URL) throws
  • init(data: Data) throws
  • func prepareToPlay() -> Bool
  • func play()
  • func pause()
  • func stop()
  • var isPlaying: Bool
  • var duration: TimeInterval
  • var numberOfLoops: Int
  • var volume: Double
  • var rate: Double
  • var currentTime: TimeInterval
  • var url: URL?
  • var data: Data?
🟒
AVAudioRecorder
  • init(url: URL, settings: [String: Any]) throws
  • func prepareToRecord() -> Bool
  • func record()
  • func pause()
  • func stop()
  • func deleteRecording() -> Bool
  • var isRecording: Bool
  • var url: URL
  • var settings: [String: Any]
  • var currentTime: TimeInterval
  • func peakPower(forChannel channelNumber: Int) -> Float
  • func averagePower(forChannel channelNumber: Int) -> Double
🟠
AVPlayer
  • init()
  • init(playerItem: AVPlayerItem?)
  • init(url: URL)
  • func play()
  • func pause()
  • func seek(to time: CMTime)
🟠
AVPlayerItem
  • init(url: URL)
🟑
VideoPlayer
  • init(player: AVPlayer?)

License

This software is licensed under the GNU Lesser General Public License v3.0, with the following linking exception to clarify that distribution to restricted environments (e.g., app stores) is permitted:

This software is licensed under the LGPL3, included below. As a special exception to the GNU Lesser General Public License version 3 ("LGPL3"), the copyright holders of this Library give you permission to convey to a third party a Combined Work that links statically or dynamically to this Library without providing any Minimal Corresponding Source or Minimal Application Code as set out in 4d or providing the installation information set out in section 4e, provided that you comply with the other provisions of LGPL3 and provided that you meet, for the Application the terms and conditions of the license(s) which apply to the Application. Except as stated in this special exception, the provisions of LGPL3 will continue to comply in full to this Library. If you modify this Library, you may apply this exception to your version of this Library, but you are not obliged to do so. If you do not wish to do so, delete this exception statement from your version. This exception does not (and cannot) modify any license terms which apply to the Application, with which you must still comply.