我要在不同的词汇卡片视图中插入不同的短音频,如何在不同的imagebutton上播放不同的声音?(到目前为止,第一张卡片的图像按钮可以播放,但是除第一张卡片以外的其他卡片也可以播放)可以玩,但与第一张相同)
这是我的代码
import android.animation.ArgbEvaluator;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.haerul.swipeviewpager.PopOut.Y4;
import java.util.ArrayList;
import java.util.List;
public class Year4 extends AppCompatActivity {
private Button button;
private Button button2;
private Button button3;
private ImageButton soundbtn;
private MediaPlayer soundPlayer [] = new MediaPlayer[2];
private int soundResources[]={R.raw.village,R.raw.archaeologist,R.raw.astronaut};
MediaPlayer mp;
ViewPager viewPager;
com.haerul.swipeviewpager.Adapter.Y4 adapter;
List<com.haerul.swipeviewpager.Model.Y4> models;
Integer[] colors = null;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainy4);
Intent intent = getIntent();
for (int i = 0; i < soundPlayer.length; i++) {
soundPlayer[i] = MediaPlayer.create(Year4.this, soundResources[i]);
}
models = new ArrayList<>();
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.archaeologist, "Archaeologist", "Someone who studies the buildings, graves, tools, and other objects of people who lived in the past."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.astronauts, "Astronaut", "A person who has been trained for travelling in space."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.fishing, "Fishing", "The sport or job of catching fish."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.hardware, "Hardware", "The electronic parts of a computer"));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.market2, "Market", "The place who might want to buy something."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.refrigerator, "Refrigerator", "Kitchen equipment that uses electricity to preserve food at a cold temperature."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.stationery, "Stationery", "The things needed for writing."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.sprinkle, "Sprinkle", "To drop a few pieces or drops of something over a surface."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.market, "Supermarket", "A large shop which sells most types of food and other goods needed in the home."));
models.add(new com.haerul.swipeviewpager.Model.Y4(R.drawable.village, "Village", "A group of houses and other buildings that is smaller than a town, usually in the countryside."));
adapter = new com.haerul.swipeviewpager.Adapter.Y4(models, this);
viewPager = findViewById(R.id.viewPager);
soundbtn = findViewById(R.id.soundButton);
viewPager.setAdapter(adapter);
viewPager.setPadding(100, 0, 100, 0);
Integer[] colors_temp = {
getResources().getColor(R.color.color9),
getResources().getColor(R.color.color10),
getResources().getColor(R.color.color2),
getResources().getColor(R.color.color5),
getResources().getColor(R.color.color3),
getResources().getColor(R.color.color4),
getResources().getColor(R.color.color6),
getResources().getColor(R.color.color7),
getResources().getColor(R.color.color8),
getResources().getColor(R.color.color1),
};
colors = colors_temp;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position < (adapter.getCount() - 1) && position < (colors.length - 1)) {
viewPager.setBackgroundColor(
(Integer) argbEvaluator.evaluate(
positionOffset,
colors[position],
colors[position + 1]
)
);
} else {
viewPager.setBackgroundColor(colors[colors.length - 1]);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
button = findViewById(R.id.btnOrder);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openMain2Activity();
}
});
button2 = findViewById(R.id.btnOrder3);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openPopActivity();
}
});
button3 = findViewById(R.id.btnOrder2);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openScreenTestActivity();
}
});
}
public void openMain2Activity() {
Intent intent = new Intent(this, Y4Q1.class);
startActivity(intent);
}
public void openPopActivity() {
Intent intent = new Intent(this, Y4.class);
startActivity(intent);
}
public void openScreenTestActivity() {
Intent intent = new Intent(this, ScreenTestActivity.class);
startActivity(intent);
}
public void buttonClicked(View view) {
switch(view.getId()) {
case R.id.soundButton:
soundPlayer[0].start();
break;
default:
soundPlayer[1].start();
break;
}
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to back to title screen?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Year4.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}