Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从资产文件夹中读取音乐_Java_Android - Fatal编程技术网

Java 从资产文件夹中读取音乐

Java 从资产文件夹中读取音乐,java,android,Java,Android,我的项目是用户可以设置为铃声的音乐列表。 我的所有音乐都位于raw中,它工作正常,而且我的铃声名称是raw“zeallist”中的文本。 我的问题是如何将我的音乐放入资产文件夹 以下是我播放原始音乐的代码: public ArrayList<SongInfo> getAllSong(Context context) { ArrayList<SongInfo> listSong = new ArrayList<SongInfo>(); Rin

我的项目是用户可以设置为铃声的音乐列表。 我的所有音乐都位于raw中,它工作正常,而且我的铃声名称是raw“zeallist”中的文本。 我的问题是如何将我的音乐放入资产文件夹

以下是我播放原始音乐的代码:

  public ArrayList<SongInfo> getAllSong(Context context) {
    ArrayList<SongInfo> listSong = new ArrayList<SongInfo>();
    RingtonesSharedPreferences pref = new RingtonesSharedPreferences(
            context);
    Field[] fields = R.raw.class.getFields();
    for (int i = 0; i < fields.length - 1; i++) {
        SongInfo info = new SongInfo();
        try {
            String name = fields[i].getName();           
            if (!name.equals("ringtones")) {   
                info.setFileName(name + ".mp3");
                info.setFavorite(pref.getString(info.getFileName()));
                int audioResource = R.raw.class.getField(name).getInt(name);
                info.setAudioResource(audioResource);
            }
            // info.setName(name);
        } catch (Exception e) {

        }
        listSong.add(info);
    }
    InputStream inputStream = context.getResources().openRawResource(
            R.raw.zeallist);
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream));   
    try {
        String line;
        int i = 0;
        while ((line = reader.readLine()) != null) {
            listSong.get(i).setName(line);
            i++;
        }
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return listSong;
}
public ArrayList getAllSong(上下文){
ArrayList listSong=新的ArrayList();
RingtoneSharedPreferences pref=新的RingtoneSharedPreferences(
上下文);
Field[]fields=R.raw.class.getFields();
对于(int i=0;i

如何更改我的这部分代码以从资产中读取它们并返回我的listsong?

右键单击res文件夹并创建名为raw的新文件夹。现在复制并粘贴几个.MP3文件。检查这些链接以便更好地理解

来自Assest文件夹

public void playBeep() {
    try {
        if (mp.isPlaying()) {
            mp.stop();
            mp.release();
            mp = new MediaPlayer();
        }

        AssetFileDescriptor descriptor = getAssets().openFd("mysong.mp3");
        mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();

        mp.prepare();
        mp.setVolume(1f, 1f);
        mp.setLooping(true);
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

您可以使用
AssetManager

AssetManager assetManager = getAssets();
String[] files = assetManager.list("");

请注意,此文件是字符串数组。所以,在迭代之前,不要忘记为数组的每个元素初始化新文件

核心部分我已经完成了。一些细节你需要自己做

  public ArrayList<SongInfo> getAllSong(Context context) throws IOException {
        ArrayList<SongInfo> listSong = new ArrayList<SongInfo>();
        RingtonesSharedPreferences pref = new RingtonesSharedPreferences(context);
            String[] files = context.getAssets().list("Your songs path");
            for (int i = 0; i < files.length - 1; i++) {
                SongInfo info = new SongInfo();
                    String name = files[i];
                    if (!name.equals("ringtones")) {
                        info.setFileName(name + ".mp3");
                        info.setFavorite(pref.getString(info.getFileName()));
                    /*    int audioResource = R.raw.class.getField(name).getInt(name);
                        info.setAudioResource(audioResource);*/  //fileName is enough to you
                    }
                    // info.setName(name);
                listSong.add(info);
            }
            InputStream inputStream = context.getAssets().open("Your zeallist path");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream));
            try {
                String line;
                int i = 0;
                while ((line = reader.readLine()) != null) {
                    listSong.get(i).setName(line);
                    i++;
                }
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return listSong;
    }

希望这能解决您的问题。

请您看一下我的代码,我确实需要media player,我必须更改代码的这一部分。Field[]fields=R.raw.class.getFields();和int audioResource=R.raw.class.getField(名称).getInt(名称);也许这个链接会很有帮助:[查看这个链接]()现在我可以正确地从资产中读取我的音乐,但是当我想播放时,我从这一行得到了错误。AssetFileDescriptor AssetFileDescriptor=context.getAssets().openFd(musicName);我从程序中复制的那一行,在我的程序中起作用。因此,我需要知道该行导致的错误日志。但是我找不到您发布的日志。请检查您传递的上下文是否为null。如果context.getAssets().list(“您的歌曲路径”)起作用,那么context.getAssets().openFd(musicName)抛出NullPointerException就毫无意义。或者您传递了错误的musicname。检查音乐的路径和名称是否正确我检查了代码,发现我的问题在这一行,这里我需要一个int来播放我的音乐。这一行-->int audioResource=R.raw.class.getField(name).getInt(name);info.setAudioResource(audioResource);以原始形式保存的任意文件。要使用原始InputStream打开这些资源,请使用资源ID(R.raw.filename)调用resources.openRawResource()。但是,如果需要访问原始文件名和文件层次结构,则可以考虑在资产/目录中保存一些资源(而不是RES/WRAW/)。assets/中的文件没有资源ID,因此只能使用AssetManager读取它们。如果要将音乐文件放入资源中,必须使用AssetManager打开它们。
  public void play(MediaPlayer mediaPlayer, Context context, String musicName) throws IOException {
        AssetFileDescriptor assetFileDescriptor = context.getAssets().openFd(musicName);
        mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                assetFileDescriptor.getStartOffset(),
                assetFileDescriptor.getLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
    }