【问题标题】:How can I use music in raw folder in this music player?如何在此音乐播放器的原始文件夹中使用音乐?
【发布时间】:2014-04-23 03:26:07
【问题描述】:

我有这段代码可以将 sdcard 中的音乐添加到我的音乐播放器中:

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/sdcard/");
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
}

但我想在我的音乐播放器的 raw 文件夹中使用音乐而不是 sdcard 中的音乐....我该怎么办?

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个..

    String path = "android.resource://" + getPackageName() + "/" + R.raw.yourfile;
    

    【讨论】:

      【解决方案2】:

      请按照以下步骤实现:-

      第 1 步:-

      在 res 文件夹中创建一个 raw 文件夹,并将音频文件放在该文件夹中。

      第 2 步:-

      在你的activity类中,以这种方式从raw文件夹中读取,

      Field[] fields = R.raw.class.getFields();
      

      // 仅当原始文件夹中有多个文件时,否则只需这样做

      fields.getName()

      获取字符串文件。

         for(int count=0; count < fields.length; count++){                
                  int rid = fields[count].getInt(fields[count]);
                  String filename = fields[count].getName();
                  flist.add(filename);
              }
      

      第 3 步:-

      现在您有了文件名,您可以在音频播放器中使用它。

      String xyz = "android.resource://"+getPackageName()+"/raw/"+(file name);
      

      希望这会有所帮助。

      【讨论】:

      • 我看不懂你的代码........我应该在哪一行添加你的代码???
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多