1

我正在开发一个 GPS 应用程序,用户可以在其中点击屏幕上的任何位置来录制音频片段,然后在记录时附加到轨道上。我正在使用媒体记录器在我的应用程序中执行此操作,而不是发出隐含的意图。这样,用户可以“无视”操作,如下所示。我编写了两种模式:(1)用户点击和应用程序记录一个简短的、固定的(10 秒)片段;(2) 用户点击启动媒体记录器,然后再次点击停止。

我在 Overlay.onTap 中观察水龙头,并在其自己的线程中运行媒体记录器。代码如下。

这是我的问题,在模式(2)下:第一次点击后,媒体记录器启动正常,但后续点击不会触发 onTap。事实上,在 60 秒的录制过程中,UI 线程似乎完全冻结了。

顺便说一句,我最初在 Activity 中编写了媒体记录器的内容;然后我将其移至后台线程以尝试解决冻结问题,但无济于事。

覆盖代码如下:

/**
* DKR: When User has tapped on a media icon,
* The MapView calls us here, giving us the GeoPoint
* (lat and lon) where User tapped. 
*/
@Override
public boolean onTap(GeoPoint tappedGeoPoint, MapView mapview)
{
  return commonOnTap(tappedGeoPoint);
}
//boolean isRecording = false;
 boolean oneTapAudio = false;
 boolean twoTapAudio = true;
 boolean firstTap = false;

 /**
 * Method AsyncOverlay.onTap calls commonOnTap and sends us the GeoPoint of the tap.
  * Send the tapped GeoPoint to each segment object, to see which one has
* the corresponding media waypoint, if any.
 */
 public boolean commonOnTap(GeoPoint tappedGeoPoint)
{
   boolean isLogging = GPSLoggerService.isLogging();
   if ( isLogging )
   {
      if ( oneTapAudio )  // Override so that tap takes User to audio recorder @ InsertNote.addQuickAudio
      {                   // Possibly run addQuickAudio in background thread for greater reliability.
         Intent intent = new Intent(mLoggerMap.getActivity(), InsertNote.class);
         String s = GPStracking.AUTHORITY+"/Request";
         intent.putExtra(s, InsertNote.ONETAPAUDIO);
         mLoggerMap.getActivity().startActivity(intent);
         return true;
         //mLoggerMap.invalidate();
         //InsertNote insert = new InsertNote();
         //insert.addQuickAudio();
      }
      if ( twoTapAudio )
      {
         if ( !firstTap )
         {
            firstTap = true;
            Intent intent = new Intent(mLoggerMap.getActivity(), InsertNote.class);
            String s = GPStracking.AUTHORITY+"/Request";
            intent.putExtra(s, InsertNote.TWOTAPONAUDIO);
            mLoggerMap.getActivity().startActivity(intent);
         }
         else
         {
            firstTap = false;
            Intent intent = new Intent(mLoggerMap.getActivity(), InsertNote.class);
            String s = GPStracking.AUTHORITY+"/Request";
            intent.putExtra(s, InsertNote.TWOTAPOFFAUDIO);
            mLoggerMap.getActivity().startActivity(intent);
         }
         return true;
      }
   }

  boolean handled = false;
  for (SegmentRendering segment : mSegmentRenderersList)
  {
     if (!handled)
     {
        handled = segment.commonOnTap(tappedGeoPoint);
     }
  }
  return handled;
}

活动代码如下:

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  this.setVisible(false);
  paused = false;
  isRecording = false;
  mLoggerServiceManager = new GPSLoggerServiceManager(this);

  final Semaphore audioSemaphore = new Semaphore(0);
  mRecordAudioThread = new Thread("audioRecorder")
  {

     @Override
     public void run()
     {
        Looper.prepare();             // Initialize the current thread as a looper.
        mAudioThreadHandler = new Handler();     // Create an Handler to communicate with the Looper.
        audioSemaphore.release(); // the only release in the whole program
        Looper.loop();                // Run the message queue in this thread.
     }
  };
  mRecordAudioThread.start();
  try
  {
     audioSemaphore.acquire();
  }
  catch (InterruptedException e)
  {
     Log.e(TAG, "Failed waiting for a semaphore", e);
  }
 }

@Override
protected void onResume()
{
  int request = 0;
  super.onResume();
  String s = GPStracking.AUTHORITY+"/Request";
  Bundle extras = getIntent().getExtras();
  if (extras != null) request = extras.getInt(s);      
  if ( request  == ONETAPAUDIO || request  == TWOTAPONAUDIO || request  == TWOTAPOFFAUDIO )
  {
     QuickAudioLogic( request );
     return;
  }

  if (mServiceBindAction == null)
  {
     mServiceBindAction = new Runnable()
     {
        @Override
        public void run()
        {
           showDialog(DIALOG_INSERTNOTE);
        }
     };
  }
  ;
  mLoggerServiceManager.startup(this, mServiceBindAction);
}

@Override
protected void onPause()
{
  super.onPause();
  mLoggerServiceManager.shutdown(this);
  paused = true;
  if (mRecorder != null) {
     mRecorder.release();
     mRecorder = null;
 }
}

public void QuickAudioLogic(int request)
{
  mRecorder = new MediaRecorder();
  if ( request == ONETAPAUDIO )  
  {
     if ( !isRecording )
     {
        isRecording = true;
        mDuration = ONETAPDURATION;
        mAudioThreadHandler.post(recordAudioAsync);
        //            QuickAudioAction ( ONETAPDURATION );
     }
     return;
  }
  if ( request == TWOTAPONAUDIO )
  {
     if ( !isRecording )
     {
        isRecording = true;
        mDuration = TWOTAPDURATION;
        mAudioThreadHandler.post(recordAudioAsync);
        //            QuickAudioAction ( TWOTAPDURATION );
     }
     return;
  }
  if ( request == TWOTAPOFFAUDIO )
  {
     if ( isRecording )
     {
        isRecording = false;
        Toast.makeText(this, "Stop recording audio",Toast.LENGTH_SHORT).show();
        mRecorder.reset();
        mRecorder.release();
        mRecorder = null;
        finish();
     }
  }
}

背景(媒体记录器)线程如下:

public Runnable recordAudioAsync = new Runnable()
{

  @Override
  public void run()
  {
     String newName;
     Calendar c = Calendar.getInstance();
     newName = String.format( "Audio" + getString( R.string.dialog_filename_default)+".3gpp", c, c, c, c, c, c );
     //newName = String.format("Audio_%tY-%tm-%td_%tH%tM%tS.3gpp", c, c, c, c, c, c);
     File newFile = new File(Constants.getSdCardDirectory(InsertNote.this) + newName);
     newName = newFile.toString();
     android.net.Uri.Builder builder = new Uri.Builder();;
     sUri = builder.scheme("file").appendEncodedPath("/"+newName).build();
     //sUri = Uri.parse(newName);  // store Uri static, so that   
     try {
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(newName);
        mRecorder.setMaxDuration( mDuration );//mDuration = 10000 for one tap or 60000 for two tap mode
        mRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener(){ 
           @Override 
           public void onInfo(MediaRecorder mr, int what, int extra) { 
                 if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED ) 
                 {
                    InsertNote.this.mLoggerServiceManager.storeMediaUri(sUri);
                    //setResult(MENU_VOICE, new Intent());
                    //mLoggerMap.invalidate();
                    isRecording = false;
                    Toast.makeText(InsertNote.this, "Timeout recording audio",Toast.LENGTH_SHORT).show();
                    mRecorder.reset();
                    mRecorder.release();
                    finish();
                    //startActivity(new Intent(this.getApplicationContext(), ANOTHERACTIVITY.class)); 
                 } 
              } 
           });
        mRecorder.prepare();
        isRecording = true;
        Toast.makeText(InsertNote.this, "Start recording audio",Toast.LENGTH_LONG).show();
        mRecorder.start();
        //((Button)(findViewById(R.id.record_button))).setText("stop");
     } 
     catch (Exception e) {
        Toast.makeText(InsertNote.this, "Error starting audio recorder.",Toast.LENGTH_SHORT).show();
     }
     return;
  }
};
4

1 回答 1

0

UI 组件只能从 UI 线程修改。删除 toast 消息或尝试将您的线程作为 UI 线程运行。

public void run(){
activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
                    // Things to do
                 }
              });
             }
于 2013-03-25T19:24:04.530 回答