1

有人可以指出我正确的方向吗?我需要获取随机请求的音频文件。我想我可以通过使用 AssetManager 来做到这一点,然后使用字符串数组作为随机长度的位置。我在 OnClickMethod 处遇到错误。这是带有 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");
    }
}

}

崩溃日志:

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)

崩溃日志:

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)

我一直在努力尝试调试这个。我一直在更改我的代码并放入一些 Log-Method。字符串数组正在工作,我的长度始终为 14,并且我得到随机位置。所以这一切都有效。她是日志“FileNotFoundException”中的问题,所以文件没有加载,但是当我记录 MediaFileL 时,我得到了一个音频文件名。我使用错了 MediaPlayer 还是 AssetFileDescriptor?这是带有 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();
}

}

日志:

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)
4

3 回答 3

1

这是一个清晰而通用的方法:

private static String getRandomMediaFilePath(Context context, String assetSpecificFolderPath)
{
    AssetManager assetManager;
    String[] fileList;
    String mediaFileName;
    String mediaFilePath;

    try
    {
        assetManager = context.getAssets();
        fileList = assetManager.list(assetSpecificFolderPath);
        mediaFileName = fileList[getRandomNumber(0,fileList.length - 1)];
        mediaFilePath = assetSpecificFolderPath + "/" + mediaFileName;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return mediaFilePath;
}


private static int getRandomNumber(int min, int max)
{
    // Usually this should be a field rather than a method variable so
    // that it is not re-seeded every call.
    Random random = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = random.nextInt((max - min) + 1) + min;

    return randomNum;
}

用法:

getRandomMediaFilePath(context, "MediaResources/Sounds")

虽然资产文件夹包含文件夹“MediaResources/Sounds”

于 2014-05-11T09:15:11.890 回答
0

采用

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

因为你的 String[] 文件列表;是空的。所以首先从资产中获取文件列表中的所有文件列表,然后在您的 OnClickMethod() 中使用。

于 2012-05-27T03:04:57.433 回答
0

对于那些可能有帮助的人。它是这样工作的。我需要它来添加我的子文件夹字符串“audio/”+RandomAudio

这是我的错误:

descriptor = getAssets().openFd("audio/" + MediaFileL);



public class AudioRanActivity extends Activity implements OnClickListener {
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() {
    setContentView(R.layout.main);
    // Geting audio-files
    Agetlist = this.getAssets();
    try {
        // Constructs string-arry from assets/audio folder
        filelist = Agetlist.list("audio");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    playSound = (Button) findViewById(R.id.button1);
    playSound.setOnClickListener(this);

    // MediaPlayer Constructor
    RandomAudioPlayer = new MediaPlayer();
    Log.d("In OnClickMethod Method TagLog", "Constructor For mp");

}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        OnClickMethod();
        break;

    default:
        break;
    }

}

private void OnClickMethod() {
    // Random-factory
    Random randomGenerator = new Random();

    // Gets int file-list length
    randomInt = randomGenerator.nextInt(filelist.length);
    Log.i("Length Log: ", String.valueOf(filelist.length));
    Log.i("RanInt Log: ", String.valueOf(randomInt));
    try {

        // One Audio-file putIn string
        // InputRaw = openFileInput(name)
        MediaFileL = filelist[randomInt];
        Log.i("What file is it? Log: ", MediaFileL);

        // AssetFileDescriptor open audio-file
        descriptor = getAssets().openFd("audio/" + MediaFileL);
        /*
         * Parameters fd: the FileDescriptor for the file you want to play
         * offset: the offset into the file where the data to be played
         * starts, in bytes length: the length in bytes of the data to be
         * played
         */
        RandomAudioPlayer.setDataSource(descriptor.getFileDescriptor(),
                descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        RandomAudioPlayer.prepare();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    RandomAudioPlayer.start();
    Log.d("In OnClickMethod TagLog", "Start mp");
}

}

于 2012-05-28T17:51:11.430 回答