1

I have created a package for SoundManagement, with a single class extending MediaPlayer. I was hoping to globally control this variable. Here is my Code:

package soundmanagement;

import android.content.Context;

import android.media.MediaPlayer;

import java.io.IOException;

public class MusicManager extends MediaPlayer {

    public static MediaPlayer mediaPlayer = new MediaPlayer();

    public void MusicManager() {
    }

    public static MediaPlayer create(Context context, int musicID) {
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        mediaPlayer.create(context, musicID);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaPlayer.create(context, musicID);
    }

    public void prepare() {
        try {
            mediaPlayer.prepare();
            super.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
        super.start();
    }

    public void pause() {
        mediaPlayer.pause();
        super.pause();
    }

    public void stop() {
        super.stop();
        mediaPlayer.release();
    }

    public void release() {
        mediaPlayer.release();
        super.release();
    }

    public boolean isPlaying(boolean isPlaying) {
        super.isPlaying();
        return isPlaying;
    }

    public void setLooping(boolean setLoop) {
        mediaPlayer.setLooping(setLoop);
        super.setLooping(setLoop);
    }
}

And in my MainActivity.class, in onCreate(), all I do is write this code in the onCreate():

MusicManager.mediaPlayer.create(MainActivity.this, R.raw.riseofcc);
MusicManager.mediaPlayer.start();

The application compiles and runs fine, just no music playing once it starts up.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Azurai
  • 107
  • 2
  • 10
  • If your class extends `MediaPlayer`, then a `MusicManager` object **is a** `MediaPlayer`. So why are you creating a new `MediaPlayer`, so that you have two `MediaPlayer`s? Are you sure this is what you want? Maybe it's what you want, I don't understand your code, but I've seen that mistake before. – ajb Apr 24 '14 at 18:34
  • @ajb... I think the problem is that MediaPlayer doesn't expose a lot of its internal workings and marks a lot of methods as final. ... however, it's still not the optimal approach. – ajacian81 Apr 24 '14 at 18:54
  • Yeah sorry guys, I just wanted to be a Media Player that was global within the whole application. I will have multiple Activities manipulating it (pausing, starting, creating) and I needed it in a separate format. I have tried using a single MediaPlayer within each Activity, and things got complicated quickly. – Azurai Apr 24 '14 at 20:22
  • possible duplicate of [Android MediaPlayer Problems :"Error (-38 , 0) " and "stop called in state 1"](http://stackoverflow.com/questions/11913108/android-mediaplayer-problems-error-38-0-and-stop-called-in-state-1) – rds Jun 22 '15 at 08:56

1 Answers1

1

You're taking a curious approach to the MediaPlayer, but one thing that jumps out at me is this:

public static MediaPlayer create(Context context, int musicID) {
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        mediaPlayer.create(context, musicID);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaPlayer.create(context, musicID);
    }

You call mediaPlayer.create(), then .prepare(), then create(...) again?

Also, create is a static method, so it should be called as MediaPlayer.create and you should hold the reference to it. When you're calling prepare(), you're calling prepare() on a MediaPlayer object that hasn't been created.

Edit: If you want to keep this method, revise it accordingly:

public static MediaPlayer create(Context context, int musicID) {
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        mediaPlayer = MediaPlayer.create(context, musicID);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaPlayer;
    }
ajacian81
  • 7,419
  • 9
  • 51
  • 64
  • It was telling me that a return was needed, and I have tried returning: return mediaPlayer; || return mediaPlayer.create(...); And this code was one of my last tries before I asked on here. I see what you are saying and I will try to re-do it. – Azurai Apr 24 '14 at 20:24
  • Thank you, I do appreciate it! In any other time I must extend an API class, for methods within the super class, it's best to use the constructor as stated?: mediaPlayer = MediaPlayer.create(..); – Azurai Apr 24 '14 at 20:46
  • To be honest, I'm not sure why you're extending the MediaPlayer class. Usually it's better to write a wrapper to handle the various states, something like this: https://gist.github.com/danielhawkes/1029568 – ajacian81 Apr 24 '14 at 20:49
  • That looks like the framework of the other layouts I've seen. I thought it would be simpler to extend it for its methods, so I can manipulate a separate class that is shared among all Activites. But it looks like as long as its instantiated as a MediaPlayer Object, it holds all the methods. Sorry, I'm completely new with no mentor. – Azurai Apr 24 '14 at 20:55
  • It's ok we've all been there :). I still don't see why you'd need to extend MediaPlayer. There's not much in functionality you're gaining. Even in your above object, you're extending MediaPlayer but you're holding a separate reference to a different MediaPlayer object. – ajacian81 Apr 24 '14 at 20:59
  • The really interesting parts of MediaPlayer are marked as final and therefore can't be modified anyway: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.2_r1/android/media/MediaPlayer.java#MediaPlayer – ajacian81 Apr 24 '14 at 20:59
  • Yeah that first real step is long because before that we have to have a drive to move and then crawl first. Ah, so it pretty much is a useless extensive then. I would have to take out all the super..() statements then? I mean I think they are useless, since it is done via mediaPlayer.release(); And THANK YOU! I have searched for that code, so I could research it. You would think the official android site would have the actual code for it, then clips of it. – Azurai Apr 24 '14 at 21:14