Play sound on another channel with MediaPlayer

cziter15

New Member
[RESOLVED] Play sound on another channel with MediaPlayer

Hello,

I saw native MediaPlayer has a function: setAudioStreamType, wchich allows to play sound as ALARM, SYSTEM.. etc.

I want to mute MUSIC channel, because my app says some informations to user.
But, media player supports only MUSIC channel, and i'm confused.

How to play mp3 file on another channel ?

Regards,
Chris
 
Last edited:

cziter15

New Member
Just decompiled MediaPlayer.jar and saw that it wraps mediaplayer to local protected variable: protected MediaPlayer mp;

I used this code to set audio stream type, I think it may help someone:
B4X:
Dim rf As Reflector
rf.Target = mp
rf.Target = rf.GetField("mp")
rf.RunMethod2("setAudioStreamType","4","java.lang.int")

4 is the value of an ALARM stream type.

Regards,
Chris
 
Last edited:
Upvote 0

Widiar

Member
Licensed User
Longtime User
Quite an old discussion and there seems to be several audio classes available already. However, I fought quite long with this one and found out you can play to different (volume) channels using Mediaplayer with the given reflector method, IF you load the file AFTER the reflector call.

In my Google Nexus I can play using SYSTEM/NOTIFICATION/DTMF/RING channels, anything EXCEPT for the ALARM and VOICE_CALL channels, for reason unknown. (Well, voice_call makes sense, sort of..)

As the method description says as follows:

public void setAudioStreamType (int streamtype)
Added in API level 1
Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.


So, a code like this:

B4X:
mp.Initialize2("MediaPlayer")
mp.looping=False
Dim rf As Reflector
rf.Target = mp
rf.Target = rf.GetField("mp")
rf.RunMethod2("setAudioStreamType", rf.GetStaticField("android.media.AudioManager","STREAM_RING"),"java.lang.int")
mp.Load(File.DirAssets, SoundStyle)

Seems to work just fine, as the Mediaplayer apparently does that "prepare()"-method when loading a new file, not in the initialize-stage.

I have no idea why the ALARM channel does not work when all the others do. Might be that my Nexus (google version) uses some other name for the alarm channel or just come 3rd party software I am using (a lot of them) is blocking the channel.

Available channel constants are:


intSTREAM_ALARMThe audio stream for alarms
intSTREAM_DTMFThe audio stream for DTMF Tones
intSTREAM_MUSICThe audio stream for music playback
intSTREAM_NOTIFICATIONThe audio stream for notifications
intSTREAM_RINGThe audio stream for the phone ring
intSTREAM_SYSTEMThe audio stream for system sounds
intSTREAM_VOICE_CALLThe audio stream for phone calls
intUSE_DEFAULT_STREAM_TYPESuggests using the default stream type.
 
Upvote 0
Top