自定义语音通知栏 NotificationManager

前段时间公司接了一个项目,是物流公司要弄一款像滴滴打车那样的app,其中有一需求是在手机弹出通知栏NotificationManager后还要实现自定义文字转语音(TTS)。因为以前没接触过TTS,所以刚开始的时候还是有点紧张的,好在办公位对面坐了一位大神,他说“讯飞”应该可以实现这功能,于是就迫不及待的科普了下,官网地址:http://www.xfyun.cn/,结果问题便游刃而解了,废话少说先看效果。

这里写图片描述

代码实现:
一:按API要求导入讯飞架包,如下图:
这里写图片描述

二:源码:

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    NotificationManager nm;
    static final int NOTIFICATION_ID = 0x123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        // 将“12345678”替换成您申请的APPID,申请地址:http://open.voicecloud.cn
        SpeechUtility.createUtility(MainActivity.this, SpeechConstant.APPID
                + "=12345678");
        // 获取系统的NotificationManager服务
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }

    /**
     * 合成监听器 
     */
    private SynthesizerListener mSynListener = new SynthesizerListener() {
        // 会话结束回调接口,没有错误时,error为null
        public void onCompleted(SpeechError error) {
        }

        // 缓冲进度回调
        // percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲音频在文本中结束位置,info为附加信息。
        public void onBufferProgress(int percent, int beginPos, int endPos,
                String info) {
        }

        // 开始播放
        public void onSpeakBegin() {
        }

        // 暂停播放
        public void onSpeakPaused() {
        }

        // 播放进度回调
        // percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音频在文本中结束位置.
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
        }

        // 恢复播放回调接口
        public void onSpeakResumed() {
        }

        // 会话事件回调接口
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
        }

    };

    /**
     * 触发事件的按钮
     * @param v
     */
    public void button(View v) {
        sendNotification();
        sendVoice();
    }

    /**
     * 发送语音
     */
    public void sendVoice() {
        // 1.创建SpeechSynthesizer对象, 第二个参数:本地合成时传InitListener
        SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(
                MainActivity.this, null);
        // 2.合成参数设置,详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer 类
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");// 设置发音人
        mTts.setParameter(SpeechConstant.SPEED, "50");// 设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "80");// 设置音量,范围0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 设置云端
        // 设置合成音频保存位置(可自定义保存位置),保存在“./sdcard/iflytek.pcm”
        // 保存在SD卡需要在AndroidManifest.xml添加写SD卡权限
        // 如果不需要保存合成音频,注释该行代码
        // mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
        // "./sdcard/iflytek.pcm");
        // 3.开始合成
        mTts.startSpeaking("全球监控系统遭到华裔黑客攻击,请所有休假人员即刻归位!", mSynListener);
    }

    /**
     * 发送自定义通知栏
     */
    public void sendNotification() {
        // 创建一个启动其他Activity的Intent
        Intent intent = new Intent(MainActivity.this, SendActivity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0,
                intent, 0);
        Notification notify = new Notification.Builder(this)
        // 设置打开该通知,该通知自动消失
                .setAutoCancel(true)
                // 设置显示在状态栏的通知提示信息
                .setTicker("有新消息")
                // 设置通知的图标
                .setSmallIcon(R.drawable.notify)
                // 设置通知内容的标题
                .setContentTitle("五角大楼办公室")
                // 设置通知内容
                .setContentText("全球监控系统遭到华裔黑客攻击,请所有休假人员即刻归位!")
                // // 设置使用系统默认的声音、默认LED灯
                // .setDefaults(Notification.DEFAULT_SOUND
                // |Notification.DEFAULT_LIGHTS)
                // 设置通知的自定义声音
                // .setSound(Uri.parse("android.resource://org.crazyit.ui/"+
                // R.raw.msg))
                .setWhen(System.currentTimeMillis())
                // 设改通知将要启动程序的Intent
                .setContentIntent(pi).build();
        // 发送通知
        nm.notify(NOTIFICATION_ID, notify);
    }

}

源码下载地址:http://download.csdn.net/detail/sunshine_mood/9546823

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Firebase 推送消息可以通过自定义通知来展示更加个性化的消息。 首先,在 Firebase 控制台中,你需要为你的应用程序创建一个自定义通知通道。在创建通道时,你可以指定通知的名称、描述和重要性级别等信息。 接下来,你需要在你的 Android 应用程序中创建一个自定义通知布局。你可以使用一个 XML 文件来定义布局,其中包含你想要显示的所有文本、图像和其他元素。然后,在接收到 Firebase 消息时,你可以使用 RemoteViews 类将自定义布局与通知相关联。 最后,在接收到 Firebase 消息时,你需要在代码中使用 NotificationCompat.Builder 类构建自定义通知。在构建通知时,你可以指定标题、文本和自定义布局等信息。最后,使用 NotificationManagerCompat 类将通知发送到系统通知中。 以下是一个示例代码片段,可以帮助你开始构建自定义通知: ``` // 创建自定义通知通道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(channelDescription); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 创建自定义通知布局 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); contentView.setTextViewText(R.id.title, notificationTitle); contentView.setTextViewText(R.id.text, notificationText); // 构建自定义通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.notification_icon) .setCustomContentView(contentView) .setPriority(NotificationCompat.PRIORITY_HIGH); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 注意:在使用自定义通知时,你需要确保你的应用程序有足够的权限来发送通知。如果你的应用程序没有这些权限,则无法正常发送通知
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值