2012-02-08 60 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 2012-02-08 13:35:52

+0

对不起,我没有让你请再来 – Goofy 2012-02-08 13:38:35

+1

对不起,你是什么意思? – iSun 2012-02-08 13:40:26

回答

0

此服务从您的原始数据中加载音频文件,您必须在您的活动中调用startService()以启动此服务e,别忘了将此服务添加到您的Android清单。

为什么你没有使用服务类EDITED

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

但这里它循环一个文件,但我有多个音频文件需要依次播放 – Goofy 2012-02-08 14:04:26

+0

请检查我的编辑请。 – iSun 2012-02-08 14:14:01