Sound does not play in Android version greater than 4.0

I am using the following code to play sound in my application. Everything worked fine until ICS. But in ICS and later, no sound is heard, although the error does not appear.

EDIT . Please note that the following code is triggered by the receiver in broadband mode. The BroadCast receiver invokes an asynchronous task. The following method is called in the post-process method of the asycn task.

This error occurs only on certain mobile models (for example, nexus), and on some models users can play sounds that come with a margin, but not with their own sounds, which they place in a folder with bindings.

I can’t understand what problems are occurring on a particular phone.

What could be the mistake?

public static void playSound(final Context context, final int volume, Uri uri, final int stream, int maxTime, int tickTime) { //stopPlaying(); /* if (stream < 0 || stream > 100) { throw new IllegalArgumentException( "volume must be between 0 and 100 .Current volume " + volume); }*/ final AudioManager mAudioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); int deviceLocalVolume = getDeviceVolume(volume, mAudioManager.getStreamMaxVolume(stream)); Log.d(TAG, "device max volume = " + mAudioManager.getStreamMaxVolume(stream) + " for streamType " + stream); Log.d(TAG, "playing sound " + uri.toString() + " with device local volume " + deviceLocalVolume); final int oldVolume = mAudioManager.getStreamVolume(stream); // set the volume to what we want it to be. In this case it max volume // for the alarm stream. Log.d(Constants.APP_TAG, "setting device local volume to " + deviceLocalVolume); mAudioManager.setStreamVolume(stream, deviceLocalVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); final MediaPlayer mediaPlayer = new MediaPlayer(); golbalMMediaPlayer = mediaPlayer; try { final OnPreparedListener OnPreparedListener = new OnPreparedListener() { @Override public void onPrepared(final MediaPlayer mp) { Log.d(TAG, "onMediaPlayercompletion listener"); mp.start(); countDownTimer.start(); } }; mediaPlayer.setDataSource(context.getApplicationContext(), uri); mediaPlayer.setAudioStreamType(stream); mediaPlayer.setLooping(false); mediaPlayer.setOnPreparedListener(OnPreparedListener); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.d(Constants.APP_TAG, "Entered onCompletion listener of mediaplayer"); mAudioManager.setStreamVolume(stream, oldVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); try{ if(mediaPlayer != null && mediaPlayer.isPlaying()){ mediaPlayer.release(); } }catch(Exception ex){ Log.e(Constants.APP_TAG, "error on oncompletion listener" ,ex); } } }); CountDownTimer timer = new CountDownTimer(maxTime*1000, tickTime*1000) { @Override public void onTick(long millisUntilFinished) { Log.d(TAG, "tick while playing sound "); } @Override public void onFinish() { Log.d(TAG, "timer finished"); stopPlaying(); } }; countDownTimer = timer; mediaPlayer.prepareAsync(); } catch (Exception e) { Log.e(TAG, "problem while playing sound", e); } finally { } } 

MAGAZINES

 :07-01 00:00:00.030: D/beephourly(9500): device max volume = 7 for streamType 5 07-01 00:00:00.030: D/beephourly(9500): playing sound content://media/internal/audio/media/166 with device local volume 7 07-01 00:00:00.030: D/beephourly(9500): setting device local volume to 7 07-01 00:00:00.080: D/beephourly(9500): vibrating with pattern = [ J@428bae20 07-01 00:00:00.090: D/beephourly(9500): will show normal notification 07-01 00:00:00.100: D/beephourly(9500): notification is enabled 07-01 00:00:00.100: D/usersettings(9500): hr = 0 07-01 00:00:00.110: D/beephourly(9500): onMediaPlayercompletion listener 07-01 00:00:00.451: D/beephourly(9500): tick while playing sound 07-01 00:00:20.460: D/beephourly(9500): timer finished 07-01 00:00:20.460: D/beephourly(9500): got request to stop playing 07-01 00:00:20.460: D/beephourly(9500): cancelling countdowntimer 07-01 00:00:20.460: D/beephourly(9500): releasing mediaplayer now 

This question is the next question to the question I asked earlier: the sound does not play in android> ice cream sandwich

+5
source share
1 answer

You must request audio focus, even for notifications. In your case, it will look something like this:

 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //request a transient lock on the notification stream int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // could not get audio focus. } 

I suspect there are some other apps (apps) on the devices that give you problems that require audio focus. If other applications request sound focus and you do not, your application may not be played back in the last stream. The focus request itself will ensure that no other processes interfere with your notification sound. Although it was introduced some time ago, newer versions of Android are much more sensitive to applications that use this mechanism, especially because of the background.

To be correct, when you are done with the notification, you can release the focus:

 audioManager.abandonAudioFocus(this); 
+2
source

Source: https://habr.com/ru/post/1206512/


All Articles