MediaPlayer : I'm getting mad !

All your problems with Audio, Video and Images.

MediaPlayer : I'm getting mad !

Postby bluef » Tue Dec 22, 2009 11:14 am

Hello !

I'm getting mad trying to make my application play a sound !...

I'm developping under Eclipse, with SDK 1.5. I put my mp3 file into the raw folder.

And put this function in my main activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void playMusic() {
  2.  
  3.                 try {
  4.  
  5.                         MediaPlayer mp = new MediaPlayer();
  6.  
  7.                         mp.create(this, R.raw.mynotif);
  8.  
  9.                         mp.start();
  10.  
  11.                         Toast.makeText(this, "Launched ?",Toast.LENGTH_SHORT).show();
  12.  
  13.                         mp.release();
  14.  
  15.                 } catch (Exception e) {
  16.  
  17.                         Toast.makeText(this, "Exception: " + e.getMessage(),Toast.LENGTH_SHORT).show();
  18.  
  19.                 }
  20.  
  21.         }
  22.  
  23.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The activity only display a view, and launch the playMusic function.

The result is only a TOAST saying "Launched ?" : taht means there is no exception but ... I CANNOT HEAR ANYTHING !

I thought it was an emulator setting issue, but the application "Music" can play music and I can hear it...

Any idea ??

Tahnk you very much for your precious help !!!

Nek.
bluef
Developer
Developer
 
Posts: 33
Joined: Thu Nov 19, 2009 9:45 am

Top

Postby bluef » Tue Dec 22, 2009 1:04 pm

Additional info

If I add the method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mp.prepare();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

it raises a exception, saying the mp is NULL...

If it helps...
bluef
Developer
Developer
 
Posts: 33
Joined: Thu Nov 19, 2009 9:45 am

Postby qlimax » Tue Dec 22, 2009 1:31 pm

...
MediaPlayer mp= MediaPlayer.create(this,R.raw.mynotif)
mp.start();
....
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby qlimax » Tue Dec 22, 2009 1:31 pm

and you should call the release method, when you are done with the sound.
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby bluef » Tue Dec 22, 2009 1:33 pm

Thanks for the answer, but I found...

I changed three things: the declaration of the MediaPlayer, the context, and the RELEASE...

I think the release is launched too "early", and stop the sound...

Here is the working code :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         MediaPlayer mp;
  3.  
  4.        
  5.  
  6.         public void playMusic(Context theContext) {
  7.  
  8.                 try {
  9.  
  10.                        
  11.  
  12.                 mp = new MediaPlayer();
  13.  
  14.                 mp = MediaPlayer.create(theContext, R.raw.mynotif);
  15.  
  16.                 mp.start();                    
  17.  
  18.                         Toast.makeText(this, "Launched ?",Toast.LENGTH_SHORT).show();
  19.  
  20.                         mp.release();
  21.  
  22.                 } catch (Exception e) {
  23.  
  24.                         Toast.makeText(this, "Exception: " + e.getMessage(),Toast.LENGTH_SHORT).show();
  25.  
  26.                 }
  27.  
  28.         }
  29.  
  30.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thanks !
bluef
Developer
Developer
 
Posts: 33
Joined: Thu Nov 19, 2009 9:45 am

Postby bluef » Tue Dec 22, 2009 1:38 pm

Yep ! Thank you very much, qlimax !!
bluef
Developer
Developer
 
Posts: 33
Joined: Thu Nov 19, 2009 9:45 am

Top

Postby qlimax » Tue Dec 22, 2009 1:41 pm

you can set a listener to release the resources when you are done,

something like that:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mp.setOnCompletionListener(new OnCompletionListener(){
  2.  
  3.  
  4.  
  5. mp.release();
  6.  
  7.  
  8.  
  9. });
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests