我有一个列表视图,显示文件夹/diarioscolastico
这是我的代码
public class ActivityRegistrazione extends ActionBarActivity {
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
//Used for audio listview
private List<String> myList;
File file;
ArrayAdapter<String> adapter;
//Variabile usata per usare un solo bottone come start/stop
boolean mStartRecording = true;
private void onRecord(boolean start) {
if (start) {
//Creo un file per ogni registrazione che viene fatta partire
File mFolder = new File(Environment.getExternalStorageDirectory() + "/diarioscolastico");
mFileName = Environment.getExternalStorageDirectory() + "/diarioscolastico";
boolean success = true;
if (!mFolder.exists()) {
success = mFolder.mkdir();
}
if (success) {
// Create an audio file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String audioFileName = "AUDIO_" + timeStamp + "_.3gp";
// Do something on success
mFileName += "/"+ audioFileName ;
} else {
// Do something else on failure
}
startRecording();
} else {
stopRecording();
//Aggiorno la listview con la nuova immagine
//Lista con il nuovo elemento
List<String> myNewList = new ArrayList<>();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/diarioscolastico" );
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myNewList.add(0,list[i].getName());
}
//Aggiorno la Listview dell'activity con il nuovo inserimento
myList.clear();
myList.addAll(myNewList);
adapter.notifyDataSetChanged();
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_registrazione);
final Button recButton = (Button) findViewById(R.id.recButton);
recButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
onRecord(mStartRecording);
if (mStartRecording) {
recButton.setText("Stop");
} else {
recButton.setText("Rec");
}
mStartRecording = !mStartRecording;
}
});
ListView audioListView = (ListView) findViewById(R.id.audioList);
myList = new ArrayList<>();
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/diarioscolastico" );
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add(0, list[i].getName());
}
audioListView.setAdapter(adapter); //Set all the file in the list.
// ListView Item Click Listener
audioListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO play the touched audio file in listview
}
});
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_registrazione, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的问题是,当我点击列表视图中的音频文件时,我不知道如何启动某些媒体播放器来收听音频。 我找到了一些holdoverflow帖子,但是它们发布在2010年。
我也找到了这个代码:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
但是如何从listview音频文件名中取出uri?
有更好的方法可以做到这一点吗? 我可以使用一些意图来使用标准的audioplayer吗?
答案 0 :(得分:0)
您可以使用您知道的外部存储的文件路径,然后从中获取URI,从而使用它来启动设备的标准音频播放器
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("your file path");
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
由于