您的位置:首页 > 其它

自定义语音通知栏 NotificationManager

2016-06-12 11:07 309 查看
前段时间公司接了一个项目,是物流公司要弄一款像滴滴打车那样的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息