5

I am using following code to play sound in my app. Everything worked fine before ICS. But on ICS and more recent versions no sound can be heard although there is no error appearing.

EDIT: Note, the following code is triggered by a broadcase receiver. BroadCast receiver invokes a async task. In the post process method of asycn task the following method is called.

This error happens only on specific mobile models (Eg nexus ) and on some models users are able to play sounds which comes with stock os but not their own sounds which they places in rigtones folder.

I am not able get whats the issues as it happens on specific handset

What could the error possibly be?

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's 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 {

        }
    }

LOGS

: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 a follow up question to question which i asked previously: sound not playing in android > icecream sandwich

Community
  • 1
  • 1
user93796
  • 18,749
  • 31
  • 94
  • 150
  • Are you able to identify a sound that does play and one that doesn't and let me know the bitrate and format of the audio you're trying to play and on what device? – Ben Pearson Nov 11 '14 at 09:53
  • one of the format that cannot be played is mp3 – user93796 Nov 11 '14 at 12:15
  • There is nothing obviously wrong with your code – a sample app can play in the NOTIFICATION stream on my Nexus. I suggest you add an Error Listener, and remove the log filter you might have – interesting logs will come from MediaPlayer and NuPlayer. – rds Nov 17 '14 at 22:29

1 Answers1

2

You should be requesting audio focus, even for notifications. In your case it would 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 is some other app(s) running on the devices which are giving you problems which are requesting audio focus. If other apps request audio focus, and you do not, it is possible your app will not be played out to the final stream. Requesting focus yourself will ensure no other processes are interfering with your notification sound. Although this was introduced a while ago, newer versions of Android are much more sensitive to apps using this mechanism, especially from the background.

To be proper, when you're done with the notification, you can then release the focus:

audioManager.abandonAudioFocus(this);
Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55