📌  相关文章
📜  如何在Android中构建木琴应用程序?

📅  最后修改于: 2021-05-10 17:23:10             🧑  作者: Mango

在本文中,我们将使用android中的Java和XML在Xylophone上构建一个项目。木琴最初是通过敲击一排木棒演奏的乐器。该应用程序将实现一种能够产生声音的类似木琴的乐器。我们将在此应用程序中创建几个按钮,这些按钮在被敲击时将充当乐器的键。这将是一个单一活动的应用程序。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

第2步:首先进入编码部分,您必须做一些准备工作

添加颜色:我们需要在resources目录的值中添加颜色。我们将为按钮使用这些颜色。

XML


    #0F9D58
    #F21BB56A
    #E620C374
    #D929CF7E
    #CC2ED885
    #BF34E18D
    #B33FF39B


XML


  
    


Java
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // Helpful Constants
    private final int sim_sound = 7;
    private final float lft_vol = 1.0f;
    private final float rgt_vol = 1.0f;
    private final int loop = 0;
    private final int prty = 0;
    private final float NORMAL_PLAY_RATE = 1.0f;
  
    // Add member variables here
    private SoundPool mSoundPool;
    private int mCSoundId1;
    private int mDSoundId2;
    private int mESoundId3;
    private int mFSoundId4;
    private int mGSoundId5;
    private int mASoundId6;
    private int mBSoundId7;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a new SoundPool
        mSoundPool = new SoundPool(sim_sound, AudioManager.STREAM_MUSIC, 0);
  
        // Load and get the IDs to identify the sounds
        mCSoundId1 = mSoundPool.load(getApplicationContext(), R.raw.note1_c, 1);
        mDSoundId2 = mSoundPool.load(getApplicationContext(), R.raw.note2_d, 1);
        mESoundId3 = mSoundPool.load(getApplicationContext(), R.raw.note3_e, 1);
        mFSoundId4 = mSoundPool.load(getApplicationContext(), R.raw.note4_f, 1);
        mGSoundId5 = mSoundPool.load(getApplicationContext(), R.raw.note5_g, 1);
        mASoundId6 = mSoundPool.load(getApplicationContext(), R.raw.note6_a, 1);
        mBSoundId7 = mSoundPool.load(getApplicationContext(), R.raw.note7_b, 1);
  
    }
  
    // Add the play methods triggered by the buttons
    public void c (View v){
        mSoundPool.play(mCSoundId1, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void d (View v){
        mSoundPool.play(mDSoundId2, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    // Add the play methods triggered by the buttons
    public void e (View v){
        mSoundPool.play(mESoundId3, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void f (View v){
        mSoundPool.play(mFSoundId4, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void g (View v){
        mSoundPool.play(mGSoundId5, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void a (View v){
        mSoundPool.play(mASoundId6, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void b (View v){
        mSoundPool.play(mBSoundId7, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
}


添加主要注释:将这些音频注释保存在应用程序> res>原始目录中

  • 注1
  • 笔记2
  • 注3
  • 注4
  • 注5
  • 注6
  • 注7

步骤3:使用activity_main.xml文件

XML代码用于构建活动的结构及其样式部分。在此XML文件中,我们将创建一个垂直方向的LinearLayout。这将包含七个按钮,每个按钮具有不同的onClick功能。当我们向下移动时,按钮的宽度会减小,以使它看起来像木琴,并且也象征着按钮越小,音符声音就越小。以下是activity_main.xml文件的代码。

XML格式



  
    

步骤4:使用MainActivity。 Java文件

在MainActivity中,我们需要创建一个新的SoundPool 。 Soundpool使用MediaPlayer库服务来加载音频文件。我们将创建一个函数有不同的音频记下每个按键。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // Helpful Constants
    private final int sim_sound = 7;
    private final float lft_vol = 1.0f;
    private final float rgt_vol = 1.0f;
    private final int loop = 0;
    private final int prty = 0;
    private final float NORMAL_PLAY_RATE = 1.0f;
  
    // Add member variables here
    private SoundPool mSoundPool;
    private int mCSoundId1;
    private int mDSoundId2;
    private int mESoundId3;
    private int mFSoundId4;
    private int mGSoundId5;
    private int mASoundId6;
    private int mBSoundId7;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a new SoundPool
        mSoundPool = new SoundPool(sim_sound, AudioManager.STREAM_MUSIC, 0);
  
        // Load and get the IDs to identify the sounds
        mCSoundId1 = mSoundPool.load(getApplicationContext(), R.raw.note1_c, 1);
        mDSoundId2 = mSoundPool.load(getApplicationContext(), R.raw.note2_d, 1);
        mESoundId3 = mSoundPool.load(getApplicationContext(), R.raw.note3_e, 1);
        mFSoundId4 = mSoundPool.load(getApplicationContext(), R.raw.note4_f, 1);
        mGSoundId5 = mSoundPool.load(getApplicationContext(), R.raw.note5_g, 1);
        mASoundId6 = mSoundPool.load(getApplicationContext(), R.raw.note6_a, 1);
        mBSoundId7 = mSoundPool.load(getApplicationContext(), R.raw.note7_b, 1);
  
    }
  
    // Add the play methods triggered by the buttons
    public void c (View v){
        mSoundPool.play(mCSoundId1, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void d (View v){
        mSoundPool.play(mDSoundId2, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    // Add the play methods triggered by the buttons
    public void e (View v){
        mSoundPool.play(mESoundId3, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void f (View v){
        mSoundPool.play(mFSoundId4, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void g (View v){
        mSoundPool.play(mGSoundId5, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void a (View v){
        mSoundPool.play(mASoundId6, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
    public void b (View v){
        mSoundPool.play(mBSoundId7, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    }
  
}

输出: