美文网首页
自定义通知声音

自定义通知声音

作者: 遇见_未见 | 来源:发表于2016-09-09 13:16 被阅读17次

用MediaPlayer实现自定义声音,本来是不需要先把mediaplayer置空的,但是如果需要连续播放声音的时候,会出现隔一段时间(应该是音频的长度)才成功调用一次,而我需要的是后边的调用会覆盖前边的,也就是短时间多次调用会在最后完整的播放;本来想通过mediaPlayer.stop()方法来停止之前的调用,但是没有效果。

public class SoundUtil {
    private static final String TAG = "SoundUtil";
    private static MediaPlayer mediaPlayer = null;
    public static void playSound(Context context) {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            }
            mediaPlayer = MediaPlayer.create(context, R.raw.beep);
            mediaPlayer.start();
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG, "playSound_Ex: " + ex.getMessage());
        }
    }
    public static void relaseMediaPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

在网上看到另一种方法:
原文地址:http://blog.csdn.net/rzleilei/article/details/17792437

private static Ringtone ring;

//将自定义声音当做铃声播放
public static void playNotify(Context context) { 
   if (ring == null) {
        String uri = "android.resource://" + context.getPackageName() + "/" + R.raw.notify;
        Uri no = Uri.parse(uri);
        ring = RingtoneManager.getRingtone(context, no);
    } 
   if (ring.isPlaying()) { 
       ring.stop();
    }
    ring.play();
}

相关文章

  • 自定义通知声音

    用MediaPlayer实现自定义声音,本来是不需要先把mediaplayer置空的,但是如果需要连续播放声音的时...

  • iOS 自定义通知声音

    官方文档 https://developer.apple.com/library/content/document...

  • iOS 自定义通知声音

    项目中遇到需要自定义通知声音的需求,以前没做过,就查了下官方文档,就像文档上说的,实现起来确实挺简单,就整理下当做...

  • iOS 自定义通知声音

    iOS10+的本地通知开始使用“UNUserNotificationCenter”,由于系统通知的声音太单调,总有...

  • iOS自定义通知声音

    场景 在消息推送里面播放自定义生成的声音 解决方案 生成自定义声音文件后,必须要写入到 【/Library/Sou...

  • iOS 自定义通知声音

    真!的!很!简!单!可以给不同类型的推送指定不同的通知音效。 一、准备好铃声文件。 目前只支持:Linear PC...

  • Android和iOS自定义通知声音

    Android 自定义通知声音 在安卓开发中、很多时候要使用通知提醒用户、那么使用通知就会设计到通知的提示音、那么...

  • 推送自定义声音的设置

    简单记录一下推送自定义声音 关于推送,官方文档、各种第三方推送文档都很全,就跳过了。 由于自定义通知声音还是由 i...

  • iOS中的设计模式

    一,通知自定义通知

  • RemoteView

    常驻通知栏 Notification RemoteView 自定义通知栏中,需要自定义通知栏的视图时,需要使用Re...

网友评论

      本文标题:自定义通知声音

      本文链接:https://www.haomeiwen.com/subject/vnseettx.html