我正在学习android开发,因为在观看教程后我创建了这个应用程序,其中使用viewpager扫描图像。但是现在如果我想在每个图像滑动上播放不同的声音?这有可能吗?这是我的代码
PageIndicatiorActivity
package com.horizontalscrollviewwithpageindicator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.media.MediaPlayer;
public class PageIndicatorActivity extends Activity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
myPager.setOnPageChangeListener(player);
}
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};
OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg0)
{
mp = MediaPlayer.create(PageIndicatorActivity.this, mAudio[arg0]);
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.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
我建议您让Activity
或Adapter
实施ViewPager.OnPageChangeListener
界面。
<强> http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html 强>
查看界面的方法并在那里实现“声音”。
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
答案 1 :(得分:0)
SuRaj Prince,反转调用的顺序,它应该工作,所以首先设置ChangeListener,然后调用setCurrentItem。
此外,如果您只播放短音,请使用SoundPool而不是MediaPlayer。
答案 2 :(得分: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;
}
}