@@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
22
22
import android .media .MediaPlayer ;
23
23
import android .media .MediaPlayer .OnCompletionListener ;
24
24
import android .media .MediaPlayer .OnErrorListener ;
25
+ import android .media .MediaPlayer .OnInfoListener ;
25
26
import android .media .MediaPlayer .OnPreparedListener ;
26
27
import android .media .MediaRecorder ;
27
28
import android .os .Environment ;
@@ -48,7 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
48
49
* android_asset: file name must start with /android_asset/sound.mp3
49
50
* sdcard: file name is just sound.mp3
50
51
*/
51
- public class AudioPlayer implements OnCompletionListener , OnPreparedListener , OnErrorListener {
52
+ public class AudioPlayer implements OnCompletionListener , OnPreparedListener , OnErrorListener , OnInfoListener {
52
53
53
54
// AudioPlayer modes
54
55
public enum MODE { NONE , PLAY , RECORD };
@@ -62,12 +63,18 @@ public enum STATE { MEDIA_NONE,
62
63
MEDIA_LOADING
63
64
};
64
65
66
+ // AudiorPlayer informations
67
+ public enum INFORMATION { MEDIA_BUFFERING_START ,
68
+ MEDIA_BUFFERING_END
69
+ };
70
+
65
71
private static final String LOG_TAG = "AudioPlayer" ;
66
72
67
73
// AudioPlayer message ids
68
74
private static int MEDIA_STATE = 1 ;
69
75
private static int MEDIA_DURATION = 2 ;
70
76
private static int MEDIA_POSITION = 3 ;
77
+ private static int MEDIA_INFO = 4 ;
71
78
private static int MEDIA_ERROR = 9 ;
72
79
73
80
// Media error codes
@@ -444,6 +451,8 @@ public float getDuration(String file) {
444
451
public void onPrepared (MediaPlayer player ) {
445
452
// Listen for playback completion
446
453
this .player .setOnCompletionListener (this );
454
+ // Listen for info aboutplayback
455
+ this .player .setOnInfoListener (this );
447
456
// seek to any location received while not prepared
448
457
this .seekToPlaying (this .seekOnPrepared );
449
458
// If start playing after prepared
@@ -493,6 +502,20 @@ public boolean onError(MediaPlayer player, int arg1, int arg2) {
493
502
return false ;
494
503
}
495
504
505
+ @ Override
506
+ public boolean onInfo (MediaPlayer player , int what , int extra ) {
507
+ LOG .d (LOG_TAG , "AudioPlayer.onInfo(" + what + ", " + extra + ")" );
508
+ switch (what ) {
509
+ case MediaPlayer .MEDIA_INFO_BUFFERING_START :
510
+ sendInfo (INFORMATION .MEDIA_BUFFERING_START );
511
+ break ;
512
+ case MediaPlayer .MEDIA_INFO_BUFFERING_END :
513
+ sendInfo (INFORMATION .MEDIA_BUFFERING_END );
514
+ break ;
515
+ }
516
+ return false ;
517
+ }
518
+
496
519
/**
497
520
* Set the state and send it to JavaScript.
498
521
*
@@ -505,6 +528,15 @@ private void setState(STATE state) {
505
528
this .state = state ;
506
529
}
507
530
531
+ /**
532
+ * Send information to Javascript.
533
+ *
534
+ * @param info
535
+ */
536
+ private void sendInfo (INFORMATION info ) {
537
+ sendStatusChange (MEDIA_INFO , null , (float )info .ordinal ());
538
+ }
539
+
508
540
/**
509
541
* Set the mode and send it to JavaScript.
510
542
*
0 commit comments