0

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

Bharath Gupta
  • 344
  • 3
  • 8
  • 20

4 Answers4

5

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()

thomaux
  • 19,133
  • 10
  • 76
  • 103
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • I had got the required functionality.But i am unable to give the resume option to the player when pause is pressed.i.e., the audio file starts playing from the first when I click the play button after clicking the pause.. Please some one help m,e to clear it. – Bharath Gupta Jan 09 '12 at 14:23
  • @BharathGupta you need to change the button image dynamically while playing and pause. – Padma Kumar Jan 09 '12 at 14:44
  • @ Padma Kumar thanks a ton, I had done well with your suggestion and could you please tell me a solution for the following problem faced by me. When I navigate to other app and come back to this and click on the options, its giving ma a prompt Force close. – Bharath Gupta Jan 10 '12 at 06:42
  • just post your error log so that only I can know whats your problem. minanjal.com@gmail.com – Padma Kumar Jan 10 '12 at 07:00
1

Replace

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

with

playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);
Guillaume
  • 22,694
  • 14
  • 56
  • 70
1

use this code to create mediaplayer context

 playr = MediaPlayer.create(getBaseContext(),R.raw.showme);
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Ranjit Mishra
  • 440
  • 4
  • 8
0

This is my activity example to play videos in Android using VideoView:

package br.com.player;

import android.support.v7.app.AppCompatActivity;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class PlayerActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        Button buttonPlayVideo = (Button)findViewById(R.id.playvideobuttom);

        getWindow().setFormat(PixelFormat.UNKNOWN);

        VideoView mVideoView = (VideoView)findViewById(R.id.videoview);

        buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {

                    VideoView mVideoView = (VideoView)findViewById(R.id.videoview);

                    String uriPath = "android.resource://br.com.player/"+R.raw.filevideo;

                    Uri uri = Uri.parse(uriPath);
                    mVideoView.setVideoURI(uri);
                    mVideoView.requestFocus();
                    mVideoView.start();

            }});
     }
}

And this is activity's xml player.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

   <VideoView
    android:id="@+id/videoview" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>
Henrique Ho
  • 101
  • 1
  • 5