+ Reply to Thread
Results 1 to 6 of 6

Thread: What is the best way to play short samples repeating

  1. #1
    Join Date
    May 2009
    Location
    Kingdom of Bavaria
    Posts
    3

    Default What is the best way to play short samples repeating


    hello,

    i want to play a very short click sample, when counting up the score, so lets say player has scored 50000 points, i want to count up from 0 to 50000 in maybe 3 seconds and on every count, i want to play a short sample

    the sample comes from a resource id, so i initialize it with

    Code:
    MediaPlayer p=MediaPlayer.create(mContext, R.raw.sample);
    when i now use p.start(), it plays one but never again.

    api docs say i should use reset() and setDataSource(), but there is no setDataSource for resource id's.

    now i tried something like

    Code:
    class MyClass {
      MediaPlayer p=null;
    
      void playSound() {
        if (p!=null) p.reset();
        p=MediaPlayer.create(mContext, R.raw.sample);
        p.start();
      }
    }
    but it slows everything down

    so how can i achieve this?

  2. #2
    Join Date
    Sep 2008
    Location
    The Boonies of Pinal County, Arizona
    Posts
    1,187

    Default Re: What is the best way to play short samples repeating

    Quote Originally Posted by 93interactive View Post
    ...
    now i tried something like

    Code:
    class MyClass {
      MediaPlayer p=null;
    
      void playSound() {
        if (p!=null) p.reset();
         p=MediaPlayer.create(mContext, R.raw.sample);
        p.start();
      }
    }
    but it slows everything down

    so how can i achieve this?
    Why are you reinitializing "p" after calling .reset() on it? I'd imagine that creating the MediaPlayer object is expensive, so if you just want to play the same sound again, try setLooping(true) or seekTo(0).
    I blame
    To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
    for most programming problems.

  3. #3
    Join Date
    May 2009
    Location
    Kingdom of Bavaria
    Posts
    3

    Default Re: What is the best way to play short samples repeating

    thanx for answer

    after calling reset() you need to call setDataSource() again, which does not support a resource id (which i think is a problem of the api, if there is a create() with resource id, there should be a setDataSource(Context context, int resid) )

    setLooping works for the special case of counting up the scores, but i also want to play a sound on single actions, so it is no option for that.

    seekAt(0) would be exactly what i need, but no matter what i do, after using seekAt(0), the sound does not play anymore.

  4. #4
    Join Date
    Sep 2008
    Location
    The Boonies of Pinal County, Arizona
    Posts
    1,187

    Default Re: What is the best way to play short samples repeating

    Looks like you'll have to .release() your previous MediaPlayer before instantiating a new one until Google fixes the API.

    You may also want to try Context's getResources().getResourceName(int resId). If getResourceName() gives you the proper name back, you could try using that value in MediaPlayer's setDataSource().
    I blame
    To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
    for most programming problems.

  5. #5
    Join Date
    May 2009
    Location
    Kingdom of Bavaria
    Posts
    3

    Default Re: What is the best way to play short samples repeating

    here is the best solution i came up with:

    Code:
    class MyClass ... {
        AsyncPlayer aPlayer=new AsyncPlayer("aPlayer");
    
        public void doStuff() {
          aPlayer.play(mContext, Uri.parse("android.resource://"+PACKAGE_NAME+"/"+R.raw.soundfile), false, AudioManager.STREAM_SYSTEM);
       }
    }
    whereas PACKAGE_NAME is what ever is specified in AndroidManifest.xml

  6. #6
    Join Date
    Sep 2008
    Location
    The Boonies of Pinal County, Arizona
    Posts
    1,187

    Default Re: What is the best way to play short samples repeating


    I'd file the lack of a resId in .play() as a bug on http://b.android.com.
    I blame
    To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
    for most programming problems.

+ Reply to Thread

Similar Threads

  1. Repeating Calendar Notification
    By realityhack in forum New Application Ideas
    Replies: 17
    Last Post: 01-17-2011, 11:25 AM
  2. Replies: 1
    Last Post: 01-18-2009, 01:43 PM
  3. Short Cuts
    By tego10122 in forum General Android Chat
    Replies: 2
    Last Post: 01-05-2009, 02:41 AM
  4. Call Log Short Cut
    By Tonester in forum Applications
    Replies: 9
    Last Post: 10-30-2008, 07:04 PM
  5. Replies: 0
    Last Post: 09-30-2008, 02:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts