Playing local sound with MediaPlayer?

All your problems with Audio, Video and Images.

Playing local sound with MediaPlayer?

Postby Danneman » Thu Aug 20, 2009 9:47 pm

Ive got a simple code that I want to play a local mp3 (mySound, which is located in the asset-folder).

Im trying to use MediaPlayer, but any other advice is greatly appreciated.

Despite having followed this official devguide on developer.android.com (Under "Playing from a File or Stream", http://developer.android.com/guide/topi ... index.html), which resulted in the code you see below, no sound is being played. And no error is being reported either.

Code: Select all
package com.myApp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.media.MediaPlayer;
import android.net.Uri;
import java.io.*;

import com.myApp.R.id;


public class myApp extends Activity
{
    MediaPlayer mp;
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mp = new MediaPlayer();
        try
        {
           mp.setDataSource("file:///android_asset/mySound.mp3");
           mp.prepare();
           mp.start();
        }
        catch(Exception e) {}
    }
}


As you can see, Ive replaced the string "PATH_TO_FILE" in the tutorial with "file:///android_asset/mySound.mp3".


I have tried using the res-folder, but since I have loads and loads of sound-files I need to be able to load them dynamically, and so far I havent found a solution for doing so.

For instance, this code plays the file beautifully.
Code: Select all
package com.myApp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.media.MediaPlayer;
import android.net.Uri;
import java.io.*;

import com.myApp.R.id;


public class myApp extends Activity
{
    // Init:    MediaPlayer
    MediaPlayer mp;
       
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mp = new MediaPlayer();
        mp = MediaPlayer.create(getBaseContext(), R.raw.mySound);
        mp.start();
    }
}


But replacing:
Code: Select all
mp = MediaPlayer.create(getBaseContext(), R.raw.mySound);

..with something like..
Code: Select all
String soundName = "mySound";
mp = MediaPlayer.create(getBaseContext(), Uri.parse("R.raw." + soundName));

..doesnt work.

Any suggestions?
Last edited by Danneman on Fri Aug 21, 2009 9:23 pm, edited 1 time in total.
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Top

Postby mad.dogg » Thu Aug 20, 2009 10:53 pm

The fact that you do not get an exception does not mean that there is no error. Check out the MediaPlayer's listeners may be one of them will tell you what is going on, like the OnErrorListener one for example :)
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby Danneman » Fri Aug 21, 2009 1:48 pm

Thanks, and yes, there were errors :shock:

1) MediaPlayer: Unable to create media player
2) PlayerDriver: Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
3) MediaPlayer: error (1, -4)

However, with my limited knowledge Im in no position to descifer what this actually means. Do you know?
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Postby mad.dogg » Fri Aug 21, 2009 4:04 pm

I am not sure, cause I haven't used MediaPlayer this way, but I think that the problem is with the path
("file:///android_asset/mySound.mp3") you try load.
From your code I am guessing that the file is in your assets, if this is true try AssetManager to get its path or an InputStream to it.
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby Danneman » Fri Aug 21, 2009 9:15 pm

I just dont get how I could use AssetManager for this since its functions seem to open the files and here we want a path to them.

Unfortunately Im VERY new when it comes to Eclipse and Java (mostly done C++/C# before), and Im finding the documentation to be rather uninformative since they lack any codesnippets to help illustrate how to implement the code for a novice.
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Postby mad.dogg » Sat Aug 22, 2009 1:04 am

No worries. And you are right the documentation of some classes is ... well strange.
So option one we stick to the assets.
In the AssetsManager class you have this method
AssetFileDescriptor openFd(String fileName)
And in the AssetFileDescriptor you have FileDescriptor getFileDescriptor()
And finally in the MediaPlayer class you have void setDataSource(FileDescriptor fd)
Which should work (haven't tested it though).

Anyway if you do not like the above approach here comes option number two you may move the file from your assets to your resources in the /raw directory. And just use this static method of the MediaPlayer class
public static MediaPlayer create(Context context, int resid)

If you don't like both approaches, go deeper in the docs I haven't worked with assets for a long time so I might be missing something. Good luck.
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Top

Postby Danneman » Sat Aug 22, 2009 5:59 pm

Ive tried this approach, but get stuck on the finishing-line it seems.
Code: Select all
        AssetManager assetManager = null;
        try{
           assetManager.openFd("file:///android_asset/ahora.mp3");
        } catch (Exception e) {}     
       
        AssetFileDescriptor assetFileDescriptor = null;
        assetFileDescriptor.getFileDescriptor();       
                 
        mp = new MediaPlayer();
        try {
           mp.setDataSource(assetFileDescriptor);
           mp.prepare();
           mp.start();
        }  catch(Exception e) { }


The line "mp.setDataSource(assetFileDescriptor);" gives the following error-message:
"The method setDataSource(String) in the type MediaPlayer is not applicable for the arguments (AssetFileDescriptor)"

This despite the fact that I have chosen "setDataSource(FileDescription fd)" when automatically suggested by the ide.
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Postby mad.dogg » Sun Aug 23, 2009 1:26 am

Your code with my edit and comments :)
Code: Select all
        AssetManager assetManager = null;   // null ???  Get the AssetManager here.
        AssetFileDescriptor assetFileDescriptor = null;
        try{
           assetFileDescriptor = assetManager.openFd("file:///android_asset/ahora.mp3");
                FileDescriptor fd = assetFileDescriptor.getFileDescriptor();
                mp = new MediaPlayer();
           mp.setDataSource(fd); // mp.setDataSource(assetFileDescriptor); You can not pass AssetFileDescriptor.
           mp.prepare();
           mp.start();
        } catch (Exception e) {}     

So once again the steps are:
1) get the manager.
2) get the AssetFileDescriptor
3) get a FileDescriptor from the AssetFileDescriptor
4) init the player
5) call setDataSource(FileDescriptor);
6) play
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby Danneman » Sun Aug 23, 2009 3:49 am

I keep thinking that knowing C# will be sufficient, but especially when working with the android api thats not sufficient :P So thanks a lot for you input, youve really clariefied a couple of things.

The specific problem, however, is still unresolved it seems - no sound :?

I only get one error from the Error Log, though:
1) MediaPlayer: Unable to create media player

An improvement, I guess, since these errors have disappeared:
2) PlayerDriver: Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
3) MediaPlayer: error (1, -4)


At the moment I have resorted to an emergency-solution consisting of making a function for each sound in my res/raw-folder. Yeah, EXHAUSTING!

Code: Select all

    public class JS_SoundPlayer
    { 
       
            public void mySound1() {
                mp = new MediaPlayer();
                mp = MediaPlayer.create(getBaseContext(), R.raw.mySound1);
                mp.start();
            }       
// etc
            public void mySound100() {
                mp = new MediaPlayer();
                mp = MediaPlayer.create(getBaseContext(), R.raw.mySound100);
                mp.start();
            }   


But I guess in the end I will have spent as much time figuring this problem out as I would have doing it this way, so :lol:
Danneman
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 26, 2009 12:42 pm

Postby darkante » Tue Sep 01, 2009 2:45 pm

You can always get a handle to the AssetManager through your Activity or Context.

AssetManager am = context.getAssets();

For example...
Ante.NET
darkante
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Sep 01, 2009 2:43 pm

Postby japandroid » Sat Dec 19, 2009 1:24 pm

I have a question regarding the first post posted by Danneman.

He said
Code: Select all
mp = MediaPlayer.create(getBaseContext(), R.raw.mySound);

worked but

Code: Select all
String soundName = "mySound";
mp = MediaPlayer.create(getBaseContext(), Uri.parse("R.raw." + soundName));

didn't work.

Well, That's exactly what I'm trying to do right now.

If i have mySound.mp3 file.
The following works
Code: Select all
mp = MediaPlayer.create(getBaseContext(), R.raw.mySound));

But since I have file names in sequence like
mySound1.mp3
mySound2.mp3
mySound3.mp3
mySound4.mp3

so I want to make the file names using for loop.
Can anyone please help me out?

Thanks for your help in advance.
japandroid
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Dec 19, 2009 1:17 pm

Postby marcjc » Wed Feb 10, 2010 5:27 pm

Did anyone figure this one out? I am looking for the same thing..

Thanks.
marcjc
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Jan 05, 2010 3:16 pm

Postby Sir^Knigget » Sun Feb 21, 2010 1:44 pm

Me too, please help :?
Sir^Knigget
Freshman
Freshman
 
Posts: 9
Joined: Wed Sep 30, 2009 6:00 pm

Postby pskink » Sun Feb 21, 2010 4:02 pm

Sir^Knigget wrote:Me too, please help :?


Resources#getIdentifier is your friend

more info here:

viewtopic.php?p=24260
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: Playing local sound with MediaPlayer?

Postby raito_yagami » Mon Sep 06, 2010 6:26 pm

if the files are in the assets,you could:

AssetFileDescriptor assetFileDescriptor = getAssets().openFd("TroubleIsaFriend.mp3");//directly file name
FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();
mediaPlayer.reset();
mediaPlayer.setDataSource(path);

I learned above and tested successed

I'm sorry for my pool English.
raito_yagami
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 5:48 pm

Top
Next

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 17 guests