@@ -2,7 +2,7 @@ import Flutter
22import UIKit
33
44public class SwiftAudioWaveformsPlugin : NSObject , FlutterPlugin {
5-
5+
66 let audioRecorder : AudioRecorder
77 var audioPlayers = [ String: AudioPlayer] ( )
88 var extractors = [ String: WaveformExtractor] ( )
@@ -27,158 +27,158 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
2727 public func handle( _ call: FlutterMethodCall , result: @escaping FlutterResult ) {
2828 let args = call. arguments as? Dictionary < String , Any >
2929 switch call. method {
30- case Constants . startRecording:
31- guard let args = call. arguments as? Dictionary < String , Any > else {
32- result ( FlutterError ( code: Constants . audioWaveforms, message: " Invalid Arguments " , details: nil ) )
33- return
34- }
35- audioRecorder. startRecording ( result, RecordingSettings . fromJson ( ( args) ) )
36- break
37- case Constants . pauseRecording:
38- audioRecorder. pauseRecording ( result)
39- break
40- case Constants . resumeRecording:
41- audioRecorder. resumeRecording ( result)
42- case Constants . stopRecording:
43- audioRecorder. stopRecording ( result)
44- break
45- case Constants . getDecibel:
46- audioRecorder. getDecibel ( result)
47- break
48- case Constants . checkPermission:
49- audioRecorder. checkHasPermission ( result)
50- break
51- case Constants . preparePlayer:
52- let key = args ? [ Constants . playerKey] as? String
53- if ( key != nil ) {
54- initPlayer ( playerKey: key!)
55- audioPlayers [ key!] ? . preparePlayer ( path: args ? [ Constants . path] as? String ,
56- volume: args ? [ Constants . volume] as? Double ,
57- updateFrequency: args ? [ Constants . updateFrequency] as? Int ,
58- result: result,
59- overrideAudioSession: ( args ? [ Constants . overrideAudioSession] as? Bool ) ?? false )
60- } else {
61- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not prepare player " , details: " Player key is null " ) )
62- }
63- break
64- case Constants . startPlayer:
65- let key = args ? [ Constants . playerKey] as? String
66- if ( key != nil ) {
67- audioPlayers [ key!] ? . startPlyer ( result: result)
68- } else {
69- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not start player " , details: " Player key is null " ) )
70- }
71- break
72- case Constants . finishMode:
73- let key = args ? [ Constants . playerKey] as? String
74- let releaseType = args ? [ Constants . finishType] as? Int
75- if ( key != nil ) {
76- audioPlayers [ key!] ? . setFinishMode ( result: result, releaseType: releaseType)
77- } else {
78- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set release mode " , details: " Player key is null " ) )
79- }
80- case Constants . pausePlayer:
81- let key = args ? [ Constants . playerKey] as? String
82- if ( key != nil ) {
83- audioPlayers [ key!] ? . pausePlayer ( )
84- result ( true )
85- } else {
86- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not pause player " , details: " Player key is null " ) )
87- }
88- break
89- case Constants . stopPlayer:
90- let key = args ? [ Constants . playerKey] as? String
91- if ( key != nil ) {
92- audioPlayers [ key!] ? . stopPlayer ( )
93- result ( true )
94- } else {
95- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not stop player " , details: " Player key is null " ) )
96- }
97- break
98- case Constants . releasePlayer:
99- let key = args ? [ Constants . playerKey] as? String
100- if ( key != nil ) {
101- audioPlayers [ key!] ? . release ( result: result)
102- }
103- break ;
104- case Constants . seekTo:
105- let key = args ? [ Constants . playerKey] as? String
106- if ( key != nil ) {
107- audioPlayers [ key!] ? . seekTo ( args ? [ Constants . progress] as? Int , result)
108- } else {
109- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not seek to postion " , details: " Player key is null " ) )
110- }
111- case Constants . setVolume:
112- let key = args ? [ Constants . playerKey] as? String
113- if ( key != nil ) {
114- audioPlayers [ key!] ? . setVolume ( args ? [ Constants . volume] as? Double , result)
115- } else {
116- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set volume " , details: " Player key is null " ) )
117- }
118- case Constants . setRate:
119- let key = args ? [ Constants . playerKey] as? String
120- if ( key != nil ) {
121- audioPlayers [ key!] ? . setRate ( args ? [ Constants . rate] as? Double , result)
122- } else {
123- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set rate " , details: " Player key is null " ) )
124- }
125- case Constants . getDuration:
126- let type = args ? [ Constants . durationType] as? Int
127- let key = args ? [ Constants . playerKey] as? String
128- if ( key != nil ) {
129- do {
130- if ( type == 0 ) {
131- try audioPlayers [ key!] ? . getDuration ( DurationType . Current, result)
132- } else {
133- try audioPlayers [ key!] ? . getDuration ( DurationType . Max, result)
30+ case Constants . startRecording:
31+ guard let args = call. arguments as? Dictionary < String , Any > else {
32+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Invalid Arguments " , details: nil ) )
33+ return
34+ }
35+ audioRecorder. startRecording ( result, RecordingSettings . fromJson ( ( args) ) )
36+ break
37+ case Constants . pauseRecording:
38+ audioRecorder. pauseRecording ( result)
39+ break
40+ case Constants . resumeRecording:
41+ audioRecorder. resumeRecording ( result)
42+ case Constants . stopRecording:
43+ audioRecorder. stopRecording ( result)
44+ break
45+ case Constants . getDecibel:
46+ audioRecorder. getDecibel ( result)
47+ break
48+ case Constants . checkPermission:
49+ audioRecorder. checkHasPermission ( result)
50+ break
51+ case Constants . preparePlayer:
52+ let key = args ? [ Constants . playerKey] as? String
53+ if ( key != nil ) {
54+ initPlayer ( playerKey: key!)
55+ audioPlayers [ key!] ? . preparePlayer ( path: args ? [ Constants . path] as? String ,
56+ volume: args ? [ Constants . volume] as? Double ,
57+ updateFrequency: args ? [ Constants . updateFrequency] as? Int ,
58+ result: result,
59+ overrideAudioSession: ( args ? [ Constants . overrideAudioSession] as? Bool ) ?? false )
60+ } else {
61+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not prepare player " , details: " Player key is null " ) )
62+ }
63+ break
64+ case Constants . startPlayer:
65+ let key = args ? [ Constants . playerKey] as? String
66+ if ( key != nil ) {
67+ audioPlayers [ key!] ? . startPlyer ( result: result)
68+ } else {
69+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not start player " , details: " Player key is null " ) )
70+ }
71+ break
72+ case Constants . finishMode:
73+ let key = args ? [ Constants . playerKey] as? String
74+ let releaseType = args ? [ Constants . finishType] as? Int
75+ if ( key != nil ) {
76+ audioPlayers [ key!] ? . setFinishMode ( result: result, releaseType: releaseType)
77+ } else {
78+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set release mode " , details: " Player key is null " ) )
79+ }
80+ case Constants . pausePlayer:
81+ let key = args ? [ Constants . playerKey] as? String
82+ if ( key != nil ) {
83+ audioPlayers [ key!] ? . pausePlayer ( )
84+ result ( true )
85+ } else {
86+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not pause player " , details: " Player key is null " ) )
87+ }
88+ break
89+ case Constants . stopPlayer:
90+ let key = args ? [ Constants . playerKey] as? String
91+ if ( key != nil ) {
92+ audioPlayers [ key!] ? . stopPlayer ( )
93+ result ( true )
94+ } else {
95+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not stop player " , details: " Player key is null " ) )
96+ }
97+ break
98+ case Constants . releasePlayer:
99+ let key = args ? [ Constants . playerKey] as? String
100+ if ( key != nil ) {
101+ audioPlayers [ key!] ? . release ( result: result)
102+ }
103+ break ;
104+ case Constants . seekTo:
105+ let key = args ? [ Constants . playerKey] as? String
106+ if ( key != nil ) {
107+ audioPlayers [ key!] ? . seekTo ( args ? [ Constants . progress] as? Int , result)
108+ } else {
109+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not seek to postion " , details: " Player key is null " ) )
110+ }
111+ case Constants . setVolume:
112+ let key = args ? [ Constants . playerKey] as? String
113+ if ( key != nil ) {
114+ audioPlayers [ key!] ? . setVolume ( args ? [ Constants . volume] as? Double , result)
115+ } else {
116+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set volume " , details: " Player key is null " ) )
117+ }
118+ case Constants . setRate:
119+ let key = args ? [ Constants . playerKey] as? String
120+ if ( key != nil ) {
121+ audioPlayers [ key!] ? . setRate ( args ? [ Constants . rate] as? Double , result)
122+ } else {
123+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not set rate " , details: " Player key is null " ) )
124+ }
125+ case Constants . getDuration:
126+ let type = args ? [ Constants . durationType] as? Int
127+ let key = args ? [ Constants . playerKey] as? String
128+ if ( key != nil ) {
129+ do {
130+ if ( type == 0 ) {
131+ try audioPlayers [ key!] ? . getDuration ( DurationType . Current, result)
132+ } else {
133+ try audioPlayers [ key!] ? . getDuration ( DurationType . Max, result)
134+ }
135+ } catch {
136+ result ( FlutterError ( code: " " , message: " Failed to get duration " , details: nil ) )
134137 }
135- } catch {
136- result ( FlutterError ( code: " " , message: " Failed to get duration" , details: nil ) )
138+ } else {
139+ result ( FlutterError ( code: Constants . audioWaveforms , message: " Can not get duration" , details: " Player key is null " ) )
137140 }
138- } else {
139- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not get duration " , details: " Player key is null " ) )
140- }
141- case Constants . stopAllPlayers:
142- for (playerKey, _) in audioPlayers {
143- audioPlayers [ playerKey] ? . stopPlayer ( )
144- audioPlayers [ playerKey] = nil
145- }
146- result ( true )
147- case Constants . extractWaveformData:
148- guard let key = args ? [ Constants . playerKey] as? String else {
149- result (
150- FlutterError (
151- code: Constants . audioWaveforms,
152- message: " Can not get waveform data " ,
153- details: " Waveform key is null "
141+ case Constants . stopAllPlayers:
142+ for (playerKey, _) in audioPlayers {
143+ audioPlayers [ playerKey] ? . stopPlayer ( )
144+ audioPlayers [ playerKey] = nil
145+ }
146+ result ( true )
147+ case Constants . extractWaveformData:
148+ guard let key = args ? [ Constants . playerKey] as? String else {
149+ result (
150+ FlutterError (
151+ code: Constants . audioWaveforms,
152+ message: " Can not get waveform data " ,
153+ details: " Waveform key is null "
154+ )
154155 )
156+ break
157+ }
158+ let path = args ? [ Constants . path] as? String
159+ let noOfSamples = args ? [ Constants . noOfSamples] as? Int
160+ createOrUpdateExtractor (
161+ playerKey: key,
162+ result: result,
163+ path: path,
164+ noOfSamples: noOfSamples
155165 )
166+ case Constants . stopExtraction:
167+ guard let key = args ? [ Constants . playerKey] as? String else {
168+ result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not get waveform data " , details: " Waveform key is null " ) )
169+ break
170+ }
171+ extractors [ key] ? . cancel ( )
172+ result ( true )
173+ case Constants . pauseAllPlayers:
174+ for (playerKey, _) in audioPlayers {
175+ audioPlayers [ playerKey] ? . pausePlayer ( )
176+ }
177+ result ( true )
156178 break
157- }
158- let path = args ? [ Constants . path] as? String
159- let noOfSamples = args ? [ Constants . noOfSamples] as? Int
160- createOrUpdateExtractor (
161- playerKey: key,
162- result: result,
163- path: path,
164- noOfSamples: noOfSamples
165- )
166- case Constants . stopExtraction:
167- guard let key = args ? [ Constants . playerKey] as? String else {
168- result ( FlutterError ( code: Constants . audioWaveforms, message: " Can not get waveform data " , details: " Waveform key is null " ) )
179+ default :
180+ result ( FlutterMethodNotImplemented)
169181 break
170- }
171- extractors [ key] ? . cancel ( )
172- result ( true )
173- case Constants . pauseAllPlayers:
174- for (playerKey, _) in audioPlayers {
175- audioPlayers [ playerKey] ? . pausePlayer ( )
176- }
177- result ( true )
178- break
179- default :
180- result ( FlutterMethodNotImplemented)
181- break
182182 }
183183 }
184184
@@ -202,14 +202,11 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
202202 let newExtractor = try WaveformExtractor ( url: audioUrl!, flutterResult: result, channel: flutterChannel)
203203 extractors [ playerKey] = newExtractor
204204 Task {
205- let data = await newExtractor
206- . extractWaveform ( samplesPerPixel: noOfSamples, playerKey: playerKey)
207- if ( newExtractor. progress == 1.0 ) {
208- let waveformData = newExtractor. getChannelMean ( data: data!)
209- DispatchQueue . main. async {
210- result ( waveformData)
211- }
212- }
205+ await newExtractor
206+ . extractWaveform ( samplesPerPixel: noOfSamples, playerKey: playerKey,
207+ onExtractionComplete: { data in
208+ result ( data)
209+ } )
213210 }
214211 } catch {
215212 result ( FlutterError ( code: Constants . audioWaveforms, message: " Failed to decode audio file " , details: nil ) )
0 commit comments