Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.messaging.util;
     17 
     18 import android.content.Context;
     19 import android.content.res.AssetFileDescriptor;
     20 import android.media.AudioManager;
     21 import android.media.MediaPlayer;
     22 
     23 /**
     24  * Default implementation of MediaUtil
     25  */
     26 public class MediaUtilImpl extends MediaUtil {
     27 
     28     @Override
     29     public void playSound(final Context context, final int resId,
     30             final OnCompletionListener completionListener) {
     31         // We want to play at the media volume and not the ringer volume, but we do want to
     32         // avoid playing sound when the ringer/notifications are silenced. This is used for
     33         // in app sounds that are not critical and should not impact running silent but also
     34         // shouldn't play at full ring volume if you want to hear your ringer but don't want
     35         // to be annoyed with in-app volume.
     36         AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
     37         try {
     38             final MediaPlayer mediaPlayer = new MediaPlayer();
     39             mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
     40             final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
     41             mediaPlayer.setDataSource(
     42                     afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
     43             afd.close();
     44             mediaPlayer.prepare();
     45             mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
     46                 @Override
     47                 public void onCompletion(final MediaPlayer mp) {
     48                     if (completionListener != null) {
     49                         completionListener.onCompletion();
     50                     }
     51                     mp.stop();
     52                     mp.release();
     53                 }
     54             });
     55             mediaPlayer.seekTo(0);
     56             mediaPlayer.start();
     57            return;
     58         } catch (final Exception e) {
     59             LogUtil.w("MediaUtilImpl", "Error playing sound id: " + resId, e);
     60         }
     61         if (completionListener != null) {
     62             // Call the completion handler to not block functionality if audio play fails
     63             completionListener.onCompletion();
     64         }
     65     }
     66 }