java 如何在android中播放来自内部和外部SD卡的mp3文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12005566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:14:05  来源:igfitidea点击:

How to play mp3 files from internal and external SD card in android?

javaandroidandroid-sdcard

提问by Adarsh H S

I am working on a mp3 player app, which plays .mp3 files present anywhere inside an internal SD card.

我正在开发一个 mp3 播放器应用程序,它播放存在于内部 SD 卡内任何位置的 .mp3 文件。

I have used the following codes to fetch the .mp3 files present in internal storage.

我使用以下代码来获取内存中存在的 .mp3 文件。

ArrayList<File> inFiles = new ArrayList<File>();
File list[] = file.listFiles();
//Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++) 
{
    // myList.add( list[i].getName() );
    File temp_file = new File(file.getAbsolutePath(),list[i].getName());
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
    if (temp_file.listFiles() != null) 
    {
        //Log.i("inside", "call fn");
        listfiles(temp_file);

    }
    else 
    {
        if (list[i].getName().toLowerCase().contains(".mp3"))
        {
            inFiles.add(list[i]);
        //Log.e("Music", list[i].getName());
        }
    }
}

How do I similarly get the .mp3 files from external SD card as well?

我如何同样从外部 SD 卡获取 .mp3 文件?

采纳答案by Mohit Deshpande

Trying using this:

尝试使用这个:

File root = Environment.getExternalStorageDirectory();

That will get you the root of the external storage, provided that there is one. Then you can filter out files that aren't .mp3

这将使您获得外部存储的根目录,前提是有一个。然后你可以过滤掉不是 .mp3 的文件

Also consider looking at this: List all of one file type on Android device?

还可以考虑查看:列出 Android 设备上的所有一种文件类型?

回答by Pratik

You can get the root directory of the external sdcard using this code of line

您可以使用此行代码获取外部sdcard的根目录

File root = Environment.getExternalStorageDirectory();

Now you can do the same thing as you do for internal

现在你可以做和内部一样的事情

ArrayList<File> inFiles = new ArrayList<File>();
File list[] = root.listFiles(); // here use the root object of File class to the list of files and directory from the external storage
//Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++) 
{
    // myList.add( list[i].getName() );
    File temp_file = new File(file.getAbsolutePath(),list[i].getName());
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
    if (temp_file.listFiles() != null) 
    {
        //Log.i("inside", "call fn");
        listfiles(temp_file);

    }
    else 
    {
        if (list[i].getName().toLowerCase().contains(".mp3"))
        {
            inFiles.add(list[i]);
        //Log.e("Music", list[i].getName());
        }
    }
}

回答by Tushar Gupta

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

    enter code here

import android.os.Environment;

public class SongsManager {


    final String MEDIA_PATH = Environment.getExternalStorageDirectory()``
            .getPath() + "/";
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String mp3Pattern = ".mp3";
    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
         System.out.println(MEDIA_PATH);
            if (MEDIA_PATH != null) {
                File home = new File(MEDIA_PATH);
                File[] listFiles = home.listFiles();
                if (listFiles != null && listFiles.length > 0) {
                    for (File file : listFiles) {
                        System.out.println(file.getAbsolutePath());
                        if (file.isDirectory()) {
                            scanDirectory(file);
                        } else {
                            addSongToList(file);
                        }
                    }
                }
            }
            // return songs list array
            return songsList;
        }

        private void scanDirectory(File directory) {
            if (directory != null) {
                File[] listFiles = directory.listFiles();
                if (listFiles != null && listFiles.length > 0) {
                    for (File file : listFiles) {
                        if (file.isDirectory()) {
                            scanDirectory(file);
                        } else {
                            addSongToList(file);
                        }

                    }
                }
            }
        }

        private void addSongToList(File song) {
            if (song.getName().endsWith(mp3Pattern)) {
                HashMap<String, String> songMap = new HashMap<String, String>();
                songMap.put("songTitle",
                        song.getName().substring(0, (song.getName().length() - 4)));
                songMap.put("songPath", song.getPath());

                // Adding each song to SongList
                songsList.add(songMap);
            }
        }
    /**
     * 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"));
        }
    }
}

回答by Andy Res

To get a reference to a file located on: sdcard/music/song.mp3:

要获取对位于以下位置的文件的引用:sdcard/music/song.mp3:

File file = Environment.getExternalStorageDirectory()+"/music/song.mp3";

回答by ankush dubey

A slight Modification to Tushar Gupta's Codes. This worked fine for me. I have complete code for a Music Player

对 Tushar Gupta 的代码稍作修改。这对我来说很好。我有音乐播放器的完整代码

import android.os.Environment;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {


        final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
        private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        private String mp3Pattern = ".mp3";
        // Constructor
        public SongsManager(){

        }

        /**
         * Function to read all mp3 files from sdcard
         * and store the details in ArrayList
         * */
        public ArrayList<HashMap<String, String>> getPlayList(){
            System.out.println(MEDIA_PATH);
            if (MEDIA_PATH != null) {
                File home = new File(MEDIA_PATH);
                File[] listFiles = home.listFiles();
                if (listFiles != null && listFiles.length > 0) {
                    for (File file : listFiles) {
                        System.out.println(file.getAbsolutePath());
                        if (file.isDirectory()) {
                            scanDirectory(file);
                        } else {
                            addSongToList(file);
                        }
                    }
                }
            }
            // return songs list array
            return songsList;
        }

        private void scanDirectory(File directory) {
            if (directory != null) {
                File[] listFiles = directory.listFiles();
                if (listFiles != null && listFiles.length > 0) {
                    for (File file : listFiles) {
                        if (file.isDirectory()) {
                            scanDirectory(file);
                        } else {
                            addSongToList(file);
                        }

                    }
                }
            }
        }

        private void addSongToList(File song) {
            if (song.getName().endsWith(mp3Pattern)) {
                HashMap<String, String> songMap = new HashMap<String, String>();
                songMap.put("songTitle",
                        song.getName().substring(0, (song.getName().length() - 4)));
                songMap.put("songPath", song.getPath());

                // Adding each song to SongList
                songsList.add(songMap);
            }
        }
        /**
         * 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"));
            }
        }
    }