如何拾取音频文件';Android中设备的路径或uri?
我正在创建一个简单的应用程序,我想在其中播放声音。按下应用程序上的“播放”按钮时,它将播放声音。 对于播放我正在使用的声音:如何拾取音频文件';Android中设备的路径或uri?,android,audio,Android,Audio,我正在创建一个简单的应用程序,我想在其中播放声音。按下应用程序上的“播放”按钮时,它将播放声音。 对于播放我正在使用的声音: MediaPlayer mediaPlayer; mediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Over
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Completed",
Toast.LENGTH_LONG).show();
}
});
mediaPlayer.start();
而且效果很好
但现在我希望用户能够从设备中选择一个音频文件,这样当他或她按下播放按钮时,所选的声音就会被播放
我没有得到任何像图像可以挑选的意图行动
Intent photo_picker_intent = new Intent(Intent.ACTION_PICK);
photo_picker_intent.setType("image/*");
startActivityForResult(photo_picker_intent, PHOTOS_FROM_GALLERY);
在上面的代码中,用户只需进入gallery即可。在点击任何图像后,他或她将获得一个图像URI,可用于在应用程序中显示图像。我想做同样的事情,除了音频文件选择。只要使用这两行
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Gallery"), reqCode);
或者
你需要选择图像和音频吗?
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode);