我在这里使用viewpager来刷我的图像&它在每个图像滑动上播放不同的声音。现在它的工作正常但是当我向后滑动时声音与图像不匹配...
我的代码: -
imagepageradapter
package com.android.learning_numbers;
public class ImagePagerAdapter {
}
主要活动
package com.android.learning_numbers;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Numbers extends Activity {
MediaPlayer mp;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
player = MediaPlayer.create(this, R.raw.jingle);
player.setLooping(true);
player.setVolume(100, 100);
player.start();
}
@Override
protected void onPause() {
super.onPause();
player.pause();
}
@Override
protected void onResume() {
super.onResume();
player.start();
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,R.drawable.no10,R.drawable.bg2,};
private int[] mAudio = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,R.raw.ten,R.raw.one,};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Numbers.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
if(position>=1){
mp = MediaPlayer.create(Numbers.this, mAudio[position-1]);
mp.start();
}
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
答案 0 :(得分:0)
将用于播放声音的MediaPlayer
对象和代码移至onPageChangeListener
对象的ViewPager
。目前,只有在实例化项目时才播放音频。
如果您将它们移至onPageChangeListener
,则无论是否实例化新项目,它都会在每次页面更改时播放。
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(player);
player = MediaPlayer.create(this, R.raw.jingle);
player.setLooping(true);
player.setVolume(100, 100);
player.start();
}
OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg0)
{
mp = MediaPlayer.create(Numbers.this, mAudio[arg0]);
mp.start();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
答案 1 :(得分:0)
我找到了答案。现在这是绝对有效的代码
public class Numbers extends Activity {
MediaPlayer mp;
MediaPlayer BG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setOnPageChangeListener(player);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
BG = MediaPlayer.create(this, R.raw.jingle);
BG.setLooping(true);
BG.setVolume(100, 100);
BG.start();
mp = MediaPlayer.create(Numbers.this, mAudio[0]);
mp.start();
}
@Override
protected void onPause() {
super.onPause();
BG.pause();
}
@Override
protected void onResume() {
super.onResume();
BG.start();
}
private int[] mAudio = new int[]{R.raw.one,
R.raw.two,
R.raw.three,
R.raw.four,
R.raw.five,
R.raw.six,
R.raw.seven,
R.raw.eight,
R.raw.nine,
R.raw.ten};
private int imageArra[] = {R.drawable.no1,
R.drawable.no2,
R.drawable.no3,
R.drawable.no4,
R.drawable.no5,
R.drawable.no6,
R.drawable.no7,
R.drawable.no8,
R.drawable.no9,
R.drawable.no10};
public OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg1)
{
mp = MediaPlayer.create(Numbers.this, mAudio[arg1]);
mp.start();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.numbers, menu);
return true;
}
}
答案 2 :(得分:0)
对我有用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_farm_animals);
ButterKnife.bind(this);
farmAnimalAdapter = new FarmAnimalAdapter(this);
viewPager.setOnPageChangeListener(players);
viewPager.setAdapter(farmAnimalAdapter);
}
public int[] animal_images = new int[]{
R.drawable.cow,
R.drawable.sheep,
R.drawable.horse,
R.drawable.goat
};
public int[] animal_sound = new int[]{
R.raw.cow,
R.raw.sheep,
R.raw.horse,
R.raw.goat
};
public String[] names = new String[] {
"COW","SHEEP","HORSE","GOAT"
};
ViewPager.OnPageChangeListener players= new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
//audio
MediaPlayer sound = MediaPlayer.create(getApplicationContext(),animal_sound[i]);
sound.start();
}
@Override
public void onPageScrollStateChanged(int i) {
}
};` public class FarmAnimalAdapter extends PagerAdapter{
Context mContext;
LayoutInflater mLayoutInflater;
public FarmAnimalAdapter(Context mContext) {
this.mContext = mContext;
this.mLayoutInflater = mLayoutInflater;
}
@Override
public int getCount() {
return names.length ;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
mLayoutInflater =(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = mLayoutInflater.inflate(R.layout.farmanimal_adapter_list,container,false);
ImageView txtimage = (ImageView)view.findViewById(R.id.txtimage);
AppCompatTextView txtname = (AppCompatTextView)view.findViewById(R.id.txtname);
txtimage.setImageResource(animal_images[position]);
txtname.setText(names[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout)object);
}
}
`
`