程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mediaplayer-不使用 OnCompletionListener() 如何動態的播放音頻文件?

mediaplayer-不使用 OnCompletionListener() 如何動態的播放音頻文件?

編輯:編程綜合問答
不使用 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文件夾獲取音頻文件,我還需要動態的播放這些文件。如何實現?

最佳回答:


這個文件從你的源文件中加載一個音頻文件,你應該在 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");
}   }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved