Play Sound Android Example
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager
{
private SoundPool soundPool;
private int[] sm;
Context context;
public SoundManager(Context context) {
this.context = context;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
sm = new int[2];
// fill your sounds
sm[0] = soundPool.load(context, R.raw.my_sound1, 1);
sm[1] = soundPool.load(context, R.raw.my_sound2, 1);
}
public final void playSound(int sound) {
AudioManager mgr = (AudioManager)context.getSystemService(
Context.AUDIO_SERVICE);
float streamVolumeCurrent =
mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(sm[sound], volume, volume, 1, 0, 1f);
}
public final void cleanUpIfEnd() {
sm = null;
context = null;
soundPool.release();
soundPool = null;
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Landscape - portrait orientation change:
boolean mbOrientationLandscape = true;
if(mbOrientationLandscape ){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mbOrientationLandscape =false;
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mbOrientationLandscape =true;
}
Samsung S6102 Galaxy Y Duos umožňuje pracovat se dvěma aktivními SIM kartami a má GPS navigaci. Cena cca 4.500 korun / léto 2012.
Displej
Rozlišení displeje 320 x 240
Velikost displeje 3.14 "
Počet barev 262000
Rozměry 104 mm x 60 mm x 11.5 mm
Fotoaparát 3 Mpix, natačení videosekvencí
Operační system Android 2.3 (Gingerbread)
Procesor 832 MHz
Funkce Dual sim, Změny velikosti písma, Java, Internetový prohlížeč Přehrávání MP3, jack 3,5, Psaní SMS a e-mailů Multimediální zprávy MMS, Dlouhé SMS, Emailový prohlížeč, Kontakty a volání Hlasité handsfree, MP3/WMA/AAC vyzvánění, Skupiny volajících, Baterie
Typ baterie Li-Ion 1300 mAh
Doba hovoru 370 min
Pohotovostní doba 440 hodin
Uživatelská paměť 160 MB
Datové funkce GPS modul, WiFi, Bluetooth, GPRS, EDGE, Hardwarový modem
Aplikace Diktafon, Kalendář, FM rádio, Záznamník hovoru, Hry v telefonu, Kalkulačka, Budík
Mobilní telefony Samsung mají velmi dobrý poměr KVALITA / CENA

Displej
Rozlišení displeje 320 x 240
Velikost displeje 3.14 "
Počet barev 262000
Rozměry 104 mm x 60 mm x 11.5 mm
Fotoaparát 3 Mpix, natačení videosekvencí
Operační system Android 2.3 (Gingerbread)
Procesor 832 MHz
Funkce Dual sim, Změny velikosti písma, Java, Internetový prohlížeč Přehrávání MP3, jack 3,5, Psaní SMS a e-mailů Multimediální zprávy MMS, Dlouhé SMS, Emailový prohlížeč, Kontakty a volání Hlasité handsfree, MP3/WMA/AAC vyzvánění, Skupiny volajících, Baterie
Typ baterie Li-Ion 1300 mAh
Doba hovoru 370 min
Pohotovostní doba 440 hodin
Uživatelská paměť 160 MB
Datové funkce GPS modul, WiFi, Bluetooth, GPRS, EDGE, Hardwarový modem
Aplikace Diktafon, Kalendář, FM rádio, Záznamník hovoru, Hry v telefonu, Kalkulačka, Budík
Mobilní telefony Samsung mají velmi dobrý poměr KVALITA / CENA

public static Bitmap createBitmap (int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
// //www.apache.org/licenses/LICENSE-2.0
public class MainActivity extends Activity {
private static final int WIDTH = 50;
private static final int HEIGHT = 50;
private static final int STRIDE = 64; // must be >= WIDTH
private static int[] createColors() {
int[] colors = new int[STRIDE * HEIGHT];
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int r = x * 255 / (WIDTH - 1);
int g = y * 255 / (HEIGHT - 1);
int b = 255 - Math.min(r, g);
int a = Math.max(r, g);
colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
return colors;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.GREEN);
int[] mColors = createColors();
int[] colors = mColors;
Bitmap bitmap = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
Bitmap.Config.RGB_565);
canvas.drawBitmap(bitmap, 50,20, paint);
}
}
}
Set, get string , array of array of strings, Java example code
public final static String[][]_arArOfString_1 = {
new String[] {"bla","bla","hello world!"},
new String[] {},
new String[] {},
new String[] {},
new String[] {},
new String[] {}
};
String sOut = _arArOfString_1[0][2]; // hello world!
Bitmap size calculation:
bmpHeight * bmpWidth
For example:
Resolution of image 1024x860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640x4= 3 522 560 (3.5MB)
Get bitmap size without allocation of memory:
Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
bmpHeight * bmpWidth
For example:
Resolution of image 1024x860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640x4= 3 522 560 (3.5MB)
Get bitmap size without allocation of memory:
BitmapFactory.Options options = new BitmapFactory.Options();
// If set to true, the decoder will return null (no bitmap), but the out... fields will still
// be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options);
int imageHeight = options.outHeight; // 1024
int imageWidth = options.outWidth; // 860
String imageType = options.outMimeType; // .jpg .png .gif
Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
float density = getResources().getDisplayMetrics().density;
Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int freeMemory = (int) (Runtime.getRuntime().freeMemory() / 1024);
String memMessage = String.format(
"Free=%d kB,
MaxMem=%d kB,
Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB",
freeMemory,
maxMemory,
memoryInfo.getTotalPss() / 1024.0,
memoryInfo.getTotalPrivateDirty() / 1024.0,
memoryInfo.getTotalSharedDirty() / 1024.0);
((TextView)findViewById(R.id.textViewInfo)).setText(memMessage );
Editace: 2013-01-29 20:09:26
Počet článků v kategorii: 396
Url:play-sound-android-example