Android中的声音样本播放器

时间:2015-08-30 15:43:47

标签: java android compatibility soundpool

我有一个程序可以播放鼓包中的样本。当我在一部手机上尝试但另一部手机(点击正在注册并执行振动)时,它的功能非常完美。声音没有播放。如果代码是正确的,为什么它在其他设备中无法正常工作?!这是代码......

import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import com.example.android.searchabledict.R;

public class GameMain extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_main);

    // Initialise SoundPool to play the drum sample sounds
    soundPoolDrums = new SoundPool (8, AudioManager.STREAM_MUSIC, 0);

    // Create HashMap of sounds
    soundMap = new HashMap<Integer, Integer>();
  }

  // Constants and variables for storing sounds for drumkit
  public static final int HIHAT_SOUND_ID=0;
  public static final int RIDE_SOUND_ID=1;
  public static final int CYMBAL_SOUND_ID=2;
  public static final int BASS_SOUND_ID=3;
  public static final int SNARE_SOUND_ID=4;
  public static final int TOMTOMA_SOUND_ID=5;
  public static final int TOMTOMB_SOUND_ID=6;
  public static final int TOMTOMC_SOUND_ID=7;
  private SoundPool soundPoolDrums;
  private Map<Integer, Integer> soundMap;

  /*
   * The next section manages the playback of drum samples
   */

  // High-hat sample player
  public void HatOne (View view) {

      soundMap.put(HIHAT_SOUND_ID, soundPoolDrums.load(this, R.raw.drumhata, 1)); 
      soundPoolDrums.play(soundMap.get(HIHAT_SOUND_ID), 1, 1, 1, 0, 1f);
  }

  // Ride sample player
  public void HatTwo (View view) {

      soundMap.put(RIDE_SOUND_ID, soundPoolDrums.load(this, R.raw.drumhatc, 1));
      soundPoolDrums.play(soundMap.get(RIDE_SOUND_ID), 1, 1, 1, 0, 1f);
      }

  // Crash sample player
  public void Crash (View view) {

      soundMap.put(CYMBAL_SOUND_ID, soundPoolDrums.load(this, R.raw.drumcrash, 1));   
      soundPoolDrums.play(soundMap.get(CYMBAL_SOUND_ID), 1, 1, 1, 0, 1f);
      }

  // Bass sample player
  public void KickDrum (View view) {

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vib.vibrate(200);

      soundMap.put(BASS_SOUND_ID, soundPoolDrums.load(this, R.raw.drumkick, 1)); 
      soundPoolDrums.play(soundMap.get(BASS_SOUND_ID), 1, 1, 1, 0, 1f);
  }

  // Snare sample player
  public void SnareDrum (View view) {

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vib.vibrate(150);

      soundMap.put(SNARE_SOUND_ID, soundPoolDrums.load(this, R.raw.drumsnare, 1)); 
      soundPoolDrums.play(soundMap.get(SNARE_SOUND_ID), 1, 1, 1, 0, 1f);
  }

  // High tom sample player
  public void TomOne (View view) {

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vib.vibrate(120);

      soundMap.put(TOMTOMA_SOUND_ID, soundPoolDrums.load(this, R.raw.drumtom1, 1)); 
      soundPoolDrums.play(soundMap.get(TOMTOMA_SOUND_ID), 1, 1, 1, 0, 1f);
  }

  // Mid tom sample player
  public void TomTwo (View view) {

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vib.vibrate(120);

      soundMap.put(TOMTOMB_SOUND_ID, soundPoolDrums.load(this, R.raw.drumtom2, 1)); 
      soundPoolDrums.play(soundMap.get(TOMTOMB_SOUND_ID), 1, 1, 1, 0, 1f);
  }

  // Low tom sample player
  public void TomThree (View view) {

      Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      vib.vibrate(120);

      soundMap.put(TOMTOMC_SOUND_ID, soundPoolDrums.load(this, R.raw.drumtom3, 1)); 
      soundPoolDrums.play(soundMap.get(TOMTOMC_SOUND_ID), 1, 1, 1, 0, 1f);
  }
  // End of sample players section

  // Handlers for Back buttons
  // If tactile back button is pressed then the user is returned to the home screen
  @Override
  public void onBackPressed(){

    soundPoolDrums.release();
    soundPoolDrums = null;

    // play back sound
    MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
    mMediaPlayer.start();
    // short vibration
    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vib.vibrate(200);

    Intent s = new Intent (this, Main.class);
    startActivity(s);
  }

  // Handler for back button 
  public void BackButton (View view) {

    soundPoolDrums.release();
    soundPoolDrums = null;  

    MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
    mMediaPlayer.start();

    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vib.vibrate(200);

    Intent s = new Intent (this, Main.class);
    startActivity(s);   
  }

}

和XML

 <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fabricblacksmr">

        <ImageButton
        android:id="@+id/BackButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:onClick="BackButton"
        android:src="@drawable/buttonhome"
        android:layout_margin="8dp"
        android:background="#00000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:text="@string/gamemaintext" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

            <ImageButton
            android:id="@+id/imageButtongame1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="HatOne"
            android:src="@drawable/dhihats" />

            <ImageButton
            android:id="@+id/imageButtongame2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:src="@drawable/dride"
            android:onClick="HatTwo"
            android:background="#00000000"/>

            <ImageButton
            android:id="@+id/imageButtongame3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:src="@drawable/dcrash"
            android:onClick="Crash"
            android:background="#00000000"/>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

            <ImageButton
            android:id="@+id/imageButtongame4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="KickDrum"
            android:src="@drawable/dbassdrum"
            android:background="#00000000" />

            <ImageButton
            android:id="@+id/imageButtongame5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"  
            android:src="@drawable/dsnare"
            android:onClick="SnareDrum"
            android:background="#00000000"/>

    </TableRow>

     <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

            <ImageButton
            android:id="@+id/imageButtongame6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:src="@drawable/dtom1"
            android:onClick="TomOne"
            android:background="#00000000"/>

            <ImageButton
            android:id="@+id/imageButtongame7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:src="@drawable/dtom2"
            android:onClick="TomTwo"
            android:background="#00000000"/>

            <ImageButton
            android:id="@+id/imageButtongame8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:src="@drawable/dtom3"
            android:onClick="TomThree"
            android:background="#00000000"/>

    </TableRow>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="@string/filsdrum"/>

     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft ="120dp" 
        android:text="@string/copyright"/>

</TableLayout>

0 个答案:

没有答案