简体   繁体   中英

android playing music from the sdcard

I need to play some music from a posisition on the sdcard how can I do that? I know I need to use the MediaPlayer class but if im traying to due like this

             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!!

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

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.

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)

Check MusicActivity.java on line 73. What's on there?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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