从设备内部存储播放音频文件。它不断在 void prepare() 上抛出 IOException
Playing audio files from device internal storage. It keeps throwing IOException on void prepare()
我想从用户设备播放存储在我的应用程序内部存储器中的音频文件:下面是提取和显示这些音频文件列表的完整代码=
//variables of retrieving audio file from internal storage
ListView listView_phone_calls;
ArrayList<File> phone_call_list;
ArrayAdapter<File> adapter;
//variables of playing an audio file
MediaPlayer player;
listView_phone_calls=(ListView)findViewById(R.id.listView_phone_calls);
phone_call_list=new ArrayList<>();
File[] file=getApplicationContext().getFilesDir().listFiles();
for (int i=0;i<file.length;i++){
phone_call_list.add(file[i]);
}
adapter=new ArrayAdapter<>(PhoneCall_Player.this,android.R.layout.simple_list_item_activated_1,phone_call_list);
listView_phone_calls.setAdapter(adapter);
listView_phone_calls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is what should happen when a user clicks on a one item
player=new MediaPlayer();
File call_rec=(File) listView_phone_calls.getItemAtPosition(position);
try {
player.setDataSource(String.valueOf(call_rec));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! File not found! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}catch (IllegalArgumentException e){
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! Something is wrong!, "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Player couldn't be prepared to play! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
player=null;
}
});
}
});
每当我点击一个项目时,播放器无法准备播放的吐司一直出现;我觉得setDataSource();没有问题那么我的代码有什么问题。有没有我没有看到的错误?还是我调用MediaPlayer不好?
在你的路径前加上 file://
这样你的路径就变成了 file:///data/user/0/com.example.mypackagename/files/audio00001.mp3
我想从用户设备播放存储在我的应用程序内部存储器中的音频文件:下面是提取和显示这些音频文件列表的完整代码=
//variables of retrieving audio file from internal storage
ListView listView_phone_calls;
ArrayList<File> phone_call_list;
ArrayAdapter<File> adapter;
//variables of playing an audio file
MediaPlayer player;
listView_phone_calls=(ListView)findViewById(R.id.listView_phone_calls);
phone_call_list=new ArrayList<>();
File[] file=getApplicationContext().getFilesDir().listFiles();
for (int i=0;i<file.length;i++){
phone_call_list.add(file[i]);
}
adapter=new ArrayAdapter<>(PhoneCall_Player.this,android.R.layout.simple_list_item_activated_1,phone_call_list);
listView_phone_calls.setAdapter(adapter);
listView_phone_calls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is what should happen when a user clicks on a one item
player=new MediaPlayer();
File call_rec=(File) listView_phone_calls.getItemAtPosition(position);
try {
player.setDataSource(String.valueOf(call_rec));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! File not found! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}catch (IllegalArgumentException e){
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! Something is wrong!, "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Player couldn't be prepared to play! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
player=null;
}
});
}
});
每当我点击一个项目时,播放器无法准备播放的吐司一直出现;我觉得setDataSource();没有问题那么我的代码有什么问题。有没有我没有看到的错误?还是我调用MediaPlayer不好?
在你的路径前加上 file://
这样你的路径就变成了 file:///data/user/0/com.example.mypackagename/files/audio00001.mp3