2014-01-09 72 views
0

我正在製作一個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; 
} 
} 

現在,我想根據數組中的圖像播放不同的聲音。

+0

你可以添加監聽器onPageChanged和播放聲音 –

+0

其實我是新的android。我沒有多餘的知識,我只是開始建立你能告訴我該怎麼做? –

+0

可能重複[輕掃的圖像輕掃](http://stackoverflow.com/questions/21014928/swipe-images-with-little-sound) – Nfear

回答

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

我還沒有嘗試過這種想法,但這應該工作。或者至少給你一個關於如何進行的想法。

+0

嘿,我需要重寫此代碼? –

+0

你必須根據你的需要實現onPageChnageListener。我不知道你到底想做什麼。但在新pahe selectio上播放聲音肯定會像這樣工作 –

+0

我已經這樣做了。但聲音不與圖像同步。我用這個代碼來聲音。 if(mp!= null){mp.reset(); \t mp.release(); \t} \t \t mp = MediaPlayer.create(Numbers.this,mAudio [position]); \t \t \t \t mp.start(); \t \t \t \t return imageView; –