cordova-plugin-nativeaudio
cordova-plugin-nativeaudio copied to clipboard
No sound in some android, but works in others
I have three android phones and two iphones, nativeaudio plugin works well on one android(4.2.2) and all iphone. there are two android(4.4.4, 4.2.1) can't hear anything.
I digged internet for whole weekend, found some weird thing. when I add these code in plugin, sounds goes back,then remove them all, compile again, sounds is still there. I cant expained it. I lost my incident ground, but something wrong in there.
// setting volume on MediaPlayer doesnt work
mp = new MediaPlayer();
mp.setVolume(volume, volume);
// force set volume on AudioManager, it works. but the code just get/set same value..
AudioManager audiMgr = (AudioManager)this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audiMgr.getStreamVolume(AudioManager.STREAM_RING);
audiMgr.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
Another useful info http://stackoverflow.com/a/15261422
Calling setVolume after calling prepare fixes this, so that audio is played at max volume. Actually, according to the docs I just quoted, you should call setLooping after prepare as well:
mediaPlayer.setDataSource(context, ringtoneUri); mediaPlayer.prepare(); mediaPlayer.setLooping(looping); mediaPlayer.setVolume(1.0f, 1.0f); mediaPlayer.start();
How does it in our plugin?
mp = new MediaPlayer(); mp.setOnCompletionListener(this); mp.setOnPreparedListener(this); mp.setDataSource( afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setVolume(volume, volume); <---- maybe something wrong here, I pointed it out but not sure mp.prepare();
I haven't inspected things on Android, but this issue might be similar to the how things were on iOS, where faded in playing of sounds gave the impression that no sound was playing
Anyway, a while ago, I tested audio performance at length and found out that the cordova audio performed similar to the audio plugins, so I'm using Media integration on Android - I don't know how things on your end, but you might also benefit from such a change (I load N Media entity's for a single sound effect and play them in turns, N varies from sound to sound)
I've started inspecting this issue too, turns out the Cordova/Media integration at android is also problematic, after upgrading to android5 on my S4, I've started experiencing sounds that sometimes don't play
Comparatively, this plugin doesn't work at all, the sounds don't initialise, there is no callback etc.
I've tested @janckerchen 's methods, the setVolume doesn't change anything, the audio manager one doesn't compile
I have a similar problem and I think is related to this. In my case the sounds play fine the first time but not the second, unless I wait a while. The callbacks are firing whether the sound plays or not. This only happens on Lollipop, there's no problem on kitkat.
I can confirm this plugin only works (plays) the sound once in Android 5. The second and successive times, the sound is not played at all.
I have switched to a different plugin, https://github.com/apache/cordova-plugin-media