使用片段I尝试从原始文件夹中获取音频文件,如下所示:
mediaPlayer = MediaPlayer.create(this, getResources().getIdentifier(audioFile, "raw", getPackageName()));
我收到Cannot resolve method 'getPackageName()'
错误。怎么解决这个问题?
答案 0 :(得分:1)
我能够通过编写这段代码来解决它:
String path = "android.resource://"+"com.example.myproject"+"/raw/"+audioFile;
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getActivity(), Uri.parse(path));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}