I am making an audio recording app and my MediaRecorder instance starts to run from a service, so that it even runs in the background, now I want to implement a timer to mark and save the recorded time, but how to activate the timer in my Service class and display the timer counter in my activity? Or do you have a better way to start the timer? I opted not to let running from the activity pq in the activity it would not mark the time if the app is in the background.
below is my code: my service class
package com.example.administrador.myapplication;
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import java.io.IOException;
/**
* Created by Administrador on 27/06/2017.
*/
public class TesteService extends Service{
MediaRecorder mediaRecorder;
PathFile pathFile;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
pathFile = new PathFile();
String file = pathFile.generateFileName();
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/"+file+".aac");
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onDestroy() {
if(mediaRecorder != null){
mediaRecorder.stop();
mediaRecorder.release();
}
super.onDestroy();
}
public void onComplete(){
if(mediaRecorder != null){
mediaRecorder.stop();
mediaRecorder.release();
}
}
}
and I want to display a timer in the layout of this my fragment:
package com.example.administrador.myapplication.fragments;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.administrador.myapplication.MainActivity;
import com.example.administrador.myapplication.R;
import com.example.administrador.myapplication.TesteService;
import java.io.IOException;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
MediaPlayer mediaPlayer;
Button start,stop,play,stopPlay;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
pathFile = new PathFile();
String file = pathFile.generateFileName();
start = (Button) rootView.findViewById(R.id.start);
stop = (Button) rootView.findViewById(R.id.stop);
play = (Button) rootView.findViewById(R.id.play);
stopPlay = (Button) rootView.findViewById(R.id.stopPlay);
Toast.makeText(getActivity(), Environment.getExternalStorageDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),TesteService.class);
getActivity().startService(intent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Recording Complete", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(),TesteService.class);
getActivity().stopService(intent);
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/"+file+".aac");
mediaPlayer.prepare();
}catch (IOException e){
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(getActivity(), "Reproduzindo Musica", Toast.LENGTH_SHORT).show();
}
});
stopPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
return rootView;
}
}
I know that to add a timer just insert it in layout.xml and in my fragment create an instance of it, associate with findViewById (); and give the start and stop. but I wanted the timer to remain in the background, so I thought about letting my service take care of it, but how can I insert it in the view of the fragment from the service?