1

我目前正在使用 SwiftUI 开发应用程序。

我想知道如何重复播放系统声音并使用AudioServicesPlayAlertSoundWithCompletion.

我想重复它们几次,直到用户点击某个按钮。

我怎么能这样做?


内容视图.swift

import SwiftUI
import AudioToolbox

struct ContentView: View {
    
    var SoundID:SystemSoundID = 1151
    
    var body: some View {
        VStack{
            Text("Sound")
                .padding()
                .onTapGesture {
                    makeSoundAndVibration()
                }
            Text("Stop")
                .padding()
                .onTapGesture {
                    stopSoundAndVibration()
                }
        }
    }
    func makeSoundAndVibration(){
        AudioServicesPlayAlertSoundWithCompletion(SoundID, nil)
        AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) {}
    }
    
    func stopSoundAndVibration(){
        // do someting to stop the sound and vibration
    }
}

SystemSoundTestApp.swift

import SwiftUI

@main
struct SystemSoundTestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Xcode:版本 12.0.1

iOS:14.0

4

0 回答 0