2012-02-08 9 views
0

OncompletionListener()を使用せずにオーディオファイルを順番に再生する方法。ここOnCompletionListener()を使用せずにオーディオファイルを動的に再生する方法

が私のコードです:

mp.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       // TODO Auto-generated method stub 
       i = i + 1; 
       System.out.println("" + audio.length); 
       if(i < audio.length){ 
        img.setImageResource(image[i]); 
        try { 
         descriptor = getAssets().openFd(audio[i]); 
         mp.reset(); 
         mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength()); 
         descriptor.close(); 
         mp.prepare(); 
         mp.start(); 
         xml(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       } 

ここでは、メソッドのXMLは、()私は資産フォルダ

から選んでいた画像や音声ファイルが含まれており、また、私は動的に

これらのファイルを再生する必要があります

助けてください

+0

を編集しましたか? – iSun

+0

申し訳ありませんが、私はあなたにもう一度来てください。 – Goofy

+1

すみません、何を意味しますか? – iSun

答えて

0

このサービスはあなたの生のオーディオファイルをロードします、このサービスを開始するにはアクティビティでstartService()に電話する必要がありますe、あなたのアンドロイドマニフェストにこのサービスを追加することを忘れないでください。

あなたは、サービスクラスを使用していなかったのはなぜ

public class Backgroundmusic extends Service { 
    // Binder given to clients 
    private final IBinder mBinder = new LocalBinder(); 
    // Random number generator 
    private final Random mGenerator = new Random(); 

    private SoundPool soundPool; 
    private HashMap<Integer, Integer> soundsMap; 
    int SOUND1=1; 
    int SOUND2=2; 
@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 

    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); 
    soundsMap = new HashMap<Integer, Integer>(); 
    soundsMap.put(SOUND1, soundPool.load(this, R.raw.baby_laugh, 1)); 
    soundsMap.put(SOUND2, soundPool.load(this, R.raw.touchdown, 1)); 
} 
public void playSound(int sound, float fSpeed) { 
    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 
    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    float volume = streamVolumeCurrent/streamVolumeMax; 


    soundPool.play(soundsMap.get(sound), volume, volume, 1, 0, fSpeed); 
    } 


/** 
* Class used for the client Binder. Because we know this service always 
* runs in the same process as its clients, we don't need to deal with IPC. 
*/ 
public class LocalBinder extends Binder { 
    LocalService getService() { 
     // Return this instance of LocalService so clients can call public methods 
     return LocalService.this; 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

/** method for clients */ 
public int getRandomNumber() { 
    return mGenerator.nextInt(100); 
} 

public void soundPlay(int index){ 

    playSound(index, 1.0f); 
    Log.d("SOUND1","hi1"); 

} } 
+0

を助けてくれるとの一例を挙げてください。しかし、ここでは1つのファイルがループしますが、複数のオーディオファイルがあり、後で再生する必要があります。 – Goofy

+0

私の編集を確認してください。 – iSun

関連する問題