Java 更新(编辑)时,不使用旧的音频文件名

Java 更新(编辑)时,不使用旧的音频文件名,java,android,media-player,Java,Android,Media Player,有一个文本和音频(录制)并将名称和路径保存在db中。单击名称时,音频也必须播放。但在编辑时,如果我想单独更改名称,它不会使用相应文件的旧文件名,它会将该文件名设为空 如果人们不更新音频(录音),如何使用旧的音频文件名 audioactivity.java private void saveState() { String audioname = et1.getText().toString(); String audiofilename = gfilename;

有一个文本和音频(录制)并将名称和路径保存在db中。单击名称时,音频也必须播放。但在编辑时,如果我想单独更改名称,它不会使用相应文件的旧文件名,它会将该文件名设为空

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

audioactivity.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();
      }
  }
我还检查了调试,如果我们不更新记录,那么它只会作为null出现

这里我还附加了我的db更新代码

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;
                        }

事实上我找到了解决问题的方法

如果我的文件名在更新时变为空,是否希望从数据库中获取文件

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




      }

我们必须从数据库中获取文件名,并检查audiofilename是否为空。这意味着,我们必须设置较旧的文件名

继续接受您的答案。这将帮助那些无法接受你回答的人。但您可以单击勾选一次,将您的答案标记为已接受