@@ -17,15 +17,7 @@ class SoundEffectPlugin: FlutterPlugin, MethodCallHandler {
17
17
private lateinit var channel : MethodChannel
18
18
private lateinit var binding: FlutterPlugin .FlutterPluginBinding
19
19
20
- private val soundPool: SoundPool = SoundPool .Builder ()
21
- .setMaxStreams(1 )
22
- .setAudioAttributes(
23
- AudioAttributes .Builder ()
24
- .setUsage(AudioAttributes .USAGE_GAME )
25
- .setContentType(AudioAttributes .CONTENT_TYPE_SONIFICATION )
26
- .build()
27
- ).build()
28
-
20
+ private var soundPool: SoundPool ? = null
29
21
private val audioMap = HashMap <String , Int >()
30
22
31
23
override fun onAttachedToEngine (flutterPluginBinding : FlutterPlugin .FlutterPluginBinding ) {
@@ -36,6 +28,21 @@ class SoundEffectPlugin: FlutterPlugin, MethodCallHandler {
36
28
37
29
override fun onMethodCall (call : MethodCall , result : Result ) {
38
30
when (call.method) {
31
+ " init" -> {
32
+ if (soundPool != = null ) {
33
+ result.error(" Already initialized" , null , null )
34
+ return
35
+ }
36
+ soundPool = SoundPool .Builder ()
37
+ .setMaxStreams(1 )
38
+ .setAudioAttributes(
39
+ AudioAttributes .Builder ()
40
+ .setUsage(AudioAttributes .USAGE_GAME )
41
+ .setContentType(AudioAttributes .CONTENT_TYPE_SONIFICATION )
42
+ .build()
43
+ ).build()
44
+ result.success(null )
45
+ }
39
46
" load" -> {
40
47
val audioId = call.argument<String >(" soundId" )
41
48
val path = call.argument<String >(" path" )
@@ -52,15 +59,15 @@ class SoundEffectPlugin: FlutterPlugin, MethodCallHandler {
52
59
try {
53
60
val assetPath = binding.flutterAssets.getAssetFilePathBySubpath(path)
54
61
val afd = binding.applicationContext.assets.openFd(assetPath)
55
- audioMap[audioId] = soundPool.load(afd, 1 )
62
+ audioMap[audioId] = soundPool!! .load(afd, 1 )
56
63
result.success(null )
57
64
} catch (e: Exception ) {
58
65
result.error(" Failed to load sound" , e.message, null )
59
66
}
60
67
}
61
68
" play" -> {
62
69
val audioId = call.argument<String >(" soundId" )
63
- val volume = call.argument<String >(" volume" )?.toFloat() ? : 1f
70
+ val volume = call.argument<Double >(" volume" )?.toFloat() ? : 1f
64
71
65
72
if (audioId == = null ) {
66
73
result.error(" Must supply a soundId" , null , null )
@@ -74,10 +81,16 @@ class SoundEffectPlugin: FlutterPlugin, MethodCallHandler {
74
81
return
75
82
}
76
83
77
- soundPool.play(audio, volume, volume, 1 , 0 , 1f )
84
+ soundPool? .play(audio, volume, volume, 1 , 0 , 1f )
78
85
79
86
result.success(null )
80
87
}
88
+ " release" -> {
89
+ soundPool?.release()
90
+ soundPool = null
91
+ audioMap.clear()
92
+ result.success(null )
93
+ }
81
94
else -> result.notImplemented()
82
95
}
83
96
}
0 commit comments