Android : Piano

Créer un Piano

1.Layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#9C27B0">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Piano"
android:layout_weight="5"
android:textColor="@color/white"
android:textSize="35dp"
android:gravity="center"
android:fontFamily="cursive"
android:background="#9C27B0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9C27B0"
android:orientation="horizontal">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey1"
android:background="#ff00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey2"
android:background="#ee00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey3"
android:background="#dd00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey4"
android:background="#cc00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey5"
android:background="#BB00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey6"
android:background="#aa00FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey7"
android:background="#9900FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey8"
android:background="#8800FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey9"
android:background="#7700FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey10"
android:background="#6600FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey11"
android:background="#5500FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey12"
android:background="#4400FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:id="@+id/pianokey13"
android:background="#3300FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:id="@+id/pianokey14"
android:background="#2200FF"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/pianokey15"
android:background="#1100FF"
/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFC107"
android:layout_weight="5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:text="Play/Pause"/>
</LinearLayout>

</LinearLayout>



2.Java

package com.example.applicationcours;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.media.MicrophoneDirection;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import java.io.IOException;

public class PianoApp extends AppCompatActivity implements View.OnClickListener {
ImageButton[]imgButton=new ImageButton[15];
int[]notes={R.raw.a0,R.raw.a1,
R.raw.b0,R.raw.b1,R.raw.b2,
R.raw.c0,R.raw.c2,
R.raw.d1,R.raw.d2,
R.raw.e0,R.raw.e2,
R.raw.g0,R.raw.g2,
R.raw.f0,R.raw.f2
};
int positionClique=0;
MediaPlayer player=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piano_app);
for(int i=0;i<15;i++)
{
int id = getResources().getIdentifier("pianokey"+(i+1), "id", getPackageName());
imgButton[i]=findViewById(id);
}


//ecoutez les boutons
for(int i=0;i<15;i++)
{
imgButton[i].setOnClickListener(this);
}


//lancer un greeting sound
player=MediaPlayer.create(this,R.raw.win);
player.start();
}

@Override
public void onClick(View view) {
//chercher sur key buton on a cliqué
for(int i=0;i<15;i++) {
if (view.getId() ==imgButton[i].getId())
{
positionClique=i;
player.stop();
player=MediaPlayer.create(this,notes[positionClique]);
player.start();


}
}
}
}