在DIRECTORY_MUSIC中播放一首歌

时间:2013-09-21 09:25:58

标签: android

我使用以下文件将文件保存到音乐目录中:

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "SpaceJam.mp3");

在我的主要活动中,我现在想要访问它并播放它。

如何检索歌曲的信息,以便我可以将其用于:

mp.setDataSource(??);

“mp”是我的MediaPlayer的名称。感谢。

3 个答案:

答案 0 :(得分:4)

    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_MUSIC);
     File file = new File(path, "Hunny.mp3");
    try {
        mp.setDataSource(file.toString());
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException 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();
    }

答案 1 :(得分:0)

如果你有外部存储

,那就是这样的
 File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_MUSIC);
 File file = new File(path, "SpaceJam.mp3");

....

答案 2 :(得分:0)

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File file = new File(path, "SpaceJam.mp3");
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(file.getAbsolutePath());
mp.prepare();
mp.start();

编辑:

mp.setDataSource(file.getAbsolutePath());