0

有一个文本和音频(录音)并将名称和路径保存在数据库中。同时单击音频必须播放的名称也可以播放。但是在编辑时如果我应该单独更改名称,它不会采用旧文件名各自的,它使那个为空。

如果人们不更新音频(录音),如何取旧的音频文件名

音频活动.java

private void saveState() {
      String audioname = et1.getText().toString();

      String audiofilename = gfilename;
      String audiocount = et2.getText().toString();

        if(audiocount.equals("")){
            audiocount ="1"; 
        }


      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

        String audiodate = sdf.format(new Date());


      if (mRowId == null || mRowId.longValue() == 0)
      {

        long id = mDbHelper.createProject4(audioname, audiofilename, audiocount, audiodate);
          if (id > 0) {
              mRowId = id;
          }
      } else {
          audiofilename=gfilename;
          mDbHelper.updateProject4(mRowId, audioname, audiofilename, audiocount,audiodate);
      }


  }


  public View.OnClickListener btnClick = new View.OnClickListener() 
  {
    @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btnStart:{
                    AppLog.logString("Start Recording");

                    enableButtons(true);
                    startRecording();

                    break;
                }
                case R.id.btnstop:{
                    AppLog.logString("Start Recording");

                    enableButtons(false);
                    stopRecording();

                    break;

                }

            }
        }

    };

    public MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            AppLog.logString("Error: " + what + ", " + extra);
        }
    };

    public MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
        @Override
        public void onInfo(MediaRecorder mr, int what, int extra) {
            AppLog.logString("Warning: " + what + ", " + extra);
        }
    };


    public void setButtonHandlers() {
        ((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick);
      ((Button)findViewById(R.id.btnstop)).setOnClickListener(btnClick);

    }

    public void enableButton(int id,boolean isEnable){
        ((Button)findViewById(id)).setEnabled(isEnable);
    }

    public void enableButtons(boolean isRecording) {
        enableButton(R.id.btnStart,!isRecording);           
        enableButton(R.id.btnstop,isRecording);
    }


    @SuppressLint("NewApi")
    private void startRecording(){
        //displayFormatDialog();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String formats[] = {"MPEG 4", "3GPP", "AMR"};

        builder.setTitle(getString(R.string.choose_format_title))
               .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    currentFormat = which;


                    dialog.dismiss();





        recorder = new MediaRecorder();         
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        recorder.setOutputFormat(output_formats[currentFormat]);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(getFilename());          
        //recorder.setOnErrorListener(errorListener);
        //recorder.setOnInfoListener(infoListener);

        try {
            recorder.prepare();
            recorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
                   }
               })
               .show();
    }



private void stopRecording(){
    if(null != recorder)
    {
        //mDbHelper.updateProject4FileName(mRowId, gfilename); 
        recorder.stop();
        recorder.reset();
        recorder.release();             
        recorder = null;
        }
    else
    {
        recorder.stop();
        recorder.release();     
    }
}


    public String getFilename(){

        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);

        if(!file.exists()){
            file.mkdirs();
        }
        gfilename = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
        return (gfilename);
    }


    @Override
    public void onCompletion (MediaPlayer arg0) 
    {               
    }

  public void  playSong(String gfilename){
      // Play song
         try
         {
          mp.reset();
          mp.setDataSource(gfilename);
          mp.prepare();
          mp.start();

          // Changing Button Image to pause image
          btnPlay.setImageResource(R.drawable.btn_pause);


      } catch (IllegalArgumentException e) {
          e.printStackTrace();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

我还检查了调试,如果我们没有更新记录,它只会将该位置作为空值。

在这里,我还附上了我的数据库更新代码

public boolean updateProject4(long _id, String audioname, String audiofilename,String audiocount,String audiodate) {
                            ContentValues args = new ContentValues();
                            args.put(CATEGORY_COLUMN_AUDIONAME, audioname );
                            args.put(CATEGORY_COLUMN_AUDIOFILENAME, audiofilename );
                            args.put(CATEGORY_COLUMN_AUDIOCOUNT, audiocount );
                            args.put(CATEGORY_COLUMN_AUDIODATE, audiodate );
                            return mDb.update(DATABASE_TABLE_AUDIOPRAYER, args, CATEGORY_COLUMN_ID4 + "=" + _id,  null) > 0;
                        }
4

1 回答 1

0

其实我有办法回答我的问题。

如果我的文件名在更新时变为空,想从 db 中获取文件

private void saveState() {
      String audioname = et1.getText().toString();

      String audiofilename = gfilename;
      String audiocount = et2.getText().toString();

        if(audiocount.equals("")){
            audiocount ="1"; 
        }






      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

        String audiodate = sdf.format(new Date());

      //String reqname= spin.getSelectedItem().toString();
      //Log.i(" save state mathod "," values are "+title+Desc+Body+reqname); 

      if (mRowId == null || mRowId.longValue() == 0)
      {

        long id = mDbHelper.createProject4(audioname, audiofilename, audiocount, audiodate);
          if (id > 0) {
              mRowId = id;
          }
      } else {


          if(audiofilename.equals("")){
              Cursor filename = mDbHelper.fetchProject4FileName(mRowId, audiofilename);
               startManagingCursor(filename);
               gfilename =filename.getString(filename.getColumnIndexOrThrow(GinfyDbAdapter.CATEGORY_COLUMN_AUDIOFILENAME));
            //mDbHelper.fetchProject4FileName(mRowId, audiofilename);
            audiofilename = gfilename;
            mDbHelper.updateProject4(mRowId, audioname, audiofilename, audiocount,audiodate);
        }
          else
          {
              audiofilename =  gfilename;
              mDbHelper.updateProject4(mRowId, audioname, audiofilename, audiocount,audiodate);
          }




      }

我们必须从 db 中获取文件名并检查我们的音频文件名是否为空意味着我们必须设置旧文件名

于 2013-09-13T11:54:42.000 回答