Android Media Player无法多次播放声音

时间:2017-10-02 10:35:41

标签: android android-mediaplayer

我正在创建一个应用程序,我在Gridlayout中放置10个按钮,在每个按钮上都有一个分配给它的音频文件&它应该在相应的按钮上播放该音频单击但是在     "第一种方法不起作用"     所以我写了长代码并创建了"第二种方法"它正在工作但是点击第10个按钮然后我再次点击第1个按钮声音不起作用?

    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);
            }
        }

布局代码

<?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>

1 个答案:

答案 0 :(得分:0)

Android MediaPlayer资源匮乏。如果您在使用后没有清理(发布)MadiaPlayer,您的应用程序只能播放十几个或更少的音频或视频。由于MediaPlayer引用是局部变量,因此无法清除。

将mediaPlayer参考从局部变量更改为字段变量并在使用后释放mediaPlayer。 (例如使用onCompleteListener) 不要忘记在上下文被破坏时释放。(活动,服务......等)

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.
}