1

Hello i'm a new coder to android. I have a program that "Plays" "Pauses" and "Stops" a local .mp3 file. Ive found the source from this website here.

http://android-er.blogspot.com/2010/07/android-mediaplayer.html

So I post in the code and everything seems to work great until I actually hit play and get an error.

Ive tried to read the error but cant figure out what exactly its saying?

My error is this(logcat):

06-12 12:02:38.810: E/AndroidRuntime(4190): FATAL EXCEPTION: main
06-12 12:02:38.810: E/AndroidRuntime(4190): java.lang.NullPointerException
06-12 12:02:38.810: E/AndroidRuntime(4190):atcom.reg.ihigh.Cocaine$1.onClick(Cocaine.java:53)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.view.View.performClick(View.java:2485)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.view.View$PerformClick.run(View.java:9089)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Handler.handleCallback(Handler.java:587)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Looper.loop(Looper.java:123)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.app.ActivityThread.main(ActivityThread.java:3806)
06-12 12:02:38.810: E/AndroidRuntime(4190): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 12:02:38.810: E/AndroidRuntime(4190): at java.lang.reflect.Method.invoke(Method.java:507)
06-12 12:02:38.810: E/AndroidRuntime(4190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-12 12:02:38.810: E/AndroidRuntime(4190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-12 12:02:38.810: E/AndroidRuntime(4190): at dalvik.system.NativeStart.main(Native Method)

Class

package com.reg.ihigh;

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

public class Cocaine extends Activity {
    MediaPlayer mediaPlayer;
     Button buttonPlayPause, buttonQuit;
     TextView textState;

     private int stateMediaPlayer;
     private final int stateMP_NotStarter = 0;
     private final int stateMP_Playing = 1;
     private final int stateMP_Pausing = 2;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.drugg);

          buttonPlayPause = (Button)findViewById(R.id.playButton);
          buttonQuit = (Button)findViewById(R.id.quitButton);
          textState = (TextView)findViewById(R.id.state);

          buttonPlayPause.setOnClickListener(buttonPlayPauseOnClickListener);
          buttonQuit.setOnClickListener(buttonQuitOnClickListener);

          initMediaPlayer();

      }

      private void initMediaPlayer()
      {
       mediaPlayer = new  MediaPlayer();
          mediaPlayer = MediaPlayer.create(Cocaine.this, R.raw.cocaine);
          stateMediaPlayer = stateMP_NotStarter;
          textState.setText("- IDLE -");
      }

      Button.OnClickListener buttonPlayPauseOnClickListener
       = new Button.OnClickListener(){

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(stateMediaPlayer){
        case stateMP_NotStarter:
         mediaPlayer.start();
         buttonPlayPause.setText("Pause");
         textState.setText("- PLAYING -");
         stateMediaPlayer = stateMP_Playing;
         break;
        case stateMP_Playing:
         mediaPlayer.pause();
         buttonPlayPause.setText("Play");
         textState.setText("- PAUSING -");
         stateMediaPlayer = stateMP_Pausing;
         break;
        case stateMP_Pausing:
         mediaPlayer.start();
         buttonPlayPause.setText("Pause");
         textState.setText("- PLAYING -");
         stateMediaPlayer = stateMP_Playing;
         break;
        }

       }
      };

      Button.OnClickListener buttonQuitOnClickListener
     = new Button.OnClickListener(){

      @Override
      public void onClick(View v) {
       // TODO Auto-generated method stub
       mediaPlayer.stop();
       mediaPlayer.release();
       finish();
      } 
      };

}

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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>
<Button
android:id="@+id/playButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

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

android:text="Quit"/>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

EDIT FIX:

When I converted my original mp3's to compress them some how it got corrupted over the copy proccess to the raw folder. So I just recompressed copied over and bam. Everything works! Thanks @MattWolfe

Reg
  • 27
  • 7
  • It looks like your mediaPlayer object is null. From the code posted above I don't see how that is possible. Is there any other code you didn't post (perhaps onPause or onDestroy method?) – Matt Wolfe Jun 12 '12 at 17:20
  • @MattWolfe nope, thats everything. Doesn't make since to me either. – Reg Jun 12 '12 at 17:23
  • try changing the: mediaPlayer = MediaPlayer.create(Cocaine.this, R.raw.cocaine); for mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.cocaine); – Raykud Jun 12 '12 at 17:32
  • @Raykud Same error:`06-12 12:33:44.551: E/AndroidRuntime(5046): java.lang.NullPointerException 06-12 12:33:44.551: E/AndroidRuntime(5046): at com.reg.ihigh.Cocaine$1.onClick(Cocaine.java:54) ` – Reg Jun 12 '12 at 17:34
  • can you change the file to use the one in that sample application that you copied your code from? Change your reference to use the other file as well and see if that works. – Matt Wolfe Jun 12 '12 at 17:51
  • Also, as others have pointed out, check not just your own apps log but look at all of the logcat messages (with ddms or whatever) and most likely you'll see some errors related to the mediaplayer.. Perhaps the file is invalid or the mediaplayer is already busy or something similar.. – Matt Wolfe Jun 12 '12 at 17:52
  • @MattWolfe Thanks, I fixed it. When I converted my original mp3's to compress them some how it got corrupted over the copy proccess to the raw folder. – Reg Jun 12 '12 at 17:56

4 Answers4

3

The MediaPlayers create() method says that:

Returns a MediaPlayer object, or null if creation failed 

That is the case thats happening in your case. Read this answer and this answer for further information about resolving your issue.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
0

Figure out what is on line 53 and add a check to make sure that whatever object (to the left of the .) is not null before you call one of its methods.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
0

You are setting the onClickListener before you call the method initMediaPlayer(), which initializes mediaPlayer. As mediaPlayer is not initialized in your onClickListener, it throws the NullPointerException.

In your onCreate, call initMediaPlayer before setting your onClickListeners.

lowell
  • 521
  • 1
  • 5
  • 10
  • It wouldn't matter anyways because you can't possibly click before the initMediaPlayer is called since all events go through the UI thread which wouldn't pickup events while in onCreate – Matt Wolfe Jun 12 '12 at 17:37
0

is your media resource ok? Try it with a uri and see if it works (you can use a url to some online mp3 file)

cris.h
  • 233
  • 2
  • 7