@capgo/native-audio 5.1.3 → 5.1.10
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -36
- package/android/src/main/java/ee/forgr/audio/AudioAsset.java +115 -107
- package/android/src/main/java/ee/forgr/audio/AudioDispatcher.java +185 -168
- package/android/src/main/java/ee/forgr/audio/Constant.java +14 -13
- package/android/src/main/java/ee/forgr/audio/NativeAudio.java +430 -405
- package/dist/docs.json +26 -26
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +4 -4
- package/dist/esm/web.d.ts +3 -3
- package/dist/esm/web.js +20 -19
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +19 -18
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +19 -18
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/AudioAsset.swift +38 -45
- package/ios/Plugin/Constant.swift +1 -0
- package/ios/Plugin/Plugin.swift +15 -11
- package/package.json +14 -13
@@ -8,178 +8,195 @@ import android.util.Log;
|
|
8
8
|
import java.util.concurrent.Callable;
|
9
9
|
|
10
10
|
public class AudioDispatcher
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
11
|
+
implements
|
12
|
+
MediaPlayer.OnPreparedListener,
|
13
|
+
MediaPlayer.OnCompletionListener,
|
14
|
+
MediaPlayer.OnSeekCompleteListener {
|
15
|
+
|
16
|
+
private final String TAG = "AudioDispatcher";
|
17
|
+
|
18
|
+
private final int INVALID = 0;
|
19
|
+
private final int PREPARED = 1;
|
20
|
+
private final int PENDING_PLAY = 2;
|
21
|
+
private final int PLAYING = 3;
|
22
|
+
private final int PENDING_LOOP = 4;
|
23
|
+
private final int LOOPING = 5;
|
24
|
+
private final int PAUSE = 6;
|
25
|
+
|
26
|
+
private MediaPlayer mediaPlayer;
|
27
|
+
private int mediaState;
|
28
|
+
private AudioAsset owner;
|
29
|
+
|
30
|
+
public AudioDispatcher(AssetFileDescriptor assetFileDescriptor, float volume)
|
31
|
+
throws Exception {
|
32
|
+
mediaState = INVALID;
|
33
|
+
|
34
|
+
mediaPlayer = new MediaPlayer();
|
35
|
+
mediaPlayer.setOnCompletionListener(this);
|
36
|
+
mediaPlayer.setOnPreparedListener(this);
|
37
|
+
mediaPlayer.setDataSource(
|
38
|
+
assetFileDescriptor.getFileDescriptor(),
|
39
|
+
assetFileDescriptor.getStartOffset(),
|
40
|
+
assetFileDescriptor.getLength()
|
41
|
+
);
|
42
|
+
mediaPlayer.setOnSeekCompleteListener(this);
|
43
|
+
mediaPlayer.setAudioAttributes(
|
44
|
+
new AudioAttributes.Builder()
|
45
|
+
.setUsage(AudioAttributes.USAGE_MEDIA)
|
46
|
+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
47
|
+
.build()
|
48
|
+
);
|
49
|
+
mediaPlayer.setVolume(volume, volume);
|
50
|
+
mediaPlayer.prepare();
|
51
|
+
}
|
52
|
+
|
53
|
+
public void setOwner(AudioAsset asset) {
|
54
|
+
owner = asset;
|
55
|
+
}
|
56
|
+
|
57
|
+
public double getDuration() {
|
58
|
+
return mediaPlayer.getDuration() / 1000.0;
|
59
|
+
}
|
60
|
+
|
61
|
+
public double getCurrentPosition() {
|
62
|
+
return mediaPlayer.getCurrentPosition() / 1000.0;
|
63
|
+
}
|
64
|
+
|
65
|
+
public void play(Double time) throws Exception {
|
66
|
+
invokePlay(time, false);
|
67
|
+
}
|
68
|
+
|
69
|
+
public boolean pause() throws Exception {
|
70
|
+
if (mediaPlayer.isPlaying()) {
|
71
|
+
mediaPlayer.pause();
|
72
|
+
mediaState = PAUSE;
|
73
|
+
return true;
|
74
|
+
}
|
75
|
+
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
|
79
|
+
public void resume() throws Exception {
|
80
|
+
mediaPlayer.start();
|
81
|
+
}
|
82
|
+
|
83
|
+
public void stop() throws Exception {
|
84
|
+
if (mediaPlayer.isPlaying()) {
|
85
|
+
mediaState = INVALID;
|
86
|
+
mediaPlayer.pause();
|
87
|
+
mediaPlayer.seekTo(0);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
public void setVolume(float volume) throws Exception {
|
92
|
+
mediaPlayer.setVolume(volume, volume);
|
93
|
+
}
|
94
|
+
|
95
|
+
public void setRate(float rate) throws Exception {
|
96
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
mediaPlayer.setPlaybackParams(
|
100
|
+
mediaPlayer.getPlaybackParams().setSpeed(rate)
|
101
|
+
);
|
102
|
+
}
|
103
|
+
|
104
|
+
public void loop() throws Exception {
|
105
|
+
mediaPlayer.setLooping(true);
|
106
|
+
mediaPlayer.start();
|
107
|
+
}
|
108
|
+
|
109
|
+
public void unload() throws Exception {
|
110
|
+
this.stop();
|
111
|
+
mediaPlayer.release();
|
112
|
+
}
|
113
|
+
|
114
|
+
@Override
|
115
|
+
public void onCompletion(MediaPlayer mp) {
|
116
|
+
try {
|
117
|
+
if (mediaState != LOOPING) {
|
118
|
+
this.mediaState = INVALID;
|
102
119
|
|
103
|
-
public void unload() throws Exception {
|
104
120
|
this.stop();
|
105
|
-
mediaPlayer.release();
|
106
|
-
}
|
107
|
-
|
108
|
-
@Override
|
109
|
-
public void onCompletion(MediaPlayer mp) {
|
110
|
-
try {
|
111
|
-
if (mediaState != LOOPING) {
|
112
|
-
this.mediaState = INVALID;
|
113
121
|
|
114
|
-
|
115
|
-
|
116
|
-
if (this.owner != null) {
|
117
|
-
this.owner.dispatchComplete();
|
118
|
-
}
|
119
|
-
}
|
120
|
-
} catch (Exception ex) {
|
121
|
-
Log.d(TAG, "Caught exception while listening for onCompletion: " + ex.getLocalizedMessage());
|
122
|
+
if (this.owner != null) {
|
123
|
+
this.owner.dispatchComplete();
|
122
124
|
}
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
125
|
+
}
|
126
|
+
} catch (Exception ex) {
|
127
|
+
Log.d(
|
128
|
+
TAG,
|
129
|
+
"Caught exception while listening for onCompletion: " +
|
130
|
+
ex.getLocalizedMessage()
|
131
|
+
);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
@Override
|
136
|
+
public void onPrepared(MediaPlayer mp) {
|
137
|
+
try {
|
138
|
+
if (mediaState == PENDING_PLAY) {
|
139
|
+
mediaPlayer.setLooping(false);
|
140
|
+
} else if (mediaState == PENDING_LOOP) {
|
141
|
+
mediaPlayer.setLooping(true);
|
142
|
+
} else {
|
143
|
+
mediaState = PREPARED;
|
144
|
+
}
|
145
|
+
} catch (Exception ex) {
|
146
|
+
Log.d(
|
147
|
+
TAG,
|
148
|
+
"Caught exception while listening for onPrepared: " +
|
149
|
+
ex.getLocalizedMessage()
|
150
|
+
);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
private void seek(Double time) {
|
155
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
156
|
+
mediaPlayer.seekTo((int) (time * 1000), MediaPlayer.SEEK_NEXT_SYNC);
|
157
|
+
} else {
|
158
|
+
mediaPlayer.seekTo((int) (time * 1000));
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
private void invokePlay(Double time, Boolean loop) {
|
163
|
+
try {
|
164
|
+
boolean playing = mediaPlayer.isPlaying();
|
165
|
+
|
166
|
+
if (playing) {
|
167
|
+
mediaPlayer.pause();
|
168
|
+
mediaPlayer.setLooping(loop);
|
169
|
+
mediaState = PENDING_PLAY;
|
170
|
+
seek(time);
|
171
|
+
} else {
|
172
|
+
if (mediaState == PREPARED) {
|
173
|
+
mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
|
174
|
+
onPrepared(mediaPlayer);
|
175
|
+
seek(time);
|
143
176
|
} else {
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
private void invokePlay(Double time, Boolean loop) {
|
149
|
-
try {
|
150
|
-
boolean playing = mediaPlayer.isPlaying();
|
151
|
-
|
152
|
-
if (playing) {
|
153
|
-
mediaPlayer.pause();
|
154
|
-
mediaPlayer.setLooping(loop);
|
155
|
-
mediaState = PENDING_PLAY;
|
156
|
-
seek(time);
|
157
|
-
} else {
|
158
|
-
if (mediaState == PREPARED) {
|
159
|
-
mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
|
160
|
-
onPrepared(mediaPlayer);
|
161
|
-
seek(time);
|
162
|
-
} else {
|
163
|
-
mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
|
164
|
-
mediaPlayer.setLooping(loop);
|
165
|
-
seek(time);
|
166
|
-
}
|
167
|
-
}
|
168
|
-
} catch (Exception ex) {
|
169
|
-
Log.d(TAG, "Caught exception while invoking audio: " + ex.getLocalizedMessage());
|
177
|
+
mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
|
178
|
+
mediaPlayer.setLooping(loop);
|
179
|
+
seek(time);
|
170
180
|
}
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
181
|
+
}
|
182
|
+
} catch (Exception ex) {
|
183
|
+
Log.d(
|
184
|
+
TAG,
|
185
|
+
"Caught exception while invoking audio: " + ex.getLocalizedMessage()
|
186
|
+
);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
@Override
|
191
|
+
public void onSeekComplete(MediaPlayer mp) {
|
192
|
+
if (mediaState == PENDING_PLAY || mediaState == PENDING_LOOP) {
|
193
|
+
Log.w("AudioDispatcher", "play " + mediaState);
|
194
|
+
mediaPlayer.start();
|
195
|
+
mediaState = PLAYING;
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
public boolean isPlaying() throws Exception {
|
200
|
+
return mediaPlayer.isPlaying();
|
201
|
+
}
|
185
202
|
}
|
@@ -2,18 +2,19 @@ package ee.forgr.audio;
|
|
2
2
|
|
3
3
|
public class Constant {
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
public static final String ERROR_AUDIO_ID_MISSING = "Audio Id is missing";
|
6
|
+
public static final String ERROR_AUDIO_ASSET_MISSING =
|
7
|
+
"Audio Asset is missing";
|
8
|
+
public static final String ERROR_AUDIO_EXISTS = "Audio Asset already exists";
|
9
|
+
public static final String ERROR_ASSET_PATH_MISSING = "Asset Path is missing";
|
10
|
+
public static final String ERROR_ASSET_NOT_LOADED = "Asset is not loaded";
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
public static final String ASSET_ID = "assetId";
|
13
|
+
public static final String ASSET_PATH = "assetPath";
|
14
|
+
public static final String OPT_FADE_MUSIC = "fade";
|
15
|
+
public static final String OPT_FOCUS_AUDIO = "focus";
|
16
|
+
public static final String VOLUME = "volume";
|
17
|
+
public static final String RATE = "rate";
|
18
|
+
public static final String AUDIO_CHANNEL_NUM = "audioChannelNum";
|
19
|
+
public static final String LOOP = "loop";
|
19
20
|
}
|