summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/MediaUtilImpl.java
blob: 272a057450a9db7daac352701346d65dce082514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.messaging.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;

/**
 * Default implementation of MediaUtil
 */
public class MediaUtilImpl extends MediaUtil {

    @Override
    public void playSound(final Context context, final int resId,
            final OnCompletionListener completionListener) {
        // We want to play at the media volume and not the ringer volume, but we do want to
        // avoid playing sound when the ringer/notifications are silenced. This is used for
        // in app sounds that are not critical and should not impact running silent but also
        // shouldn't play at full ring volume if you want to hear your ringer but don't want
        // to be annoyed with in-app volume.
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        try {
            final MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
            final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
            mediaPlayer.setDataSource(
                    afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            mediaPlayer.prepare();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(final MediaPlayer mp) {
                    if (completionListener != null) {
                        completionListener.onCompletion();
                    }
                    mp.stop();
                    mp.release();
                }
            });
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
           return;
        } catch (final Exception e) {
            LogUtil.w("MediaUtilImpl", "Error playing sound id: " + resId, e);
        }
        if (completionListener != null) {
            // Call the completion handler to not block functionality if audio play fails
            completionListener.onCompletion();
        }
    }
}