angular-语音文件播放

非angular情况单数据播放:

var audio =document.createElement("audio");

audio.src=me.global.res+audioObj.src; 

//audioObj是方法传入的参数对象,me.global.res是音频前缀

audio.play();

audio.addEventListener("play",

function() {//监听开始和结束

$(".voice span").addClass("audio-play");

},false);

audio.addEventListener("ended",function() {

$(".voice span").removeClass("audio-play").addClass("voice3");  //"audio-play"和“voice3”分别是播放时候的动画和播放(默认)结束的动画

},false)

angular数据绑定下播放单个:

showPlayAudio:function(audioObj){

varaudio =document.createElement("audio");

audio.src=me.global.res+audioObj.src;;

audio.play();

audio.addEventListener("play",

function() {//监听暂停

audioObj.audioStar=true;

that.$scope.$apply();

},false);

audio.addEventListener("ended",function() {

audioObj.audioStar=false;

$(".voice span").removeClass("audio-play");

that.$scope.$apply();

},false)

},

addEventListener脱离了ng的监管,属于临时工性质,故ng底层不会自动执行脏检测更新视图,需要手动$scope.$apply()强制执行脏检测刷新视图

控制css样式绑定在传过来的对象audioObj上,即audioStar

HTML部分:

<div ng-repeat = "d in audioList">

<p class="voice" ng-click="showPlayAudio(d)">

<span ng-class="{'voices':!d.audioStar,'audio-play':d.audioStar}"></span><i class="voice1"></i>

</p>

</div>

css部分:

.voice{width:100px;height:20px;margin-top:5px;line-height:20px;border:1px solid#E6E6E6;background-color:#f8f8f8;border-radius:10px;boxsizing: border-box;}

.audio-play{position: absolute;left:6px;animation:mymove1s infinite;-moz-animation:mymove1s infinite;/* Firefox */-webkit-animation:mymove1s infinite;/* Safari and Chrome */-o-animation:mymove1s infinite;/* Opera */}

@keyframesmymove{//注意兼容性

0%{top:7px;width:2px;height:3px;background:url("../images/voice_one.png")no-repeat;}

50%{top:4px;width:6px;height:9px;background:url("../images/voice_two.png")no-repeat;}

100%{top:2px;width:9px;height:13px;background:url("../images/voice_three.png")no-repeat;}

}

.voice3{position: absolute;left:6px;top:2px;width:9px;height:13px;background:url("../images/voice_three.png")no-repeat;}

如有问题欢迎指正

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 6,857评论 0 11
  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 8,397评论 0 11
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 5,865评论 0 2
  • 还有3公里就跑完了。 大汗淋漓的张旭深呼吸了一下,用灰黑的毛巾擦了擦汗,他再次调动全身的精力,继续跑最后一段路程。...
    美丽源于内心阅读 1,755评论 0 1
  • 今天在开发过程中定义了一个常量来标识一个view的高度 static const uint HeaderHeigh...
    紫剑香阅读 3,493评论 0 1