[Android] Soundpool으로 만드는 오카리나 앱

안드로이드에서는 SoundPool이라는 사운드 함수를 지원합니다. SoundPool이라는 함수는 간단하게 소리를 입혀주는 함수인데, 이것을 이용해 간단한 악기 앱을 만들 수 있습니다.

 

 SoundPool로 만드는 오카리나 앱 

mainpage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:orientation="horizontal"
        android:weightSum="5"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/name"
        android:id="@+id/img"
        />

    </LinearLayout>

    <Button
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:backgroundTint="#FFD0E3"
        android:text="오카리나 연주하기"
        android:textAlignment="center"
        android:onClick="start"
        android:id="@+id/start"
        />

    <Button
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:backgroundTint="#FFD0E3"
        android:text="어플 종료하기"
        android:textAlignment="center"
        android:onClick="end"
        android:id="@+id/end"
        />

</LinearLayout>

 

StartActivity.java

package com.example.samsung.ocarina;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by SAMSUNG on 2017-08-29.
 */

public class StartActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);

        Button b = (Button)findViewById(R.id.start);
        Button b2 = (Button)findViewById(R.id.end);

        b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
            }
        });
        b2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                new AlertDialog.Builder(StartActivity.this).setTitle("Application 종료")
                .setMessage("애플리케이션을 종료하시겠습니까?")
                .setPositiveButton("OK",new DialogInterface.OnClickListener(){
                 @Override
                 public void onClick(DialogInterface dialog, int which){
                 Toast.makeText(StartActivity.this,"애플리케이션 종료되었습니다",
                 Toast.LENGTH_SHORT).show();

                 finish();
                 }
              })
              .setNegativeButton("NO",null)
              .setIcon(android.R.drawable.ic_dialog_alert)
              .show();
            }
        });
    }
} 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.samsung.ocarina.MainActivity">

    <ImageView
        android:layout_width="500dp"
        android:layout_height="300dp"
        android:background="@drawable/ocarina"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/oca" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="35dp"
        android:id="@+id/Do"
        android:background="#00ff0000"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/oca"
        android:layout_alignStart="@+id/oca"
        android:onClick="Do"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:id="@+id/Re"
        android:background="#00ff0000"
        android:layout_alignBaseline="@+id/Do"
        android:layout_alignBottom="@+id/Do"
        android:layout_toRightOf="@+id/Do"
        android:layout_toEndOf="@+id/Do"
        android:onClick="Re"
        />

    <Button
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/Mi"
        android:background="#00ff0000"
        android:layout_alignBottom="@+id/Do"
        android:layout_toRightOf="@+id/Re"
        android:layout_toEndOf="@+id/Re"
        android:onClick="Mi"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/Fa"
        android:background="#00ff0000"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/Mi"
        android:onClick="Fa"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00ff0000"
        android:layout_below="@+id/Do"
        android:layout_centerHorizontal="true"
        android:id="@+id/Sol"
        android:onClick="Sol"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00ff0000"
        android:layout_below="@+id/Fa"
        android:layout_toRightOf="@+id/Sol"
        android:layout_toEndOf="@+id/Sol"
        android:id="@+id/Ra"
        android:onClick="Ra"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00ff0000"
        android:layout_alignBaseline="@+id/Ra"
        android:layout_alignBottom="@+id/Ra"
        android:layout_toRightOf="@+id/Ra"
        android:layout_toEndOf="@+id/Ra"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:id="@+id/Si"
        android:onClick="Si"/>

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00ff0000"
        android:layout_alignBaseline="@+id/Re"
        android:layout_alignBottom="@+id/Re"
        android:layout_toRightOf="@+id/Si"
        android:layout_toEndOf="@+id/Si"
        android:id="@+id/HighDo"
        android:onClick="HighDo"/>

</RelativeLayout> 

 

MainActivity.java

package com.example.samsung.ocarina;

import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.SoundPool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        Button b = (Button)findViewById(R.id.Do);
        Button b2 = (Button)findViewById(R.id.Re);
        Button b3 = (Button)findViewById(R.id.Mi);
        Button b4 = (Button)findViewById(R.id.Fa);
        Button b5 = (Button)findViewById(R.id.Sol);
        Button b6 = (Button)findViewById(R.id.Ra);
        Button b7 = (Button)findViewById(R.id.Si);
        Button b8 = (Button)findViewById(R.id.HighDo);


        final SoundPool sp = new SoundPool(8, AudioManager.STREAM_MUSIC,0);
        // 최대 음악파일의 개수, 스트림 타입, 음질(기본값 0)
        final int soundID = sp.load(this,R.raw.lowdoo,1);
        final int soundID2 = sp.load(this,R.raw.re,1);
        final int soundID3 = sp.load(this,R.raw.mi,1);
        final int soundID4 = sp.load(this,R.raw.pa,1);
        final int soundID5 = sp.load(this,R.raw.sol,1);
        final int soundID6 = sp.load(this,R.raw.ra,1);
        final int soundID7 = sp.load(this,R.raw.si,1);
        final int soundID8 = sp.load(this,R.raw.highdoo,1);


        b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID,1,1,0,0,0);
            }
        });
        b2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID2,1,1,0,0,0);
            }
        });
        b3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID3,1,1,0,0,0);
            }
        });
        b4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID4,1,1,0,0,0);
            }
        });
        b5.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID5,1,1,0,0,0);
            }
        });
        b6.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID6,1,1,0,0,0);
            }
        });
        b7.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID7,1,1,0,0,0);
            }
        });
        b8.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                sp.play(soundID8,1,1,0,0,0);
            }
        });
    }
}

오카리나 앱

 

오카리나 앱
오카리나 앱

 

댓글(0)

Designed by JB FACTORY