Android Question [SOLVED] - MediaPlayer - Volume issues

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

We have several Samsung phones running on Android 10 and our app uses the MediaPlayer to play a background sound file. The issue we are experiencing involves the volume the MediaPlayer plays the sound file. It fluctuates. Randomly it get quite loud for a duration of a few seconds and could last a minute or so. Other installed apps on the phone that produce sound will also play above their normal volume only when our app is playing the background sound. If we stop our app from playing the sound the sound from the other apps return to normal.

Could this be also be a compatibility issue with Android 10?

Heres the coding we use to play the sound in our app.

B4X:
Sub tmrForCrickets_Tick
  
    If MediaPlayerCrickets.IsPlaying = False Then

        ' When a scheduled song has started this number will come from the schedule timer.
        ' When a scheduled song is not playing, it's number comes from the database which
        ' will have the song number selected by the user.
        '---------------------------------------------------------------------------------
        If intSongNumberSelected = 0 Then
            intSongNumberSelected = kvs.Get("CurrentSongSelected")
        End If

        If blnAsongIsPlaying And MediaPlayerCrickets.IsPlaying = False Then
      
            MediaPlayerCrickets.Load(File.DirAssets, _
                kvs.Get("Song" & intSongNumberSelected & "Creature0FileName"))

            MediaPlayerCrickets.SetVolume( _
                    kvs.Get("Song" & intSongNumberSelected & "Creature0Volume"), _
                    kvs.Get("Song" & intSongNumberSelected & "Creature0Volume"))

            MediaPlayerCrickets.Play
        End If
    End If
End Sub

The volume for the above coding is stored using KVS when the user adjusts a slider view as shown in the following coding.

B4X:
Sub B4XSeekBarVolume_TouchStateChanged (Pressed As Boolean)
  
    kvs.put( _
        "Song" & intCurrentSongSelected & _
        "Creature" & intCurrentCreatureSelected & "Volume", B4XSeekBarVolume.Value / 100)

    If ServiceModule.blnAsongIsPlaying Then
        CallSubDelayed(ServiceModule, "CancelTheSong")
        CallSubDelayed(ServiceModule, "PlayTheSong")

        If ServiceModule.blnScheduleIsPlayingSong Then
            CallSubDelayed(ServiceModule, "CancelSleepTimer")
        Else
            CallSubDelayed(ServiceModule, "StartSleepTimer")
        End If
    End If

    ToastMessageShow("Volume now set at " & B4XSeekBarVolume.Value & "%.", False)
End Sub

What additional coding will I need to add that will stop the MediaPlayer from randomly changing the volume of the sound file being played? The logs don't show any errors and using log statements shows the volume number never changes unless the user adjusts the slider.

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Use ExoPlayer it is far better then MediaPlayer.
Thanks for the reply.

We had to switch over to MediaPlayer because there were different volume issues with the ExoPlayer. With the ExoPlayer, the volume would drop completely if the app played the sound file for 2 hours and all other sound files in the app would not have volumes at all unless I force stopped the app and started it up again.

I do like ExoPlayer more because it's good with short duration sound files but couldn't use in in our app because the app plays background sound files.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I was able to get a middle ground and not have the volume issue in the app with the background sound file. The app uses 2 background sound files and about 18 other sound files that are generated randomly. The 18 sound files are monitored by timers and played when a random number equals a number selected by the user. The background sounds used to be controlled by timers but I changed them to play in a loop by the following code and I got the results I wanted by using the ExoPlayer I like more. Only the 2 background files are played by the ExoPlayer and the rest are with the MediaPlayer. I think maybe using the ExoPlayer struggled with processing 20 sound files at the same time and using it for 2 sound files was easier to process. Maybe that's why running it for 2 or more hours shut down the volume for everything when the app was using only the ExoPlayer for everything. At least with the way the app is coded now, the background sound files can play constantly without any issues.

Here is how I'm making the background files loop continuously.

B4X:
PlayerForCrickets.Initialize("PlayerForCrickets")
PlayerForWater.Initialize("PlayerForWater")

These sub routines are called when the sound files finish playing so the sound is uninterrupted.:
Sub PlayerForCrickets_Complete
    PlayTheCricketsBackground
End Sub

Sub PlayerForWater_Complete
    PlayTheWaterBackground
End Sub
 
Last edited:
Upvote 0
Top