我试图嵌入音频文件到HTML与音频标签。用于播放音频的src来自API,我有5-6个不同的音频文件.
当我单击playStart()时,播放的音频文件是相同的,尽管src显示了不同的链接
<tbody>
<tr ng-repeat="val in values track by $index">
<td ng-bind="$index +1">1</td>
<td ng-bind="val._id">ED1500322</td>
<td>
<audio id="audioTag">
<source controls ng-src="{{val.file}}"></source>
</audio>
<button class="play-btn" ng-click="playStart()"><img src="app_static/images/play.png"></button>
<button class="play-btn" ng-click="playStop()">stop</button>
</td>
<td ng-bind="val.result"></td>
</tr>
</tbody>Js文件
$scope.playStart = function() {
var audio = document.getElementById("audioTag");
audio.load();
audio.play();
};
$scope.playStop = function() {
var audio = document.getElementById("audioTag");
audio.pause();
audio.currentTime = 0;
};我只听到相同的音频剪辑,即使DOM显示链接是不同的
如能从api中获得音乐,请提供任何帮助。
发布于 2016-05-17 09:10:07
使用类:
<tbody>
<tr ng-repeat="val in values track by $index">
<td ng-bind="$index +1">1</td>
<td ng-bind="val._id">ED1500322</td>
<td>
<audio class="audioTag">
<source controls ng-src="{{val.file}}"></source>
</audio>
<button class="play-btn" ng-click="playStart($index)"><img src="app_static/images/play.png"></button>
<button class="play-btn" ng-click="playStop($index)">stop</button>
</td>
<td ng-bind="val.result"></td>
</tr>
JS
$scope.playStart = function(index_) {
var audio = document.getElementsByClassName("audioTag")[index_];
audio.load();
audio.play();
};
$scope.playStop = function() {
var audio = document.getElementsByClassName("audioTag")[index_];
audio.pause();
audio.currentTime = 0;
};发布于 2016-05-17 09:05:44
你的问题是
document.getElementById("audioTag")始终返回页面中的第一个元素。
您可以做的是更改id,这样每个音频元素都会有不同的id。
变化
<audio id="audioTag">至
<audio id="audioTag{{$index}}">所以元素将有标签"audioTag1","audioTag2“。
之后,将对playStart和playStop函数的调用更改为:
playStart('audioTag{{$index}}')因此,您将得到id作为参数。
你的职能是:
$scope.playStart = function(id) {
var audio = document.getElementById(id);
audio.load();
audio.play();
};就这样。
https://stackoverflow.com/questions/37271357
复制相似问题