@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.
@@ -8,178 +8,195 @@ import android.util.Log;
8
8
  import java.util.concurrent.Callable;
9
9
 
10
10
  public class AudioDispatcher
11
- implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnSeekCompleteListener {
12
-
13
- private final String TAG = "AudioDispatcher";
14
-
15
- private final int INVALID = 0;
16
- private final int PREPARED = 1;
17
- private final int PENDING_PLAY = 2;
18
- private final int PLAYING = 3;
19
- private final int PENDING_LOOP = 4;
20
- private final int LOOPING = 5;
21
- private final int PAUSE = 6;
22
-
23
- private MediaPlayer mediaPlayer;
24
- private int mediaState;
25
- private AudioAsset owner;
26
-
27
- public AudioDispatcher(AssetFileDescriptor assetFileDescriptor, float volume) throws Exception {
28
- mediaState = INVALID;
29
-
30
- mediaPlayer = new MediaPlayer();
31
- mediaPlayer.setOnCompletionListener(this);
32
- mediaPlayer.setOnPreparedListener(this);
33
- mediaPlayer.setDataSource(
34
- assetFileDescriptor.getFileDescriptor(),
35
- assetFileDescriptor.getStartOffset(),
36
- assetFileDescriptor.getLength()
37
- );
38
- mediaPlayer.setOnSeekCompleteListener(this);
39
- mediaPlayer.setAudioAttributes(
40
- new AudioAttributes.Builder()
41
- .setUsage(AudioAttributes.USAGE_MEDIA)
42
- .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
43
- .build()
44
- );
45
- mediaPlayer.setVolume(volume, volume);
46
- mediaPlayer.prepare();
47
- }
48
-
49
- public void setOwner(AudioAsset asset) {
50
- owner = asset;
51
- }
52
-
53
- public double getDuration() {
54
- return mediaPlayer.getDuration() / 1000.0;
55
- }
56
-
57
- public double getCurrentPosition() {
58
- return mediaPlayer.getCurrentPosition() / 1000.0;
59
- }
60
-
61
- public void play(Double time, Callable<Void> callable) throws Exception {
62
- invokePlay(time, false);
63
- }
64
-
65
- public boolean pause() throws Exception {
66
- if (mediaPlayer.isPlaying()) {
67
- mediaPlayer.pause();
68
- mediaState = PAUSE;
69
- return true;
70
- }
71
-
72
- return false;
73
- }
74
-
75
- public void resume() throws Exception {
76
- mediaPlayer.start();
77
- }
78
-
79
- public void stop() throws Exception {
80
- if (mediaPlayer.isPlaying()) {
81
- mediaState = INVALID;
82
- mediaPlayer.pause();
83
- mediaPlayer.seekTo(0);
84
- }
85
- }
86
-
87
- public void setVolume(float volume) throws Exception {
88
- mediaPlayer.setVolume(volume, volume);
89
- }
90
-
91
- public void setRate(float rate) throws Exception {
92
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
93
- return;
94
- }
95
- mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(rate));
96
- }
97
-
98
- public void loop() throws Exception {
99
- mediaPlayer.setLooping(true);
100
- mediaPlayer.start();
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
- this.stop();
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
- @Override
126
- public void onPrepared(MediaPlayer mp) {
127
- try {
128
- if (mediaState == PENDING_PLAY) {
129
- mediaPlayer.setLooping(false);
130
- } else if (mediaState == PENDING_LOOP) {
131
- mediaPlayer.setLooping(true);
132
- } else {
133
- mediaState = PREPARED;
134
- }
135
- } catch (Exception ex) {
136
- Log.d(TAG, "Caught exception while listening for onPrepared: " + ex.getLocalizedMessage());
137
- }
138
- }
139
-
140
- private void seek(Double time) {
141
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
142
- mediaPlayer.seekTo((int) (time * 1000), MediaPlayer.SEEK_NEXT_SYNC);
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
- mediaPlayer.seekTo((int) (time * 1000));
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
- @Override
174
- public void onSeekComplete(MediaPlayer mp) {
175
- if (mediaState == PENDING_PLAY || mediaState == PENDING_LOOP) {
176
- Log.w("AudioDispatcher", "play " + mediaState);
177
- mediaPlayer.start();
178
- mediaState = PLAYING;
179
- }
180
- }
181
-
182
- public boolean isPlaying() throws Exception {
183
- return mediaPlayer.isPlaying();
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
- public static final String ERROR_AUDIO_ID_MISSING = "Audio Id is missing";
6
- public static final String ERROR_AUDIO_ASSET_MISSING = "Audio Asset is missing";
7
- public static final String ERROR_AUDIO_EXISTS = "Audio Asset already exists";
8
- public static final String ERROR_ASSET_PATH_MISSING = "Asset Path is missing";
9
- public static final String ERROR_ASSET_NOT_LOADED = "Asset is not loaded";
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
- public static final String ASSET_ID = "assetId";
12
- public static final String ASSET_PATH = "assetPath";
13
- public static final String OPT_FADE_MUSIC = "fade";
14
- public static final String OPT_FOCUS_AUDIO = "focus";
15
- public static final String VOLUME = "volume";
16
- public static final String RATE = "rate";
17
- public static final String AUDIO_CHANNEL_NUM = "audioChannelNum";
18
- public static final String LOOP = "loop";
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
  }