简体   繁体   English

android从sdcard播放音乐

[英]android playing music from the sdcard

I need to play some music from a posisition on the sdcard how can I do that? 我需要在sdcard上播放一些音乐,该怎么办? I know I need to use the MediaPlayer class but if im traying to due like this 我知道我需要使用MediaPlayer类,但是如果我由于这种原因而托盘

             try {

        mpSong.setDataSource(musicposisition);

        mpSong.prepare();

        mpSong.start();

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

then my app creash the varibale called musicposisition = /sdcard/musictelefon/03 Mare.mp3 when im logging it pleas help!! 然后我的应用程序在即时日志记录中添加了名为musicposisition = /sdcard/musictelefon/03 Mare.mp3当我将其记录musicposisition = /sdcard/musictelefon/03 Mare.mp3时,请帮忙!!

Can some ond tell me how I can warp code in also??? 有人可以告诉我如何修改代码吗???

don't realy know if this helps some one but here is my logcat output https://docs.google.com/document/d/14k1lqxmdCKxsmwfDUD6za5s7W48lAHOjDfbUHzfgh-s/edit?hl=en_US 真的不知道这是否对某些人有帮助,但这是我的logcat输出https://docs.google.com/document/d/14k1lqxmdCKxsmwfDUD6za5s7W48lAHOjDfbUHzfgh-s/edit?hl=zh_CN

you can also code like this if you are fetching a particular extension file .You can use the below code. 如果您要提取特定的扩展名文件,也可以这样编写代码。可以使用以下代码。 Right now I am only fetching the .mp3 file from the sdcard. 现在,我只从sdcard提取.mp3文件。

final String MEDIA_PATH = new String("/sdcard/");
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();    
    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"));
        }
    }

Your logcat output tells you that you have a NullPointerException: Caused by: java.lang.NullPointerException 08-30 22:25:38.463: ERROR/AndroidRuntime(6107): at simon.programmering.music.MusicActivity.onCreate(MusicActivity.java:73) 您的logcat输出告诉您您有一个NullPointerException:原因:java.lang.NullPointerException 08-30 22:25:38.463:ERROR / AndroidRuntime(6107):位于simon.programmering.music.MusicActivity.onCreate(MusicActivity.java: 73)

Check MusicActivity.java on line 73. What's on there? 在第73行上检查MusicActivity.java。那里有什么?

是的,所以我在android-dev irc的帮助下发现了问题,我想将mpSong声明为on create metod中的媒体播放器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM