Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中的声音示例播放器_Java_Android_Compatibility_Soundpool - Fatal编程技术网

Java Android中的声音示例播放器

Java Android中的声音示例播放器,java,android,compatibility,soundpool,Java,Android,Compatibility,Soundpool,我有一个程序可以播放drumkit的样本。当我在一部手机上试用时,它的功能非常完美,但在另一部手机上(当咔哒声记录并执行振动时),声音没有播放。如果代码正确,为什么它不能在其他设备中正常工作?!这是代码 import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.content.Context; import android.content.Intent; import a

我有一个程序可以播放drumkit的样本。当我在一部手机上试用时,它的功能非常完美,但在另一部手机上(当咔哒声记录并执行振动时),声音没有播放。如果代码正确,为什么它不能在其他设备中正常工作?!这是代码

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);   
  }

}
import java.util.HashMap;
导入java.util.Map;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.media.MediaPlayer;
导入android.media.SoundPool;
导入android.os.Bundle;
导入android.os.可控震源;
导入android.view.view;
导入com.example.android.searchabledict.R;
公共类GameMain扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game_main);
//初始化SoundPool以播放鼓样本声音
soundPoolDrums=新的SoundPool(8,AudioManager.STREAM_MUSIC,0);
//创建声音的HashMap
soundMap=newhashmap();
}
//用于存储drumkit声音的常量和变量
公共静态最终int HIHAT_SOUND_ID=0;
公共静态最终整数乘骑声音ID=1;
公共静态最终int-CYMBAL\u-SOUND\u-ID=2;
公共静态最终低音\声音\ ID=3;
公共静态最终整数陷阱声音ID=4;
公共静态最终int TOMTOMA_SOUND_ID=5;
公共静态最终int\u SOUND\u ID=6;
公共静态最终int TOMC_SOUND_ID=7;
私人声池声池鼓;
私人地图声像图;
/*
*下一节管理鼓样本的回放
*/
//高帽子样品播放器
公共无效HatOne(视图){
soundMap.put(HIHAT_SOUND_ID,soundPoolDrums.load)(这个,R.raw.drumhata,1));
soundPoolDrums.play(soundMap.get(HIHAT_SOUND_ID),1,1,1,0,1f);
}
//骑乘示例播放器
公共空间HAT2(视图){
soundMap.put(骑乘声ID,soundPoolDrums.load(这个,R.raw.Drumhatt,1));
soundPoolDrums.play(soundMap.get(骑乘声音ID)),1,1,1,0,1f;
}
//碰撞样本播放器
公共无效崩溃(视图){
soundMap.put(CYMBAL_SOUND_ID,soundPoolDrums.load)(这个,R.raw.drumcrash,1));
soundPoolDrums.play(soundMap.get(CYMBAL_SOUND_ID)),1,1,1,0,1f;
}
//贝司样本播放器
公共空踢鼓(视图){
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(200);
soundMap.put(BASS_SOUND_ID,soundPoolDrums.load)(这个,R.raw.drumkick,1));
soundPoolDrums.play(soundMap.get(BASS_SOUND_ID)),1,1,1,0,1f;
}
//陷阱示例播放器
公共无效陷阱(视图){
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(150);
soundMap.put(圈套声音ID,soundPoolDrums.load(这个,R.raw.drumsnare,1));
soundPoolDrums.play(soundMap.get(圈套声音ID),1,1,1,0,1f);
}
//高汤姆样品播放器
公共无效TomOne(视图){
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(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);
}
//中汤姆样品播放器
公共void TomTwo(视图){
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(120);
soundMap.put(tomtomourm\u SOUND\u ID,soundPoolDrums.load(this,R.raw.drumtom2,1));
soundPoolDrums.play(soundMap.get(Tomtomourm_SOUND_ID),1,1,1,0,1f);
}
//低汤姆样品播放器
公共空间三层(视图){
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(120);
soundMap.put(tomc_SOUND_ID,soundPoolDrums.load(this,R.raw.drumtom3,1));
soundPoolDrums.play(soundMap.get(tomc_SOUND_ID),1,1,1,0,1f);
}
//示例玩家部分结束
//返回按钮的处理程序
//如果按下触觉后退按钮,则用户返回主屏幕
@凌驾
public void onBackPressed(){
soundPoolDrums.release();
soundPoolDrums=null;
//播放声音
MediaPlayer=MediaPlayer.create(这个,R.raw.soundbackbutton);
mmediplayer.start();
//短振动
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(200);
Intent s=新Intent(这个,Main.class);
星触觉;
}
//后退按钮处理程序
公共无效BackButton(视图){
soundPoolDrums.release();
soundPoolDrums=null;
MediaPlayer=MediaPlayer.create(这个,R.raw.soundbackbutton);
mmediplayer.start();
可控震源vib=(可控震源)getSystemService(Context.可控震源_服务);
振动(200);
Intent s=新Intent(这个,Main.class);
星触觉;
}
}
还有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>


一部手机和另一部手机有什么区别?应该没有区别!有什么想法吗?我的代码正常还是有什么问题?!你的声音是如何编码的?mp3?wav?ogg?嗨,克里斯。它们是。wav声音一部手机和另一部手机有什么区别?应该没有区别!有什么想法吗?我的代码正常还是有什么问题?!你的声音是如何编码的?mp3?wav?ogg?嗨,克里斯。它们是.wav声音