[FREE] 🔊 MKMusicPLayer Extention with remarkable features

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.


Blocks

hole blocks:


Screenshot 2



component_event (2)

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).




component_event (3)

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.




FadeInCompleted
Description: Event triggered when the fade-in effect has completed.




FadeOutCompleted
Description: Event triggered when the fade-out effect has completed.



component_method (6)

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.


PlayAudioFromURL

  • (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.


component_method (5)

PauseAudio`:

  • Pause the currently playing audio.




component_method (8)

ResumeAudio:

  • Resume the paused audio.


component_method (13)

StopAudio

  • Stop and release the MediaPlayer instance.


SetSeekPosition

  • (int position): Set the seek position of the audio file in milliseconds.


SetVolumeLevel
(

  • int volumeLevel) Control the volume level of the audio in the range of 0 to 100.




SetPlaybackSpeed

  • (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 )


component_method (9)

SetLooping

  • (boolean looping)`: Enable or disable looping of the audio.


GetVolumeLevel

  • 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.


component_method (1)

GetDuration

  • Get the total duration of the audio file in milliseconds.


HasAudioEnded

  • Check if the audio file has ended.



GetCurrentPosition

  • Get the current seek position of the audio file in milliseconds.


component_method (3)

IsLooping

  • Check if the audio is set to loop.


component_method (4)

IsPlaying

  • Check if the audio is currently playing.



FadeIn

  • 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.


FadeOut

  • 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.

RestartWithFadeIn

  • Description: Restarts the audio playback with a fade-in effect at the beginning.

  • Parameters:

    • duration: The duration of the fade-out effect in milliseconds.
    • initialVolume: An integer representing the starting volume level.

This MKMusicPlayer will solve these issues




Video


AIX file

You can download it from here :
MKMusicPlayer.aix (17.8 KB)


AIA file

You can download it from here :

MKplayer.aia (21.2 KB)


© Mr_koder. All rights reserved.

Thanks

Our Telegram Channel

Mr_Koder

2 Likes

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.

in this case, when we can get the duration of an audio? maybe we need an event like When Audio.Loaded.

thanks, @Anke
ok I will edit it as a response function instead.

thanks, @Kevinkun I will add this function too.

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.





two events were Added

2 Likes

new two blocks for FadeIn and FadeOut effects added

2 Likes

The extension now is FREE

Enjoy

  1. Playing from the assets does not work (at least not with the compiled app / APK):


    Companion only works as shown.

  2. FadeOut does not work as expected. It should fade out the sound at the end.

  3. Gapless looping does not work (also not with .ogg files).

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" :point_down:

over all thank you for testing my extension and for your helpful feedback

And that would be? I think you need a timer like this:

However, it is not a problem to fade in/out a sound without an extension using a clock.
So it should be possible without a timer.

  1. 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.
1 Like

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 .

1 Like

@Anke Now the problem solved :point_down:

I added this block too :point_down:

And this is an example for start and pause with fade in/out effect :point_down:

blocks

It doesn't work. Post a simple test aia.


How should this work without setting the Player.Source?
So post all relevant blocks & a test aia.

Btw, where is it?

this is only related to the play button I will post aia later

I will add it later cause I have exams related to my University ,
so I am busy at these times