 |
Android - อยากทำให้เพลงที่เล่นจาก SoundPool เล่นได้มากกว่า 5 วินาทีครับ |
|
 |
|
|
 |
 |
|
ผมกำลังศึกษาโค้ด soundpool จากเว็บนี้ แล้วมาดัดแปลงเป็นแบบของผมเองครับ
(จากลิ้งนี้ครับ http://content.gpwiki.org/index.php/Android:Playing_Sound_Effects_With_SoundPool)
ตามรูปนี้ครับ

พอผมกดปุ่ม Test แล้ว มันเล่นเพลงเพียง 5 วินาที แทนที่จะเล่นทั้งเพลงครับ
(เพลงที่ผมใช้คือ ลมเปลี่ยนทิศ ของ Big Ass เป็นไฟล์ midi ครับ)
สิ่งที่ผมต้องการคือ เมื่อกดปุ่ม Test แล้ว อยากให้เล่นเพลงทั้งเพลงครับ (ผมไม่ได้สนใจในปุ่มอื่นๆ นะครับ)
พี่ๆ ท่านใดพอจะทราบไหมครับว่าผมจะต้องทำอย่างไรบ้างครับ ขอบคุณครับ
นี่คือโค้ดครับ
Code (Android-Java) - SoundManager.java
package gpwiki.demo;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager
{
private Context pContext;
private SoundPool sndPool;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return sndPool.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
sndPool.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void setSpeed(float speed)
{
rate = speed;
// Speed of zero is invalid
if(rate < 0.01f)
rate = 0.01f;
// Speed has a maximum of 2.0
if(rate > 2.0f)
rate = 2.0f;
}
public void setBalance(float balVal)
{
balance = balVal;
// Recalculate volume levels
setVolume(masterVolume);
}
// Free ALL the things!
public void unloadAll()
{
sndPool.release();
}
}
Code (Android-Java) - DemoMain.java
package gpwiki.demo;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class DemoMain extends Activity
{
SoundManager snd;
int laser, explode, pickup, meow, bark, moo, test;
OnSeekBarChangeListener barChange;
OnClickListener buttonClick;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of our sound manger
snd = new SoundManager(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the samples from res/raw
test = snd.load(R.raw.test);
laser = snd.load(R.raw.laser);
explode = snd.load(R.raw.explosion);
pickup = snd.load(R.raw.pickup);
meow = snd.load(R.raw.cat);
bark = snd.load(R.raw.barkloud);
moo = snd.load(R.raw.cow);
// Create a seek bar handler
barChange = new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
switch (seekBar.getId())
{
case R.id.VolBar1:
snd.setVolume((float)progress/100.0f);
break;
case R.id.BalBar:
snd.setBalance((float)progress/100.0f);
break;
case R.id.SpeedBar:
snd.setSpeed((float)progress/100.0f);
break;
}
}
};
// Set our handler as the ChangeListener for the seekbar controls
SeekBar sb;
sb = (SeekBar)findViewById(R.id.SpeedBar);
sb.setOnSeekBarChangeListener(barChange);
sb = (SeekBar)findViewById(R.id.BalBar);
sb.setOnSeekBarChangeListener(barChange);
sb = (SeekBar)findViewById(R.id.VolBar1);
sb.setOnSeekBarChangeListener(barChange);
}
// Button listener assigned in XML layout
public void clickHandler(View v)
{
int id = v.getId(); // Use the button id to determine which sample should be played
switch (id)
{
case R.id.button1:
snd.play(test);
break;
case R.id.button2:
snd.play(explode);
break;
case R.id.button3:
snd.play(pickup);
break;
case R.id.button4:
snd.play(meow);
break;
case R.id.button5:
snd.play(bark);
break;
case R.id.button6:
snd.play(moo);
break;
}
}
}
Tag : Mobile, Android, JAVA, Windows
|
|
 |
 |
 |
 |
Date :
2013-10-08 21:48:41 |
By :
M2Midnight |
View :
1559 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วไม่ทราบว่า MediaPlayer สามารถ เพิ่ม-ลด ความเร็วเพลงได้ไหมครับ
|
 |
 |
 |
 |
Date :
2013-10-09 19:47:18 |
By :
M2Midnight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ มีอีกคำถามหนึ่งครับ ผมสามารถใช้ AudioTrack มาประยุกต์กับโค้ดของเขาได้ไหมครับ
|
 |
 |
 |
 |
Date :
2013-10-10 20:46:10 |
By :
M2Midnight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|