import java.io.*;
import android.app.*;
import android.content.res.*;
import android.media.*;
import android.os.*;
import android.view.*;
import android.view.View.OnTouchListener;
import android.widget.*;
public class SoundPoolTest extends Activity implements OnTouchListener
{
SoundPool soundPool;
int explosionId = -1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setOnTouchListener(this);
setContentView(textView);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
try
{
AssetManager assetManager = getAssets();
AssetFileDescriptor discriptor = assetManager.openFd("explosion.ogg");
explosionId = soundPool.load(discriptor, 1);
}
catch(IOException e)
{
textView.setText("Couldn't load sound effect from asset, " + e.getMessage());
}
}
@Override
public boolean onTouch(View view, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(explosionId != -1)
{
soundPool.play(explosionId, 1, 1, 0, 0, 1);
}
}
return true;
}
}
import android.app.*;
import android.content.res.*;
import android.media.*;
import android.os.*;
import android.view.*;
import android.view.View.OnTouchListener;
import android.widget.*;
public class SoundPoolTest extends Activity implements OnTouchListener
{
SoundPool soundPool;
int explosionId = -1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setOnTouchListener(this);
setContentView(textView);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
try
{
AssetManager assetManager = getAssets();
AssetFileDescriptor discriptor = assetManager.openFd("explosion.ogg");
explosionId = soundPool.load(discriptor, 1);
}
catch(IOException e)
{
textView.setText("Couldn't load sound effect from asset, " + e.getMessage());
}
}
@Override
public boolean onTouch(View view, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(explosionId != -1)
{
soundPool.play(explosionId, 1, 1, 0, 0, 1);
}
}
return true;
}
}
'코드조각모음' 카테고리의 다른 글
[android010] 전체화면 예제 (0) | 2011.11.06 |
---|---|
[android009] 배경음을 위한 MediaPlayer 예제 (1) | 2011.11.06 |
[android007] SD카드 예제 (0) | 2011.11.06 |
[android006] 어셋(Asset) 예제 (0) | 2011.11.06 |
[andorid005] 가속도계 예제 (0) | 2011.11.06 |