我正在製作一個Android應用程序,用戶在其中滑動瀏覽不同的圖像。我想在每個圖像上播放不同的聲音。這是我的代碼看起來像在滑動圖像時播放聲音
package com.horizontalscrollviewwithpageindicator;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.media.AudioManager;
import android.media.SoundPool;
public class PageIndicatorActivity extends Activity {
private static final Object S1 = null;
private static final Object S2 = null;
private static final Object S3 = null;
@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);
}
public class OurSoundPlayer{
public static final int S1 = R.raw.ss;
public static final int S2 = R.raw.uu;
public static final int S3 = R.raw.rr;
}
private static SoundPool soundPool;
private static HashMap soundPoolMap;
public static void initSounds(Context context) {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap(3);
soundPoolMap.put(S1, soundPool.load(context, R.raw.ss, 1));
soundPoolMap.put(S2, soundPool.load(context, R.raw.uu, 2));
soundPoolMap.put(S3, soundPool.load(context, R.raw.rr, 3));
}
public static void playSound(Context context, int soundID) {
if(soundPool == null || soundPoolMap == null){
initSounds(context);
}
float volume = (float) 1.0;
soundPool.play((Integer) soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);
}
private int imageArra[] = {R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
現在,我想根據數組中的圖像播放不同的聲音。
你可以添加監聽器onPageChanged和播放聲音 –
其實我是新的android。我沒有多餘的知識,我只是開始建立你能告訴我該怎麼做? –
可能重複[輕掃的圖像輕掃](http://stackoverflow.com/questions/21014928/swipe-images-with-little-sound) – Nfear