skip to Main Content

Why when I create the MediaPlayer variable in the Main Activity, the app crashes?

( This code works fine )

public class MainActivity extends AppCompatActivity {


MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);

    public void play(View view){

        audio.start();
    }
    public void pause(View view){
       
            audio.pause();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        audio = MediaPlayer.create(this, R.raw.my_audio);

    }
}

( But this code doesn’t work; the app crashes)

public class MainActivity extends AppCompatActivity {
    
    
    MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
    
        public void play(View view){
    
            audio.start();
        }
        public void pause(View view){
           
                audio.pause();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

PLEASE give me an explanation 🙁

3

Answers


  1. in second case you havent created MediaPlayer instance at all

    MediaPlayer audio;
    

    so every audio.anyMethodCall() will throw NullPointerException

    just keep audio = MediaPlayer.create(this, R.raw.my_audio); line in onCreate, thats proper place for initing player

    and please learn some basics about programming…

    Login or Signup to reply.
  2. private MediaPlayer audio;
    

    oncreate method

     audio= MediaPlayer.create(this, R.raw.shape_of_you);
                        audio.start();
    

    You can do something like this

    Login or Signup to reply.
  3. The Correct code should be:

    public class MainActivity extends AppCompatActivity {
        
        
        MediaPlayer audio;
        
            public void play(View view){
        
                audio.start();
            }
            public void pause(View view){
               
                    audio.pause();
            }
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }
        }
    

    the only change I did was removing declaration before oncreate method:

    Earlier:

    MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
    

    Correct Code:

    MediaPlayer audio;
    

    Explanation:

    1. onCreate() method is for definition like adding audio to MediaPlayer, defining textView, etc, since onCreate() gets triggered after creation of activity(activity_main).
    2. You added audio 2 times, which is not needed, you added an audio at first and then replace it with another audio in onCreate() method
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search