Twitter Facebook Delicious Digg Stumbleupon Favorites More

Tuesday 9 January 2018

xylopone android app


MAIN ACTIVITY

package com.example.viraj.xylopone_music;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.lang.ref.PhantomReference;

public class MainActivity extends AppCompatActivity {
//HELPFUL CONSTANT
    private final int NR_OF_SIMULATANEOUS_SOUND=7;
    private final float LEFT_VOLUME=1.0f;
    private final float RIGHT_VOLUME=1.0f;
    private final int NO_LOOP=0;
    private final int PRIORITY=0;
    private final float NORMAL_PLAY_RATE=1.0f;

    //TODO add MEMBER VARIABLE HERE
    private SoundPool mSoundPool;
    private int nASoundID;
    private int nBSoundID;
    private int nCSoundID;
    private int nDSoundID;
    private int nESoundID;
    private int nFSoundID;
    private int nGSoundID;
    private int nHSoundID;



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

        //todo create a sound
        mSoundPool=new SoundPool(7, AudioManager.STREAM_MUSIC,0);

        //todo load and get the id to identify the sound
        nASoundID=mSoundPool.load(getApplicationContext(),R.raw.note6_a,1);
        nBSoundID=mSoundPool.load(getApplicationContext(),R.raw.note7_b,1);
        nCSoundID=mSoundPool.load(getApplicationContext(),R.raw.note1_c,1);
        nDSoundID=mSoundPool.load(getApplicationContext(),R.raw.note2_d,1);
        nESoundID=mSoundPool.load(getApplicationContext(),R.raw.note3_e,1);
        nFSoundID=mSoundPool.load(getApplicationContext(),R.raw.note4_f,1);
        nGSoundID=mSoundPool.load(getApplicationContext(),R.raw.note5_g,1);
        nHSoundID=mSoundPool.load(getApplicationContext(),R.raw.lst,1);
        //todo add the play method triggered by button

    }
    //todo Add the play method triggered by the button
    public void playC(View v)
    {
       // mSoundPool.play(nASoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);   same hi he demo he niche valal
mSoundPool.play(nCSoundID,1.0f,1.0f,0,0,1.0f);
    }
    public void playD(View v)
    {
        mSoundPool.play(nDSoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playE(View v)
    {
        mSoundPool.play(nESoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playF(View v)
    {
        mSoundPool.play(nFSoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playG(View v)
    {
        mSoundPool.play(nGSoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playA(View v)
    {
        mSoundPool.play(nASoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playB(View v)
    {
        mSoundPool.play(nBSoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
    public void playH(View v)
    {
        mSoundPool.play(nHSoundID,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    }
}

XML FILE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.viraj.xylopone_music.MainActivity">

    <Button
        style="@style/KeyStyle"
        android:id="@+id/c_key"
        android:background="@color/red"
        android:onClick="playC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button style="@style/KeyStyle"
        android:background="@color/orange"
        android:id="@+id/d_key"
        android:onClick="playD"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button style="@style/KeyStyle"
        android:id="@+id/e_key"
        android:background="@color/yellow"
        android:onClick="playE"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button style="@style/KeyStyle"
        android:id="@+id/f_key"
        android:background="@color/green"
        android:onClick="playF"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button style="@style/KeyStyle"
        android:id="@+id/g_key"
        android:background="@color/turquoise"
        android:onClick="playG"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button style="@style/KeyStyle"
        android:id="@+id/a_key"
        android:background="@color/blue"
        android:onClick="playA"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button style="@style/KeyStyle"
        android:id="@+id/b_key"
        android:background="@color/purple"
        android:onClick="playB"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button style="@style/KeyStyle"
        android:background="@color/red"
        android:id="@+id/h_key"
        android:onClick="playH"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

COLOUR
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>

    <color name="colorAccent">#FF4081</color>

    <color name="colorPrimaryDark">#000000</color>
    <color name="red">#e74c3c</color>
    <color name="orange">#e67e22</color>
    <color name="yellow">#f1c40f</color>
    <color name="turquoise">#1abc9c</color>
    <color name="green">#2ecc71</color>
    <color name="blue">#3498db</color>
    <color name="purple">#9b59b6</color>

</resources>

STYLE
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="KeyStyle">
        <item name="android:layout_weight">1</item>
        <item name="android:layout_marginTop">3dp</item>
        <item name="android:layout_marginBottom">3dp</item>
    </style>

</resources>




Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab