Bharath Gupta Bharath Gupta - 4 months ago 13
Android Question

Android Media Player

I do simple small programs in android for my practice. While I am on the way to create a simple player app, I had to face an error which I could not solve.The following is my code.

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndrmusiActivity extends Activity {
/** Called when the activity is first created. */
public MediaPlayer playr;
public Button b1;
public Button b2;
public Button b3;
@Override
public void onCreate(Bundle State) {
super.onCreate(State);
setContentView(R.layout.main);
b1= (Button)findViewById(R.id.play);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playr = MediaPlayer.create(this,R.raw.showme);
playr.start();

}
});
b2= (Button)findViewById(R.id.Pause);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
playr.pause();
}
});
b3=(Button)findViewById(R.id.Stop);
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playr.stop();
playr.reset();

}
});

}
}


Now I faced the error at play method at the line

playr = MediaPlayer.create(this,R.raw.showme);


Could any one please help me in this aspect.
Thanks in adv

Answer

Use this line there

 playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);

If error remains post your error logcat.

create this in your on create() method.

 playr = MediaPlayer.create(this,R.raw.showme);

For resume()

use your your code as same as below.

b1 = (Button) findViewById(R.id.play);
b1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             playr.start();
         }
     });

Calling start() to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call to start() returns, the paused MediaPlayer object goes back to the Started state.

http://developer.android.com/reference/android/media/MediaPlayer.html#start()

Comments