DRF
DRF

Reputation: 11

Android studio media player cannot work

I have an issue which media player cannot be played while pressing the play button even though the song is in the raw directory .

Here the code below.

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;

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

        mediaPlayer = MediaPlayer.create(this,R.raw.song);

        Button play = (Button) findViewById(R.id.play);

        Button pause = (Button) findViewById(R.id.pause);


        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this,"Play",Toast.LENGTH_SHORT).show();
                mediaPlayer.start();
            }
        });


        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this,"Pause",Toast.LENGTH_SHORT).show();
                mediaPlayer.pause();
            }
        });

    }


}

And here is the layout file below:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.android.musicplayer.MainActivity">


    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"/>

    <Button
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause"/>


</LinearLayout>

Upvotes: 0

Views: 390

Answers (2)

Arslan Maqbool
Arslan Maqbool

Reputation: 529

I have found the SoundPoolPlayer which is better than the mediaplayer
SoundPoolPlayer custom extention from Android SoundPool with setOnCompletionListener without the low-efficiency drawback of MediaPlayer.

Creation:

SoundPoolPlayer mPlayer = SoundPoolPlayer.create(context, resId);
mPlayer.setOnCompletionListener(
    new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {  //mp will be null here
            Log.d("debug", "completed");
        }
    };
);

play:

mPlayer.play()

pause:

mPlayer.pause();

stop:

mPlayer.stop();

resume:

mPlayer.resume();

isPlaying:

mPlayer.isPlaying();

Here is the Link to Download the player Class

Enjoy- Happy Coding

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 174

 try{ 
     mediaPlayer.prepare();  
    }catch(Exception e){e.printStackTrace();}  

Upvotes: 2

Related Questions