Hello Developers ! today Iam introducing MKMusicPlayer Extension
The MKMusicPlayer extension is a non-visible component that provides audio playback functionality for your MIT App Inventor project. You can play audio files from local storage or a URL, control playback (play, pause, resume, stop), set the playback speed, control volume, and set looping.
The extension requires READ_EXTERNAL_STORAGE and INTERNET permissions.
AudioLoaded event
The AudioLoaded event is triggered when the audio is loaded and prepared for playback. It has one parameter, success , which is a boolean value indicating whether the audio was successfully loaded (true) or not (false).
AudioEnded event
The AudioEnded event is triggered when the audio playback has finished. It does not have any parameters and does not return any value.
FadeOutCompleted
Description: Event triggered when the fade-out effect has completed.
PlayAudio
(String filePath): Play an audio file from the given local storage path.
It releases any existing MediaPlayer instance, creates a new one, and starts playing the audio.
(String url):Play an audio file from the given URL. It releases any existing MediaPlayer instance, creates a new one, and starts playing the audio after preparing it asynchronously.
(float speed) Set the playback speed of the audio file. This function is supported on Android devices running API level 23 (Marshmallow) and above.
this take float speed like these values (0.25 , 0.5 , 1 , 1.5 ,2 ,2.5 , 3 )
SetLooping
(boolean looping)`: Enable or disable looping of the audio.
This function retrieves the current volume level of the audio being played by the media player. The volume level is represented as a value ranging from 0 to 100.
GetDuration
Get the total duration of the audio file in milliseconds.
Description: This function applies a fade-in effect to a media player, gradually increasing the volume from 0 to the final volume level over a specified duration.
By using this function, you can achieve a fade-in effect by gradually increasing the volume of a mediaPlayer from 0 to a specified level over a given duration.
Parameters:
duration: An integer representing the fade-in duration in milliseconds.
finalVolume: An integer representing the desired volume level at the end of the fade-in.
Description: This function applies a fade-out effect to a media player, gradually decreasing the volume from the initial volume level to 0 over a specified duration.
By using this function, you can achieve a fade-out effect by gradually decreasing the volume of a mediaPlayer from the initial volume level to 0 over a given duration. The mediaPlayer will be paused after the fade-out is completed.
Parameters:
duration: The duration of the fade-out effect in milliseconds.
initialVolume: An integer representing the starting volume level.
A nice feature to be able to adjust the speed. But shouldn't there be at least a Player.Completed event. Otherwise .HasAudioEnded would have to be queried via a timer. This is annoying and unnecessary.
Is the extension largely based on the Android MediaPlayer? If so, then the looping method shouldn't be able to create gapless loops.
Yes, this extension is largely based on the Android MediaPlayer. The looping functionality in this extension is provided by the SetLooping(boolean looping) method, which internally calls the mediaPlayer.setLooping(looping) method on the MediaPlayer instance.
You are correct that the looping method in Android's MediaPlayer does not guarantee gapless looping. When using the setLooping(true) method in MediaPlayer, there might be a small gap or glitch in between the end and the beginning of the loop, depending on the audio file format and the device's capabilities.
yes the extension plays audio from the file path the only way to right now to play from assets is to play it as an ASD path of the app and play it but I will edit it to play from file name
I set it like this to be more customizable so you can fade out at any point of the audio for example when you pause the audio it makes a cool smooth effect of pausing and fade in for start too and you can make it fade out at the end by simple procedure method too
I use MediaPlayerliberary and Unfortunately, the MediaPlayer class in Android does not support gapless looping out of the box, and this limitation applies to all audio formats, including .ogg files.
To achieve gapless looping, I would need to use a different approach or a different audio library that provides support for seamless looping.
but you can enable the loop effect to make the audio looping it working for me just enter "true" or "false"
over all thank you for testing my extension and for your helpful feedback
When fading in, the volume ends at 100%, when fading out, it starts at 100% (regardless of the previously set volume). However, many / most users only want to hear a sound at a certain preset volume. It should therefore be possible to specify not only the duration for the fade in/out, but also the volume to fade in to and fade out from.
Yes, it's done this way because in the majority of music player scenarios, you need to employ a clock mechanism to obtain the current position and utilize a slider control to manage it.
ok no problem I will try to add this option
Yes, I overlooked that. Thank you for bringing it to my attention. I appreciate your alertness.
no problem I will add the volume parameter to both of them to fade in/out .