2017-04-21 52 views
0

我使用listview显示我的记录音频,但是当我记录一个新的 我想刷新列表视图。 但它不工作。 我没有经验惠特安卓工作室呢,对不起,如果我不beeing 具体 你能帮助我吗?更新我的列表视图惠普新记录器音频

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(); 

       } 
      }); 

      //recorder 
      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; 

     } 

回答

0
如果你希望你的新记录在列表视图刷新

然后u有记录的音频/视频后运行这些代码块每次的。 while(fs.getfetchstatus()!= true){my_shares = 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", ""); 

所以将此代码复制并粘贴在私人无效stopRecording(){}在结束.. 可能会工作...

+0

它的作品! 感谢您的帮助! –