We will make a fake snore sound app. Children like this app very much.
Create a new project, add these lines in to declare of class Main in
AndroidManifest.xml.
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
Create folder drawable, copy icon in to it. Create new folder name raw,
put mp3 sound file in to it. Go to http://www.freesound.org/ download sound
file.
This is activity_main.xml file.
<?xml version="1.0"
encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imgPicture2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@drawable/nn" />
<ImageView
android:id="@+id/imgPicturem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@drawable/cb" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:id="@+id/t0"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/bb"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:text="SNORE"
android:textColor="#0000cd"
android:textSize="26sp"
/>
<TextView
android:id="@+id/t"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:background="@drawable/bc"
android:clickable="true"
android:gravity="center"
android:onClick="onClick2"
android:text="PAUSE"
android:textColor="#c90016"
android:textSize="26sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/bb"
android:clickable="true"
android:gravity="center"
android:onClick="onClick3"
android:text="SNORE"
android:textColor="#0000cd"
android:textSize="26sp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:background="@drawable/bc"
android:clickable="true"
android:gravity="center"
android:onClick="onClick4"
android:text="PAUSE"
android:textColor="#c90016"
android:textSize="26sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/bb"
android:clickable="true"
android:gravity="center"
android:onClick="onClick5"
android:text="SNORE"
android:textColor="#0000cd"
android:textSize="26sp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:background="@drawable/bc"
android:clickable="true"
android:gravity="center"
android:onClick="onClick6"
android:text="PAUSE"
android:textColor="#c90016"
android:textSize="26sp"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Run app, screen look like this.
In class Main, declare variables.
int count=0;
int count2=0;
int count3=0;
int po=0;
int po2=0;
int po3=0;
MediaPlayer mp = new MediaPlayer();
MediaPlayer mp2 = new MediaPlayer();
MediaPlayer mp3
= new MediaPlayer();
Import libraries.
If we create variables below setContentView, when press Pause button, it
can’t play again. So, we create them in button Snore.
We use textView instead of button, to control them, copy onClick funtion
in to above last close bracket.
public void onClick(View v)
{
count=1;
mp = MediaPlayer.create(this, R.raw.ab);
mp.setLooping(true);
mp.start();
po=0;
if(mp2 != null||mp3 != null) {
mp2.pause();
mp3.pause();
}
}
In this function, variable count let we
know music is playing, create and set looping music, po=0 mean not in pausing.
To avoid coinside play, we pause all other file.
Next to Pause button.
public void onClick2(View
v) {
po=po+1;
if(count==1){
if(po%2==0){
mp.start();
}
else{
mp.pause();
}
}
}
We plus po=po+1 and get modulo 2 to set play when user click one time,
set pause if click two times and so on.
Another button the same.
public void onClick3(View
v) {
count2=1;
mp2 = MediaPlayer.create(this, R.raw.ac);
mp2.setLooping(true);
mp2.start();
po2=0;
if(mp != null||mp3 != null) {
mp.pause();
mp3.pause();
}
}
public void onClick4(View
v) {
po2=po2+1;
if(count2==1){
if(po2%2==0){
mp2.start();
}
else{
mp2.pause();
}
}
}
public void onClick5(View
v) {
count3=1;
mp3 = MediaPlayer.create(this, R.raw.a);
mp3.setLooping(true);
mp3.start();
po3=0;
if(mp != null||mp2 != null) {
mp.pause();
mp2.pause();
}
}
public void onClick6(View
v) {
po3=po3+1;
if(count3==1){
if(po3%2==0){
mp3.start();
}
else{
mp3.pause();
}
}
}
Copy to above last close bracket.
@Override
public void onDestroy() {
// TODO
Auto-generated method stub
super.onDestroy();
if(mp != null||mp2 != null||mp3 != null) {
mp.stop();
mp2.stop();
mp3.stop();
finish();
}
}
This make music stop when user press back button.
Run app to see result.
No comments:
Post a Comment