No sound reading a MP3 file

All your problems with Audio, Video and Images.

No sound reading a MP3 file

Postby ammaia » Wed Nov 03, 2010 8:53 pm

Hi all!

I am trying to read a MP3 file that I have in my 'raw' folder.
The code compiles without error but there are no sound in my emulator.
I am using Android 2.2

This is the code that I am using:

Code: Select all
String path = "/raw/pd.mp3";
MediaPlayer mPlayer = new MediaPlayer();
  try {
      mPlayer.setDataSource(path);
      mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mPlayer.prepare();
       } catch (IOException ex) {
       }catch (IllegalStateException ex) {
     }
      mPlayer.start();
}


If anyone can find the problem is I will be very greatfull.

Best regards
PM
ammaia
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 03, 2010 8:47 pm

Top

Re: No sound reading a MP3 file

Postby Schermvlieger » Wed Nov 03, 2010 10:41 pm

You can put an onErrorListener in there to check for problems:

http://developer.android.com/reference/ ... tener.html

But before that, check all the obvious things: can you play the same file with another player, is the media volumestream not muted, etc.
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: No sound reading a MP3 file

Postby ammaia » Thu Nov 04, 2010 12:31 am

Well, in my first topic I was a little confused about the reading file...

I have the "sound_file_1.mp3" file inside my Android application.
So, this is a RAW resource as explained in AndroidGoogle link http://developer.android.com/guide/topics/media/index.html

They say that is only necessary 2 code lines to play it:
Code: Select all

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);   
mp.start();

and the sound file must be inside the /res/raw folder of the project.

Running the application there are no sound and an warning message appears:
"The application has stopped unexpectedly. Please try again."

Does anyone have experience to play a MP3 RAW file that can understand what is wrong?

Best regards
PM
ammaia
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 03, 2010 8:47 pm

Re: No sound reading a MP3 file

Postby Schermvlieger » Thu Nov 04, 2010 9:49 am

I do it like this (.ogg files should be same as .mp3 but I recommend you convert your mp3 to ogg anyway)

Code: Select all
private MediaPlayer mMediaPlayer = new MediaPlayer();
Uri musicfile = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.musicfile);
mMediaPlayer.reset();
try {
     mMediaPlayer.setDataSource(getApplicationContext(), musicfile);
     mMediaPlayer.prepare();
     mMediaPlayer.start();
} catch (IllegalArgumentException e) {
     e.printStackTrace();
} catch (SecurityException e) {
     e.printStackTrace();
} catch (IllegalStateException e) {
     e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: No sound reading a MP3 file

Postby ramu_57 » Wed Nov 17, 2010 12:40 pm

hi
Playing from a Raw Resource
Perhaps the most common thing to want to do is play back media (notably sound) within your own applications. Doing this is easy:

Put the sound (or other media resource) file into the res/raw folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class
Create an instance of MediaPlayer, referencing that resource using MediaPlayer.create, and then call start() on the instance:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

suppose if is not work

for that we use
string path="/sdcard/filemane.mp3"
try{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("path");
mp.prepare();
mp.start();
}
catch(Exception e){
log.i("voice test",e.toString();
}
ramu_57
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Nov 02, 2010 10:10 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 10 guests