我正在使用Android应用程序,可以在图像滑动时播放背景声音。我已经完成了多个教程和发现代码&我试过同样但它对我不起作用。这是我试过的代码..
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.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Numbers extends Activity {
private static final OnPageChangeListener change = null;
public static MediaPlayer mp = null;
@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(change);}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.sun,R.drawable.sky,R.drawable.moon,};
private int[] mAudio= new int[]{R.raw.sun,R.raw.sky,R.raw.moon,};
@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);
return imageView;
}
public OnPageChangeListener change = new OnPageChangeListener() {
@Override public void onPageSelected(int position) {
Log.d(ACTIVITY_SERVICE, "OnPageSelected");
if
(mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Numbers.this, mAudio[position]);
mp.start();
}
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override public void onPageScrollStateChanged(int state) {}
};
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
正如我想的那样,图像滑动时类不会调用声音。我的代码中有错误吗?因为这里OnPageChangeListener根本不适合我。这是什么解决方案?如何修改适合我的声音代码?
答案 0 :(得分:1)
试试这个代码&根据您的需要进行更改..这对我有用..
public class Alphabets 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(Alphabets.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.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,
R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,};
private int imageArra[] = {R.drawable.ab,R.drawable.bc,R.drawable.cd,R.drawable.de,R.drawable.ef,R.drawable.fg,R.drawable.gh,R.drawable.hi,R.drawable.ij,R.drawable.jk,R.drawable.kl,R.drawable.lm,R.drawable.mn,
R.drawable.no,R.drawable.op,R.drawable.pq,R.drawable.qr,R.drawable.rs,R.drawable.st,R.drawable.tu,R.drawable.uv,R.drawable.vw,R.drawable.wx,R.drawable.xy,R.drawable.yz,R.drawable.zz};
public OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg1)
{
mp = MediaPlayer.create(Alphabets.this, mAudio[arg1]);
mp.stop();
}
@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;
}
}