0
im trying to refresh my list of Songs when a record an audio, but is not Working, could you help me?
public class MainActivity extends AppCompatActivity {
private Button mRecordBtn;
private Button mAtualizarBtn;
private TextView mRecordLabel;
private MediaRecorder mRecorder;
private String mFileName = null;
private static final String LOG_TAG = "Record_log";
//private StorageReference mStorage;
private ProgressDialog mProgress;
ListView lv;
String[] items;
FetchSongs fs;
ArrayList<File> mySongs;
ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
lv = (ListView) findViewById(R.id.listView);
fs = new FetchSongs();
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait, Fetching Songs...");
dialog.setCancelable(true);
dialog.show();
while (fs.getfetchstatus() != true) {
mySongs = fs.findSongs(Environment.getExternalStorageDirectory());
}
if (mySongs != null) {
dialog.dismiss();
}
mySongs = fs.getsonglist();
items = new String[mySongs.size()];
for (int i = 0; i < mySongs.size(); i++) {
items[i] = mySongs.get(i).getName().toString().replace(".3gp", "");
}
final ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
lv.setAdapter(adp);
adp.notifyDataSetChanged();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Player.class);
intent.putExtra("pos", i);
adp.notifyDataSetChanged();
startActivity(intent);
finish();
}
});
mAtualizarBtn = (Button) findViewById(R.id.atualizarBtn);
mRecordLabel = (TextView) findViewById(R.id.recordLabel);
mRecordBtn = (Button) findViewById(R.id.recordBtn);
mProgress = new ProgressDialog(this);
//mFileName = getExternalCacheDir().getAbsolutePath();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording-"+ts+".mp3";
adp.notifyDataSetChanged();
lv.setAdapter(adp);
adp.notifyDataSetChanged();
//mFileName += "/record_audio.MP3";
mRecordBtn.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
startRecording();
mRecordLabel.setText("GRAVANDO");
adp.notifyDataSetChanged();
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
mRecordLabel.setText("Feche o app para " +
"atualizar a lista");
lv.setAdapter(adp);
adp.notifyDataSetChanged();
}
return false;
}
});
}
private void startRecording()
{
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording-"+ts+".mp3";
try
{
mRecorder.prepare();
} catch (IOException e)
{
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording()
{
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}