简体   繁体   English

在Android中播放sdcard0发出的声音

[英]Play a sound from sdcard0 in android

MediaPlayer mp;
mp = MediaPlayer.create(this, R.drawable.test);
mp.start();

That's my code. 那是我的代码。 It just plays a sound from res/drawable/ folder. 它只是从res / drawable /文件夹播放声音。 I need to play a sound from android's sdcard0/Test Folder/testsound.amr How to do this ? 我需要从android的sdcard0 / Test Folder / testsound.amr播放声音。

Please Help. 请帮忙。 Thanks in advance. 提前致谢。

Just use the setDataSource Method: 只需使用setDataSource方法:

MediaPlayer mp = new MediaPlayer();
//set the source of the Sound...
mp.setDataSource("/mnt/sdcard/Test Folder/testsound.amr");
//load the file
mp.prepare(); 
//Do the thing (:
mp.start();

You can also do it dynamically by doing: 您还可以通过以下方式动态地执行此操作:

mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Test Folder/testsound.amr");

NOTE: I'm not clearly sure if you need to do .getPath() or not... 注意:我不确定您是否需要执行.getPath()

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

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