我一直在学习(尝试:-D)Web Audio API。在下面的代码中我想播放 songs [] 数组中的歌曲列表。
但是我收到了错误......请帮助。提前谢谢你。
代码: -
var sources = new Array();
var actx;
var songs = ['src1.mp3'];
function start() {
console.log("WELCOME!!");
try{
actx = new AudioContext();
}catch(e){
console.log('WebAudio api is not supported!!');
}
var buffer_list =getBuffers(actx,songs);
//console.log("hello");
//console.log(buffer_list[1]);
for (var x = 0;x<(buffer_list.length);x++){
var i = actx.createBufferSource();
sources[x] = i ;
}
for (var x = 0;x<(buffer_list.length);x++){
sources[x].buffer = buffer_list[x];
}
for(var x = 0;x<(buffer_list.length);x++){
sources[x].connect(actx.destination);
}
}
function getBuffers(actx,songs){
var buffer_list = new Array();
for (var x = 0;x<songs.length;x++){
console.log(songs[x]);
var request = new XMLHttpRequest();
request.open('GET',songs[x],true);
request.responseType = 'arraybuffer';
//onload function for downloading,After sending request.
request.onload = function(){
var audioData = request.response;
//console.log(audioData);
if(audioData){
actx.decodeAudioData(audioData,function(buffer){
//console.log(buffer);
console.log(x);
buffer_list[x] = buffer;
},
function(e){
console.log(e.err);
});
}else {
console.error("problem!!");
}
}
request.send();
}
console.log(buffer_list);
return buffer_list;
}
function play() {
start();
sources[0].start(0);
//sources[1].start(0);
}
function stop(){
sources[0].stop(0);
//sources[1].stop(0);
}
当我在web控制台中进行调试时,我可以看到数组 buffer_list [] 的音频缓冲区位于 1 而不是0。我随后判断了值 X 。它来了 1 而不是 0 但为什么?我从 0 1 开始 x ?请帮忙.. 为了测试目的,我开始用歌曲[] 中的一首歌开始..
答案 0 :(得分:1)
您可以使用adapter.java
和public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public List<RecyclerItem> listItems;
private Context mContext;
public MyAdapter(List<RecyclerItem> listItems, Context mContext) {
this.listItems = listItems;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = holder.getAdapterPosition();
// if (position == 0) {
Toast.makeText(mContext, listItems.get(position).getTitle(),Toast.LENGTH_SHORT).show();
/*
}
if (position == 1) {
Toast.makeText(mContext,"Android",Toast.LENGTH_SHORT).show();
}
*/
}
});
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final RecyclerItem itemList = listItems.get(position);
holder.txtTitle.setText(itemList.getTitle());
holder.txtDescription.setText(itemList.getDescription());
holder.txtOptionDigit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Display option menu
PopupMenu popupMenu = new PopupMenu(mContext, holder.txtOptionDigit);
popupMenu.inflate(R.menu.option_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_item_save:
Toast.makeText(mContext, "Saved " + listItems.get(position).getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.mnu_item_delete:
//Delete item
Toast.makeText(mContext, "Deleted " + listItems.get(position).getTitle(), Toast.LENGTH_SHORT).show();
listItems.remove(position);
notifyDataSetChanged();
break;
default:
break;
}
return false;
}
});
popupMenu.show();
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView txtTitle;
public TextView txtDescription;
public TextView txtOptionDigit;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
txtDescription = (TextView) itemView.findViewById(R.id.txtDescription);
txtOptionDigit = (TextView) itemView.findViewById(R.id.txtOptionDigit);
}
}
}
等待完成多个异步任务
async/await