I am Creating a App where i Placed 10 Buttons in Gridlayout and on every Button there is a audio file assigned to it & it should play that audio on Respective Button Click but in "1st Method it is not working " so i wrote long code and created "2nd method" and it is working but after clicking on 10th button and then i again click on 1st button sound doesn't works?
package com.example.star.spanishnumbers;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
//1st Method
/*public void playmusic(View view)
{
int id = view.getId();
Log.i("ID","Id value is" + id);
String nameID;
nameID = view.getResources().getResourceEntryName(id);
Log.i("ID","Id value is" + nameID);
int myMusic = getResources().getIdentifier(nameID,"raw","com.example.star.spanishnumbers");
MediaPlayer mediaplayer = MediaPlayer.create(this, myMusic);
mediaplayer.start();
mediaplayer.reset();
mediaplayer.release();
}*/
//2nd Method ~ working but have some issues unable to clear that issue
public void playone(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer= MediaPlayer.create(this,R.raw.one);
mediaPlayer.start();
}
public void playtwo(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.two);
mediaPlayer.start();
}
public void playthree(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.three);
mediaPlayer.start();
}
public void playfour(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.four);
mediaPlayer.start();
}
public void playfive(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.five);
mediaPlayer.start();
}
public void playsix(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer= MediaPlayer.create(this,R.raw.six);
mediaPlayer.start();
}
public void playseven(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.seven);
mediaPlayer.start();
}
public void playeight(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.eight);
mediaPlayer.start();
}
public void playnine(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.nine);
mediaPlayer.start();
}
public void playten(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.ten);
mediaPlayer.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Layout Code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.star.spanishnumbers.MainActivity">
<GridLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<Button
android:id="@+id/one"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:background="@drawable/one"
android:onClick="playmusic"
android:text="@string/one"
tools:text="@string/one" />
<Button
android:id="@+id/two"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:background="@drawable/two"
android:onClick="playmusic"
android:text="@string/two"
tools:text="@string/two" />
<Button
android:id="@+id/three"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:background="@drawable/three"
android:onClick="playmusic"
android:text="@string/three"
tools:text="@string/three" />
<Button
android:id="@+id/four"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:background="@drawable/four"
android:onClick="playmusic"
android:text="@string/four"
tools:text="@string/four" />
<Button
android:id="@+id/five"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:background="@drawable/five"
android:onClick="playmusic"
android:text="@string/five"
tools:text="@string/five" />
<Button
android:id="@+id/six"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:background="@drawable/six"
android:onClick="playmusic"
android:text="@string/six"
tools:text="@string/six" />
<Button
android:id="@+id/seven"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:background="@drawable/one"
android:onClick="playmusic"
android:text="@string/seven"
tools:text="@string/seven" />
<Button
android:id="@+id/eight"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:background="@drawable/four"
android:onClick="playmusic"
android:text="@string/eight"
tools:text="@string/eight" />
<Button
android:id="@+id/nine"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:background="@drawable/six"
android:onClick="playmusic"
android:text="@string/nine"
tools:text="@string/nine" />
<Button
android:id="@+id/ten"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:background="@drawable/three"
android:onClick="playmusic"
android:text="@string/ten"
tools:text="@string/ten" />
</GridLayout>
</android.support.constraint.ConstraintLayout>
Android MediaPlayer is Resource-Hungry. If you do not clean(release) MadiaPlayer after use, Your app can play only dozen or less audio(s) or video(s). Since MediaPlayer reference is local variable and can not be cleaned.
Change your mediaPlayer reference from local variable to field variable And release your mediaPlayer after use. (e.g Using onCompleteListener) Don't forget to release when your context is destroyed.(Activity, Service.. etc)
public void playthree(View view)
{
int id = view.getId();
Log.i("ID", "ID value is " + id);
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.three);
mediaPlayer.start();
// mediaPlayer can not be released.
}