I'm creating a java library to use in react native and I'm facing an error, I need to access the percentage of the music that is playing at run time through mediaPlayer.setOnBufferingUpdateListener, however, this doesn't seem to work, I've tried giving toast inside or even log but nothing happens and the application runs normally, what doesn't work is just the scope of the code, I'll share my play function:
@ReactMethod
public void play(ReadableMap song) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
isPlaying = false;
}
if (song != null) {
String url = song.getString("url");
if (url != null && !url.isEmpty()) {
mediaPlayer = new MediaPlayer();
try {
startProgressUpdate();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
isPlaying = true;
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(context, "End", Toast.LENGTH_SHORT).show();
}
});
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Toast.makeText(context, percent, Toast.LENGTH_SHORT).show();
Log.d("MusicPlayerModule", "Buffering update: " + percent);
}
});
String title = song.getString("title");
String artist = song.getString("artist");
musicPlayerNotificationHelper.showNotification(title, artist, 0);
} catch (Exception e) {
Log.e("MusicPlayerModule", "Error playing music: " + e.getMessage());
}
}
}
}
should return the percentage of the music in real time