2 myq 26 myq_26 于 2013.04.28 14:13 提问

不使用 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() 方法包含图像,并从assets文件夹获取音频文件,我还需要动态的播放这些文件。如何实现?

1个回答

clcurie_11
clcurie_11   2013.04.28 17:40
已采纳

这个文件从你的源文件中加载一个音频文件,你应该在 activity 中调用 startService()来开启这个服务,不要忘了在 manifest 文件中添加这个服务。

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");
}   }
Csdn user default icon
上传中...
上传图片
插入图片