13

I have written the most basic application I can think of to try to play an mp3 file, but it is not working. I don't get any errors, but when the application starts up, the sound is not played.

public class soundtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MediaPlayer mp = new MediaPlayer();
        mp.create(getApplicationContext(), R.raw.norm_iphone_money);
        mp.start();
    }
}

What am I missing? I have "norm_iphone_money.mp3" inside the res/raw folder. The file plays fine in Windows Media Player and iTunes.

I'm using the latest versions of Java SDK and Eclipse for Java. The app is targeted for Android 2.2 and runs fine in the emulator despite no sound.

user613832
  • 141
  • 1
  • 1
  • 3
  • Have you tried on a real device? You might try using the setDataSource() and prepare() methods before a MediaPlayer start() instead of create(), but I doubt that will work if create() doesn't... – DJC Feb 12 '11 at 04:29

6 Answers6

44

The problem is that the media volume is set to 0 (not the ringer volume). You can set it by:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

Have a look at the developer page and another question for the explanation of parameters for setStreamVolume(int, int, int).

Xuan Tung Vu
  • 972
  • 9
  • 18
10

Try replacing these two lines:

MediaPlayer mp = new MediaPlayer();
mp.create(getApplicationContext(), R.raw.norm_iphone_money);

with this one line:

MediaPlayer mp = MediaPlayer.create(this, R.raw.norm_iphone_money);

And see if that works.

ShadowGod
  • 7,891
  • 3
  • 28
  • 31
  • 7
    if these two are writings are any logically different whatsoever in the compiled code, I'd be shocked. – DJC Feb 12 '11 at 04:31
  • 1
    No but testing it myself it only works that way, it doesn't work how he originally wrote it. If anyone else cares to explain exactly *why* that is, that would be great. Playing a RAW audio resource is described right here http://developer.android.com/guide/topics/media/index.html – ShadowGod Feb 12 '11 at 06:35
  • 7
    Of course it is logically different. mp.create will call the static method, and it will return a new instance of a MediaPlayer, which is never stored. Then, he calls start() on the default-constructed instance. – Yuyo Jul 18 '11 at 06:15
1

The problem is with emulator, change the emulator or try to run the application on a real device. This should solve the problem.

-2

The static method create(Context, int) from the type MediaPlayer should be accessed in a static way. Try this:

MediaPlayer.create(getApplicationContext(), R.raw.norm_iphone_money).start();

It will play the .mp3 with this line too

mp.create(getApplicationContext(), R.raw.norm_iphone_money).start();
d45ks72a
  • 39
  • 4
-4

I would suggest this :

MediaPlayer mp = new MediaPlayer();
//bla bla bla
mp = MediaPlayer.create(getApplicationContext(), R.raw.norm_iphone_money);
Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
Guzhov
  • 5
-4

Had the same problem after i clicked to start the Media Player, the screen went black and the app stopped.

i just changed

MediaPlayer mp = MediaPLayer.create(this,R.raw.sound); mp.start();

to

MediaPlayer mp = MediaPLayer.create(this,R.raw.sound).start();

I am not really sure what is the difference there, but it solved my problem.

Taryn
  • 242,637
  • 56
  • 362
  • 405