Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将音频录制为Android服务并使用计时器停止。录制长度不同,服务不受限制';我似乎没有停止_Java_Android_Android Service - Fatal编程技术网

Java 将音频录制为Android服务并使用计时器停止。录制长度不同,服务不受限制';我似乎没有停止

Java 将音频录制为Android服务并使用计时器停止。录制长度不同,服务不受限制';我似乎没有停止,java,android,android-service,Java,Android,Android Service,我最近(几天前)开始使用JAVA,所以我很确定我犯了一些基本的错误 我创建了一个活动,通过一个按钮启动一个录制音频的服务。在其中,我添加了一个计时器,用于停止录制并通过stopself()退出服务。希望这是关闭它的正确方式 但是,当我运行应用程序时,它会在录制期间生成多个音频文件(我将其设置为90分钟),并且它们会以不合理的方式被截断(其中一些是30分钟,一些是20分钟,等等)。而且服务有时似乎不会停止 我的服务级别: @SuppressLint("SimpleDateFormat") pub

我最近(几天前)开始使用JAVA,所以我很确定我犯了一些基本的错误

我创建了一个活动,通过一个按钮启动一个录制音频的服务。在其中,我添加了一个计时器,用于停止录制并通过stopself()退出服务。希望这是关闭它的正确方式

但是,当我运行应用程序时,它会在录制期间生成多个音频文件(我将其设置为90分钟),并且它们会以不合理的方式被截断(其中一些是30分钟,一些是20分钟,等等)。而且服务有时似乎不会停止

我的服务级别:

@SuppressLint("SimpleDateFormat")
 public class myservice extends Service {
     private MediaRecorder myRecorder;
       private String outputFile = null;
        public IBinder onBind(Intent arg0) {
            return null;
        }
        @Override
        public void onCreate() {
           // super.onCreate();  (not sure about this one)

            SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
            String format = s.format(new Date());

            outputFile = Environment.getExternalStorageDirectory().
                  getAbsolutePath() +  "/" + format + ".3gpp";

            myRecorder = new MediaRecorder();
            myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myRecorder.setOutputFile(outputFile);

               try {
                  myRecorder.prepare();
                  myRecorder.start();
               } catch (IllegalStateException e) {
                  // start:it is called before prepare()
                  // prepare: it is called after start() or before setOutputFormat() 
                  e.printStackTrace();
               } catch (IOException e) {
                   // prepare() fails
                   e.printStackTrace();
                }

            Timer myTimer;

            myTimer = new Timer();
            myTimer.schedule(new TimerTask() {          
                @Override
                public void run() {

        try {
                          myRecorder.stop();
                          myRecorder.reset();
                          myRecorder.release();
                          myRecorder = null;

                          stopSelf();
                          //onDestroy();  I added this one to test it out. not sure.

                       } catch (IllegalStateException e) {
                            //  it is called before start()
                            e.printStackTrace();
                       } catch (RuntimeException e) {
                            // no valid audio/video data has been received
                            e.printStackTrace();
                       }
                    }

            }, 60000 * 90); //90 minutes timer to stop the recording after 90 minutes

            }

        public int onStartCommand(Intent intent, int flags, int startId) {
            //onCreate();  
           // return 1;
            return START_STICKY; //not sure about this one
        }

        public void onStart(Intent intent, int startId) {
            // TO DO
        }
        public IBinder onUnBind(Intent arg0) {
            // TO DO Auto-generated method
            return null;
        }

        public void onStop() {

        }
        public void onPause() {

        }
//what about this one ?
       // @Override
       // public void onDestroy() {
//  myRecorder.stop();
//  myRecorder.reset();
//  myRecorder.release();
      //}

    }
我的主要活动:

public class MainActivity extends Activity {

       private Button startBtn;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);


      startBtn = (Button)findViewById(R.id.start);
      startBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent svc=new Intent(getBaseContext(), myservice.class);
            startService(svc);


            finish(); //because I want to close the UI after service started
        }
      });

   }

}
在使用android服务之前,我使用线程编写了应用程序。它工作了,但计时器只工作了很短的时间(10分钟或更短)。音频录制在某个时刻停止(通常为30分钟或更短),并且该长度不是固定的(尽管计时器设置为90分钟)。我仍然不知道为什么,但我认为Android操作系统会因为缺乏资源而关闭线程


非常感谢您的帮助

既然我解决了这个问题,如果有人面临类似的问题,我就发布我的解决方案。显然,Android操作系统也可以关闭服务(不活动[无交互]约30分钟)。我需要的是前台服务。官方文件样本:

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);