Error in NullPointerException in android market on music files

Here are the error trace from android:

java.lang.NullPointerException
at com.bappy.cror.MultiMedia.playSong(MultiMedia.java:48)
at com.bappy.cror.MultiMedia.play(MultiMedia.java:132)
at com.bappy.cror.MainActivity$1.onClick(MainActivity.java:146)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)

Here is my code:

public class MultiMedia {
    private static MediaPlayer mediaPlayer;
    private static AudioManager am;

    public static int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    public static int getDuration() {
        return mediaPlayer.getDuration();
    }

    public static int getMaxVolume() {
        return am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    public static int getVolume() {
        return am.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    public static boolean isPuase() {
        if (mediaPlayer != null) {
            return isPause;
        }
        return false;
    }

    public static boolean isStarting() {
        if (mediaPlayer != null) {
            return isStarting;
        }
        return false;
    }

    public static void playSong(Context con, int songPath) {

        mediaPlayer = new MediaPlayer();
        am = (AudioManager) con.getSystemService(Context.AUDIO_SERVICE);
        mediaPlayer = MediaPlayer.create(con, songPath);
        mediaPlayer.setLooping(false);
        mediaPlayer.start();

        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer arg0) {

                destroy();
            }

        });

    }

    public static void seek(int msec) {
        if (mediaPlayer != null) {

            if (msec >= getDuration()) {
                msec = getDuration();
            } else if (msec <= 0) {
                msec = 0;
            }
            mediaPlayer.seekTo(msec);
        }
    }

    public static synchronized void setVolume(int index) {      
        am.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
    }

    public static void updatePos(int currentPosition) {
        pos = currentPosition;

    }

    private int intVolume;

    public static boolean isPause = false, ismute = false;

    private static boolean isStarting = false;

    Context context;

    private static int pos = 0;

    public static void destroy() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            isStarting = false;

        }
    }

    public static void pause() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
            isPause = true;
        }
    }

    public static void play(Context con, int resource) {
        if (!ismute) {
            if (mediaPlayer != null) {
                mediaPlayer.start();

                isPause = false;
                isStarting = true;
            } else {
                playSong(con, resource);
            }
        }
    }

    public static void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.seekTo(0);
            mediaPlayer.pause();
        }
    }

    public MultiMedia(Context con) {
        this.context = con;
        am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mediaPlayer = new MediaPlayer();

    }

    public boolean getMute() {
        return getVolume() == 0;
    }

    public String getSongName() {
        return "this is song name";
    }

    public void setMute(Boolean isOn) {
        if (mediaPlayer != null) {
            if (isOn) {
                intVolume = getVolume();
                am.setStreamMute(AudioManager.STREAM_MUSIC, true);
            } else {
                am.setStreamMute(AudioManager.STREAM_MUSIC, false);
                setVolume(intVolume);
            }
        }
    }

}

Line 48 in this code is : mediaPlayer.setLooping(false);

I tried to re-create this error on my device but cant. I dont understand why i am getting this error. I will appreciate if any senior member can guide me or help me figure this out. Thanks.

Answers


You have to start mediaplayer like this.

1.MediaPlayer player=new MediaPlayer();

player.setDataSource(String path);

player.prepare();

Or

2.MediaPlayer player=MediaPlayer.create(ctx,SongPath);

When you are trying to play with first method,you must use prepare method.In second method no need for it.You might choose one method only,but your code seems to be using both.


The line before that is creating the media player which returns null if creation fails. It looks like that object must be null which is throwing the NullException when you try to set the looping on the mediaPlayer object. Is there anyway to figure out what is getting sent into the playSong method for context and songPath?


From reference: http://developer.android.com/reference/android/media/MediaPlayer.html#create%28android.content.Context,%20int%29

your parameters to create() MediaPlayer have problems.


MediaPlayer.create(..) return null , i think may be the second param cause the exception, make sure 'songPath' can be found properly


Need Your Help

Regex expression does not work

c# .net regex

why does the code below insert an item to the groupCollection even though "aaa" does not contain 12 digits in a row ?

Javascript Variable undefined and onchange not working

javascript variables undefined onchange

I'm a beginner with Javascript so maybe this is easy for an expert to fix. I'm working on this: http://www.3dsbuzz.com/receng/test.php The source code is below.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.