Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:39:24+00:00 2026-06-04T14:39:24+00:00

Can someone point me in the right direction with this? I need to get

  • 0

Can someone point me in the right direction with this? I need to get a audio file of random requests. I thought I could do this by using the AssetManager and then use string arry for a random length position. I am getting Error at OnClickMethod. Here is the code with with LogCat.

public class RandomSoundFormAssetsFolder extends Activity implements
    OnClickListener {
/** Called when the activity is first created. */
MediaPlayer RandomAudioPlayer;
AssetFileDescriptor descriptor;
Button playSound;
String[] filelist;
AssetManager Agetlist;
String MediaFileL;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        Initialization();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void Initialization() throws IOException {
    // TODO Auto-generated method stub
    setContentView(R.layout.main);
    playSound = (Button) findViewById(R.id.myButton);
    playSound.setOnClickListener(this);
    RandomAudioPlayer = new MediaPlayer();
    Agetlist = this.getAssets();
            filelist = Agetlist.list("");

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.myButton:
        OnClickMethod();
        break;

    default:
        break;
    }

}

public class AudioRanActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
MediaPlayer RandomAudioPlayer;
AssetFileDescriptor descriptor;
Button playSound;
String[] filelist;
AssetManager Agetlist;
String MediaFileL;
int randomInt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Initialization();

}

private void Initialization() {
    // TODO Auto-generated method stub
    setContentView(R.layout.main);
    playSound = (Button) findViewById(R.id.button1);
    playSound.setOnClickListener(this);
    RandomAudioPlayer = new MediaPlayer();
    Log.d("In Initialization Method", "Constructor For mp");

    Agetlist = this.getAssets();
    try {
        filelist = Agetlist.list("audio");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        OnClickMethod();
        break;

    default:
        break;
    }

}

private void OnClickMethod() {
    // TODO Auto-generated method stub
    Random randomGenerator = new Random();
    randomInt = randomGenerator.nextInt(filelist.length);
    Log.i("Length: ", String.valueOf(filelist.length));
    Log.i("RanInt: ", String.valueOf(randomInt));

    try {
        MediaFileL = filelist[randomInt];
        Log.i("LengtD: ", MediaFileL);

        descriptor = getAssets().openFd(MediaFileL);
        RandomAudioPlayer.setDataSource(descriptor.getFileDescriptor(),
                descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        RandomAudioPlayer.prepare();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        RandomAudioPlayer.start();
        Log.d("In OnClickMethod", "Start mp");
    }
}

}

Crash log:

05-26 17:52:51.191: W/dalvikvm(1982): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-26 17:52:51.282: E/AndroidRuntime(1982): FATAL EXCEPTION: main
05-26 17:52:51.282: E/AndroidRuntime(1982): java.lang.NullPointerException
05-26 17:52:51.282: E/AndroidRuntime(1982):     at sound.stuff.RandomSoundFormAssetsFolder.OnClickMethod(RandomSoundFormAssetsFolder.java:68)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at sound.stuff.RandomSoundFormAssetsFolder.onClick(RandomSoundFormAssetsFolder.java:51)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.view.View.performClick(View.java:3511)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.view.View$PerformClick.run(View.java:14105)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.os.Handler.handleCallback(Handler.java:605)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.os.Looper.loop(Looper.java:137)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at java.lang.reflect.Method.invoke(Method.java:511)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-26 17:52:51.282: E/AndroidRuntime(1982):     at dalvik.system.NativeStart.main(Native Method)

Crash log:

05-27 11:50:48.378: W/System.err(2512): java.io.FileNotFoundException: sounds
05-27 11:50:48.418: W/System.err(2512):     at android.content.res.AssetManager.openAssetFd(Native Method)
05-27 11:50:48.418: W/System.err(2512):     at android.content.res.AssetManager.openFd(AssetManager.java:331)
05-27 11:50:48.438: W/System.err(2512):     at sound.stuff.RandomSoundFormAssetsFolder.OnClickMethod(RandomSoundFormAssetsFolder.java:69)
05-27 11:50:48.438: W/System.err(2512):     at sound.stuff.RandomSoundFormAssetsFolder.onClick(RandomSoundFormAssetsFolder.java:53)
05-27 11:50:48.468: W/System.err(2512):     at android.view.View.performClick(View.java:3511)
05-27 11:50:48.468: W/System.err(2512):     at android.view.View.onKeyUp(View.java:6073)
05-27 11:50:48.468: W/System.err(2512):     at android.widget.TextView.onKeyUp(TextView.java:5532)
05-27 11:50:48.488: W/System.err(2512):     at android.view.KeyEvent.dispatch(KeyEvent.java:2575)
05-27 11:50:48.488: W/System.err(2512):     at android.view.View.dispatchKeyEvent(View.java:5500)
05-27 11:50:48.498: W/System.err(2512):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-27 11:50:48.498: W/System.err(2512):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-27 11:50:48.498: W/System.err(2512):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-27 11:50:48.508: W/System.err(2512):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-27 11:50:48.518: W/System.err(2512):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
05-27 11:50:48.518: W/System.err(2512):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-27 11:50:48.518: W/System.err(2512):     at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-27 11:50:48.530: W/System.err(2512):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-27 11:50:48.538: W/System.err(2512):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
05-27 11:50:48.538: W/System.err(2512):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
05-27 11:50:48.558: W/System.err(2512):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
05-27 11:50:48.558: W/System.err(2512):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 11:50:48.568: W/System.err(2512):     at android.os.Looper.loop(Looper.java:137)
05-27 11:50:48.568: W/System.err(2512):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-27 11:50:48.578: W/System.err(2512):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:50:48.578: W/System.err(2512):     at java.lang.reflect.Method.invoke(Method.java:511)
05-27 11:50:48.588: W/System.err(2512):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-27 11:50:48.600: W/System.err(2512):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-27 11:50:48.608: W/System.err(2512):     at dalvik.system.NativeStart.main(Native Method)
05-27 11:50:48.608: E/MediaPlayer(2512): start called in state 1
05-27 11:50:48.608: E/MediaPlayer(2512): error (-38, 0)
05-27 11:50:48.648: E/MediaPlayer(2512): Error (-38,0)

I have been working on trying to debug this. I have be altering my code and put in some Log-Method. String array is working I get length consistently 14 and I get random position. So that is all working. Her is the problem in the log ‘FileNotFoundException’ so the file is not loading but when I Log MediaFileL I get a audio-file name. I’m using MediaPlayer wrong or AssetFileDescriptor? Here is the code with with LogCat.

public class AudioRanActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
MediaPlayer RandomAudioPlayer;
AssetFileDescriptor descriptor;
Button playSound;
String[] filelist;
AssetManager Agetlist;
String MediaFileL;
int randomInt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Initialization();

}

private void Initialization() {
    // TODO Auto-generated method stub
    setContentView(R.layout.main);
    playSound = (Button) findViewById(R.id.button1);
    playSound.setOnClickListener(this);

    RandomAudioPlayer = new MediaPlayer();
    Log.d("In Initialization method", "RandomAudioPlayer");

    Agetlist = this.getAssets();
    Log.d("TAGAgetlist", "Agetlist");
    try {
        filelist = Agetlist.list("audio");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        OnClickMethod();
        break;

    default:
        break;
    }

}

private void OnClickMethod() {
    // TODO Auto-generated method stub
    Random randomGenerator = new Random();
    randomInt = randomGenerator.nextInt(filelist.length);
    Log.i("Lengt: ", String.valueOf(filelist.length));
    Log.i("RanInt: ", String.valueOf(randomInt));

    try {
        MediaFileL = filelist[randomInt];
        Log.i("LengtD: ", MediaFileL);

        descriptor = getAssets().openFd(MediaFileL);
        RandomAudioPlayer.setDataSource(descriptor.getFileDescriptor(),
                descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        RandomAudioPlayer.prepare();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        RandomAudioPlayer.start();
        Log.d("RanPlaystart", "RandomAudio");
    }
    Initialization();
}

}

Log:

05-28 10:12:31.175: D/In Initialization Method(792): Constructor For mp
05-28 10:13:07.606: I/Lengt:(792): 14
05-28 10:13:07.606: I/RanInt:(792): 3
05-28 10:13:07.634: I/LengtD:(792): good_job.mp3
05-28 10:13:07.655: W/System.err(792): java.io.FileNotFoundException: good_job.mp3
05-28 10:13:07.664: W/System.err(792):  at  android.content.res.AssetManager.openAssetFd(Native Method)
05-28 10:13:07.664: W/System.err(792):  at android.content.res.AssetManager.openFd(AssetManager.java:331)
05-28 10:13:07.675: W/System.err(792):  at Audio.stuff.AudioRanActivity.OnClickMethod(AudioRanActivity.java:75)
05-28 10:13:07.675: W/System.err(792):  at Audio.stuff.AudioRanActivity.onClick(AudioRanActivity.java:55)
05-28 10:13:07.684: W/System.err(792):  at android.view.View.performClick(View.java:3511)
05-28 10:13:07.684: W/System.err(792):  at android.view.View$PerformClick.run(View.java:14105)
05-28 10:13:07.684: W/System.err(792):  at android.os.Handler.handleCallback(Handler.java:605)
05-28 10:13:07.695: W/System.err(792):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 10:13:07.704: W/System.err(792):  at android.os.Looper.loop(Looper.java:137)
05-28 10:13:07.716: W/System.err(792):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-28 10:13:07.716: W/System.err(792):  at java.lang.reflect.Method.invokeNative(Native Method)
05-28 10:13:07.716: W/System.err(792):  at java.lang.reflect.Method.invoke(Method.java:511)
05-28 10:13:07.724: W/System.err(792):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-28 10:13:07.724: W/System.err(792):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-28 10:13:07.734: W/System.err(792):  at dalvik.system.NativeStart.main(Native Method)
05-28 10:13:07.734: E/MediaPlayer(792): start called in state 1
05-28 10:13:07.734: E/MediaPlayer(792): error (-38, 0)
05-28 10:13:07.745: D/In OnClickMethod(792): Start mp
05-28 10:13:07.754: E/MediaPlayer(792): Error (-38,0)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:39:26+00:00Added an answer on June 4, 2026 at 2:39 pm

    use

    RandomAudioPlayer = new MediaPlayer();
     Agetlist = this.getAssets();
    filelist = Agetlist.list("");
    

    because your String[] filelist; is empty.so first get all files list in filelist from assets then use in your OnClickMethod().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone point me in the right direction of some information on using: <%#
Hoping someone can point me in the right direction with this as i'm not
If some one can point me in the right direction for this code for
Hoping someone can point me in the right direction. I'm building a online shop
i hope someone can point me in the right direction On a existing html
I'm curious if someone can point me in the right direction here. I'm learning
I'm a Linq noobie, maybe someone can point me in the right direction. What's
I was hoping someone can point me in the right direction. I am looking
Just wondering if someone can point me in the right direction. I want to
Pretty new to Powershell and hoping someone can point me in the right direction.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.