ホームに戻る
 サウンド2

// res\raw というディレクトリを作成し test.wav を入れておく

// Sound2.java

package org.example.test;

import java.util.*;
import java.io.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.media.*;

public class Sound2 extends Activity
{
   int sp_id = 0;
   SoundPool sp = null;
   Button bt;

   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);

      LinearLayout ll = new LinearLayout(this); 
      ll.setOrientation(LinearLayout.VERTICAL);
      setContentView(ll);

      bt = new Button(this);
      bt.setText("play");
      ll.addView(bt);

      bt.setOnClickListener(new SampleClickListener());
   }

   public void onResume()
   {
     super.onResume();
     sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
     sp_id = sp.load(this, R.raw.test, 1);
   }

   public void onPause()
   {
     super.onPause();
     sp.stop(sp_id);
     sp.unload(sp_id);
     sp.release();
   }

   class SampleClickListener implements OnClickListener
   {
      public void onClick(View v)
      {
        // id, 左Vol, 右Vol, 優先度, -1でloop, 再生速度
        sp.play(sp_id, 1.0f, 1.0f, 0, 0, 1.0f);
      }
   }
}

inserted by FC2 system