播放声音,而刷卡图片 [英] Play sound while swiping images

查看:178
本文介绍了播放声音,而刷卡图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做哪些用户刷通过不同图像的Andr​​oid应用程序。我希望在每个图像相应挥笔播放不同的声音。这是我的code看起来像

I am making an android app on which user swipes through different images. I want to play a different sound on each image swipes accordingly. This is my code looks like

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

现在在这里,我想根据这是数组中的图像播放不同的声音。

Now here i want to play different sounds according to the image which are in the array.

推荐答案

我想这应该给你如何播放声音每次你切换到下一个视图的想法

I think this should give you an idea on how to play a sound everytime your switch to the next View

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

我没有试过,但是这应该工作。或者至少给你如何进行的想法。

i haven't tried this, but this should work. Or at least give you an idea on how to proceed.

这篇关于播放声音,而刷卡图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆