Difference between revisions of "Androdi Studio: Playsound"

From OnnoWiki
Jump to navigation Jump to search
(New page: Sumber: http://www.c-sharpcorner.com/UploadFile/1e5156/add-sound-to-your-application-in-android-studio/ Introduction This article explains how to start sound on a button click using the...)
 
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
  
Introduction
 
  
This article explains how to start sound on a button click using the raw folder in Android Studio.
+
Di sini akan di jelasnkan cara agar android akan mengeluarkan suara setelah tombol Start di tekan.
  
In this first you will use an Imageview and two buttons. When you click on the Start button the sound will start and when you click on the Stop button the sound will stop. In this I use a Dog as an example, you can use any other animal as an example.
+
Media player untuk menjalankan suara yang ada di folder raw dapat di aktifkan menggunakan perintah berikut
  
So to apply a sound in your application you need to create a file named raw in the res folder and add the sound file with its extension (Dog.mp3 or any other extension) inside the file. you will create the folder as in the following:
+
MediaPlayer player=MediaPlayer.create(MainActivity.this,R.raw.dog);
  
Clipboard02.jpg
+
==Folder ~/AndroidStudioProjects/Sapi/app/src/main/res/drawable==
 +
isi
  
So after adding the sound file to the raw folder you will create a media player in your class file. You will add a media player and a sound to the media player like this:
+
cat1.png  cat2.png  cow1.png
  
MediaPlayer player=MediaPlayer.create(MainActivity.this,R.raw.dog);
 
  
You will do the start sound operation on a Start button click event and do the stop operation on the Stop button click event like this:
+
==Folder ~/AndroidStudioProjects/Sapi/app/src/main/res/raw==
 +
isi
  
button1.setOnClickListener(new View.OnClickListener() {
+
believeme.wav  catmeow.wav  catpurr.wav  cow1.wav  cow.wav
            @Override
 
  
            public void onClick(View v) {
 
  
                player=MediaPlayer.create(MainActivity.this,R.raw.dog);
 
  
+
==Isi File MainActivity.Java==
 
 
          player.start();
 
  
 +
package id.onnocenter.kucing;
 +
import android.media.MediaPlayer;
 +
import android.os.Bundle;
 +
import android.support.design.widget.FloatingActionButton;
 +
import android.support.design.widget.Snackbar;
 +
import android.support.v7.app.AppCompatActivity;
 +
import android.support.v7.widget.Toolbar;
 +
import android.view.View;
 +
import android.view.Menu;
 +
import android.view.MenuItem;
 +
import android.widget.Button;
 +
import android.widget.ImageView;
 
   
 
   
 
+
public class MainActivity extends AppCompatActivity {
            }
 
 
 
        });
 
 
 
        button2.setOnClickListener(new View.OnClickListener() {
 
 
 
            @Override
 
 
 
            public void onClick(View v) {
 
 
 
                player.pause();
 
 
 
 
   
 
   
 
+
    MediaPlayer player;
            }
 
 
 
 
   
 
   
 
+
    @Override
    });
+
    protected void onCreate(Bundle savedInstanceState) {
 
+
        super.onCreate(savedInstanceState);
Step 1
+
        setContentView(R.layout.activity_main);
 
+
        ImageView imageview=(ImageView)findViewById(R.id.imageview);
Create the project like this:
+
        Button button1=(Button)findViewById(R.id.button1);
 
+
        Button button2=(Button)findViewById(R.id.button2);
Clipboard03.jpg
 
 
 
 
   
 
   
 
+
        button1.setOnClickListener(new View.OnClickListener() {
Step 2
+
            @Override
 
+
            public void onClick(View v) {
Create an XML file and write this:
+
                player=MediaPlayer.create(MainActivity.this,R.raw.cow1);
 
+
                player.start();
In this I have used a Textview and two Buttons. An Imageview inside the Relative layout .When you click on the Start button the sound will start and when you click on the Stop button the sound will stop. In this I use the example of a Dog, you can use any animal as an example.
+
            }
 
+
        });
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
 
 
    xmlns:tools="http://schemas.android.com/tools"
 
 
 
    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=".MainActivity"
 
 
 
        android:background="#566780">
 
 
 
 
   
 
   
 +
        button2.setOnClickListener(new View.OnClickListener() {
 +
            @Override
 +
            public void onClick(View v) {
 +
                player.pause();
 +
            }
 +
        });
 +
    }
 +
}
  
    <TextView
 
 
        android:layout_width="wrap_content"
 
 
        android:layout_height="wrap_content"
 
 
        android:text="SoundComesonImageClick"
 
 
        android:layout_centerHorizontal="true"
 
  
        android:textStyle="bold"
+
==File content_main.xml==
  
            android:textSize="20dp"/>
 
  
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
   
 
   
 
+
    xmlns:tools="http://schemas.android.com/tools"
    <ImageView
+
    android:layout_width="match_parent"
 
+
    android:layout_height="match_parent"
            android:layout_height="wrap_content"
+
    android:paddingLeft="@dimen/activity_horizontal_margin"
 
+
    android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_width="wrap_content"
+
    android:paddingTop="@dimen/activity_vertical_margin"
 
+
    android:paddingBottom="@dimen/activity_vertical_margin"
            android:background="@drawable/images"
+
    tools:context=".MainActivity"
 
+
    android:background="#566780">
            android:layout_centerHorizontal="true"
 
 
 
            android:layout_centerInParent="true"
 
 
 
            android:id="@+id/imageview">
 
 
 
 
   
 
   
 
+
    <TextView
            </ImageView>
+
        android:layout_width="wrap_content"
 
+
        android:layout_height="wrap_content"
 +
        android:text="SoundComesonImageClick"
 +
        android:layout_centerHorizontal="true"
 +
        android:textStyle="bold"
 +
        android:textSize="20dp"/>
 
   
 
   
 
+
    <ImageView
    <Button
+
        android:layout_height="wrap_content"
 
+
        android:layout_width="wrap_content"
            android:id="@+id/button1"
+
        android:background="@drawable/cow1"
 
+
        android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
+
        android:layout_centerInParent="true"
 
+
        android:id="@+id/imageview">
            android:layout_width="100dp"
+
    </ImageView>
 
 
            android:layout_alignParentBottom="true"
 
 
 
            android:text="Start"
 
 
 
            android:layout_marginLeft="50dp"
 
 
 
            />
 
 
 
    <Button
 
 
 
            android:id="@+id/button2"
 
 
 
            android:layout_height="wrap_content"
 
 
 
            android:layout_width="100dp"
 
 
 
            android:layout_alignParentBottom="true"
 
 
 
            android:layout_marginLeft="200dp"
 
 
 
            android:text="Stop"
 
 
 
            >
 
 
 
    </Button>
 
 
 
</RelativeLayout>
 
 
 
 
 
Step 3
 
 
 
In a Java class file you will add a media player and sound to the media player. On the start button click event the sound will start and on the stop button click event the sound will stop.
 
 
 
Create a Java file and write this:
 
 
 
package com.addmusic;
 
 
 
import android.media.MediaPlayer;
 
 
 
import android.os.Bundle;
 
 
 
import android.app.Activity;
 
 
 
import android.view.Menu;
 
 
 
import android.view.View;
 
 
 
import android.widget.Button;
 
 
 
import android.widget.ImageView;
 
 
 
 
   
 
   
 
+
    <Button
public class MainActivity extends Activity {
+
        android:id="@+id/button1"
 
+
        android:layout_height="wrap_content"
    MediaPlayer player;
+
        android:layout_width="100dp"
 
+
        android:layout_alignParentBottom="true"
    @Override
+
        android:text="Start"
 
+
        android:layout_marginLeft="50dp"
    protected void onCreate(Bundle savedInstanceState) {
+
        />
 
 
        super.onCreate(savedInstanceState);
 
 
 
        setContentView(R.layout.activity_main);
 
 
 
        ImageView imageview=(ImageView)findViewById(R.id.imageview);
 
 
 
        Button button1=(Button)findViewById(R.id.button1);
 
 
 
        Button button2=(Button)findViewById(R.id.button2);
 
 
 
      button1.setOnClickListener(new View.OnClickListener() {
 
 
 
            @Override
 
 
 
            public void onClick(View v) {
 
 
 
                player=MediaPlayer.create(MainActivity.this,R.raw.dog);
 
 
 
 
   
 
   
 
+
    <Button
          player.start();
+
        android:id="@+id/button2"
 
+
        android:layout_height="wrap_content"
 +
        android:layout_width="100dp"
 +
        android:layout_alignParentBottom="true"
 +
        android:layout_marginLeft="200dp"
 +
        android:text="Stop"
 +
        >
 
   
 
   
 
+
    </Button>
            }
 
 
 
        });
 
 
 
        button2.setOnClickListener(new View.OnClickListener() {
 
 
 
            @Override
 
 
 
            public void onClick(View v) {
 
 
 
                player.pause();
 
 
 
 
   
 
   
 +
</RelativeLayout>
  
            }
 
  
 
  
    });
+
==File AndroidManifest.xml==
  
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 +
    package="id.onnocenter.kucing" >
 
   
 
   
 
+
    <application
    }
+
        android:allowBackup="true"
 
+
        android:icon="@mipmap/ic_launcher"
 +
        android:label="@string/app_name"
 +
        android:supportsRtl="true"
 +
        android:theme="@style/AppTheme" >
 +
        <activity
 +
            android:name=".MainActivity"
 +
            android:label="@string/app_name"
 +
            android:theme="@style/AppTheme.NoActionBar" >
 +
            <intent-filter>
 +
                <action android:name="android.intent.action.MAIN" />
 
   
 
   
 
+
                <category android:name="android.intent.category.LAUNCHER" />
}
+
            </intent-filter>
+
        </activity>
 
+
    </application>
Step 4
 
 
 
Android menifest.xml file:
 
 
 
 
<?xml version="1.0" encoding="utf-8"?>
 
 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 
 
 
    package="com.addmusic"
 
 
 
    android:versionCode="1"
 
 
 
    android:versionName="1.0" >
 
 
 
 
 
 
    <uses-sdk
 
 
 
        android:minSdkVersion="7"
 
 
 
        android:targetSdkVersion="16" />
 
 
 
 
 
 
    <application
 
 
 
        android:allowBackup="true"
 
 
 
        android:icon="@drawable/ic_launcher"
 
 
 
        android:label="@string/app_name"
 
 
 
        android:theme="@style/AppTheme" >
 
 
 
        <activity
 
 
 
            android:name="com.addmusic.MainActivity"
 
 
 
            android:label="@string/app_name" >
 
 
 
            <intent-filter>
 
 
 
                <action android:name="android.intent.action.MAIN" />
 
 
 
 
 
 
                <category android:name="android.intent.category.LAUNCHER" />
 
 
 
            </intent-filter>
 
 
 
        </activity>
 
 
 
    </application>
 
 
 
 
   
 
   
 +
</manifest>
  
</manifest>
 
  
  

Latest revision as of 18:28, 20 October 2015

Sumber: http://www.c-sharpcorner.com/UploadFile/1e5156/add-sound-to-your-application-in-android-studio/


Di sini akan di jelasnkan cara agar android akan mengeluarkan suara setelah tombol Start di tekan.

Media player untuk menjalankan suara yang ada di folder raw dapat di aktifkan menggunakan perintah berikut

MediaPlayer player=MediaPlayer.create(MainActivity.this,R.raw.dog);

Folder ~/AndroidStudioProjects/Sapi/app/src/main/res/drawable

isi

cat1.png  cat2.png  cow1.png


Folder ~/AndroidStudioProjects/Sapi/app/src/main/res/raw

isi

believeme.wav  catmeow.wav  catpurr.wav  cow1.wav  cow.wav


Isi File MainActivity.Java

package id.onnocenter.kucing;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu; 
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageview=(ImageView)findViewById(R.id.imageview);
        Button button1=(Button)findViewById(R.id.button1);
        Button button2=(Button)findViewById(R.id.button2); 

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player=MediaPlayer.create(MainActivity.this,R.raw.cow1);
                player.start();
            }
        }); 

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.pause();
            }
        });
    }
}


File content_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: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=".MainActivity"
    android:background="#566780">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SoundComesonImageClick"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="20dp"/>

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/cow1"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:id="@+id/imageview">
    </ImageView>

    <Button
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="100dp"
        android:layout_alignParentBottom="true"
        android:text="Start"
        android:layout_marginLeft="50dp"
        />

    <Button
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="100dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="200dp"
        android:text="Stop"
        >

    </Button>

</RelativeLayout>


File AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="id.onnocenter.kucing" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> 

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




Referensi