Lập trình Android: Sử dụng SoundPool pot

4 861 1
Lập trình Android: Sử dụng SoundPool pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Ứng dụng SoundPool SoundPool là 1 công cụ trên Android giúp các bạn có thể mở nhiều file Audio vào cùng 1 thời điểm. Trong phần demo này mình sẽ tạo nên 1 ứng dụng chạy đồng thời 4 file MP3 trên Virtual Device . Các bước để tạo ứng dụng như sau: 1/ Tạo Project : Project name: SoundPoolDemo Build Target: Android 2.3.3 Application name: SoundPoolDemo Package name: com.dac.soundpool Create Activity: MainActivity 2/ Trong file main.xml các bạn thiết kế giao diện đơn giản như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ToggleButton android:id="@+id/button" android:textOn="Pause" android:textOff="Resume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:checked="true" /> </LinearLayout> 3/ Các bạn tạo thêm 1 folder raw trong res/ và add vào đó 4 file nhạc MP3. Mình đặt tên lần lượt là : chimp.mp3, crickets.mp3, rooster.mp3, music_flie.mp3 4/ Trong file MainActivity.java các bạn code xử lý như sau: package com.dac.soundpool Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.widget.ToggleButton; public class MainActivity extends Activity implements SoundPool.OnLoadCompleteListener { private static final int SRC_QUALITY = 0; private static final int PRIORITY = 1; private SoundPool soundPool = null; private AudioManager aMgr; private int sid_background; private int sid_roar; private int sid_bark; private int sid_chimp; private int sid_rooster; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onResume() { soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, SRC_QUALITY); soundPool.setOnLoadCompleteListener(this); aMgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); sid_background = soundPool.load(this, R.raw.crickets, PRIORITY); sid_chimp = soundPool.load(this, R.raw.chimp, PRIORITY); sid_rooster = soundPool.load(this, R.raw.rooster, PRIORITY); sid_roar = soundPool.load(this, R.raw.music_flie, PRIORITY); try { AssetFileDescriptor afd = this.getAssets().openFd("dogbark.mp3"); sid_bark = soundPool.load(afd.getFileDescriptor(), 0, afd.getLength(), PRIORITY); afd.close(); } catch (IOException e) { e.printStackTrace(); } Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN super.onResume(); } public void doClick(View view) { switch(view.getId()) { case R.id.button: if(((ToggleButton)view).isChecked()) { soundPool.autoResume(); } else { soundPool.autoPause(); } break; } } @Override protected void onPause() { soundPool.release(); soundPool = null; super.onPause(); } @Override public void onLoadComplete(SoundPool sPool, int sid, int status) { Log.v("soundPool", "sid " + sid + " loaded with status " + status); final float currentVolume = ((float)aMgr.getStreamVolume(AudioManager.STREAM_MUSIC)) / ((float)aMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); if(status != 0) return; if(sid == sid_background) { if(sPool.play(sid, currentVolume, currentVolume, PRIORITY, -1, 1.0f) == 0) Log.v("soundPool", "Failed to start sound"); } else if(sid == sid_chimp) { queueSound(sid, 5000, currentVolume); } else if(sid == sid_rooster) { queueSound(sid, 6000, currentVolume); } else if(sid == sid_roar) { queueSound(sid, 12000, currentVolume); } else if(sid == sid_bark) { queueSound(sid, 7000, currentVolume); } } private void queueSound(final int sid, final long delay, final float volume) { new Handler().postDelayed(new Runnable() { @Override public void run() { Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN if(soundPool == null) return; if(soundPool.play(sid, volume, volume, PRIORITY, 0, 1.0f) == 0) Log.v("soundPool", "Failed to start sound (" + sid + ")"); queueSound(sid, delay, volume); }}, delay); } } Và khi các bạn debug sẽ nghe được âm thanh của cả 4 file các bạn add vào Project. Các bạn nhấn vào Toggle Button để tắt hay mở SoundPool.Sau đây là 1 số hình ảnh mô tả cho ứng dụng: Lập trình Android – http://laptrinhdidong.vn Page 4 . android:id="@+id/button" android:textOn="Pause" android:textOff="Resume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick". { Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN if (soundPool == null) return; if (soundPool. play(sid, volume, volume, PRIORITY, 0, 1.0f) == 0) Log.v(" ;soundPool& quot;,. xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ToggleButton android:id="@+id/button"

Ngày đăng: 08/08/2014, 15:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan