在滑动图像时播放声音

时间:2014-01-09 09:57:34

标签: android

我正在制作一个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;
}
}

现在我想根据阵列中的图像播放不同的声音。

1 个答案:

答案 0 :(得分:0)

我认为这可以让您了解每次切换到下一个视图时如何播放声音

@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.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int pageId) {
            //TODO you will have to add some logic here that matches the
            // the pages id to the soundId
            playSound(this, pageId); 
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

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);
}

我没试过这个,但这应该有效。或者至少让您知道如何继续。

相关问题