I am trying to make a music streaming app. For the music I use a URL, and for some reason, the music is playing for 3 seconds and then automatically stops. pls help me fix it.
I also get a message in the Run says: "MediaPlayer finalized without being released".
Thanks.
the code:
String url = "https://radio.streamgates.net/stream/1036kh"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
try {
mediaPlayer.setDataSource(url);
} catch (IOException e) {
Toast.makeText(this,"failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try {
mediaPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
Toast.makeText(this,"failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
mediaPlayer.start();
Related
After recording an outgoing phone call, I am trying to play the recorded file - to make sure the call recording worked as expected (I am doing it using 'MediaPlayer'), but there is no sound.
So I tried to access the actual file on the phone (simply attached the phone to the computer and accessed it's files). When I played it the recording it was in the right length but again no sound.
What am I missing?
This is how I record the phone call:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File callAudioFile = null;
try {
File downloadsDir = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
callAudioFile = File.createTempFile("deTuCelRecord", ".amr", downloadsDir);
} catch (IOException e) {
e.printStackTrace();
}
assert callAudioFile != null;
audioFilePath = callAudioFile.getAbsolutePath();
recorder.setOutputFile(audioFilePath);
try {
recorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
#Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e("MediaRecorder", "MediaRecorder error " + what + " " + extra);
}
});
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
This is the code which ends the call recording:
recorder.stop();
recorder.release();
This is how I play the audio file:
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(audioFilePath);
mPlayer.prepare();
Toast.makeText(getApplicationContext(), "PLAYING AUDIO", Toast.LENGTH_LONG).show();
mPlayer.start();
Log.d("PLAY_AUDIO", "Started playing audio");
} catch (IOException e) {
Log.e("PLAY_AUDIO", "Failed to play audio");
}
Please check this Accessibilty Service in your testing phone.
If you are trying to record call on Android Q.
Please refer this link
You can try
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); It need Manifest.permission.CAPTURE_AUDIO_OUTPUT permission.
Please check AudioSource Source documentation for difference between VOICE_CALL and VOICE_COMMUNICATION
The issue was the android version - from android 10 it didn't allow me to record the call but on android 9 it did.
I am making a Call recorder app in Android. I have a service that detects the incoming and outgoing call and as soon as the service detects the incoming or outgoing call it starts the recording through media player. It is getting recorded sucessfully but I am unable to play it on speaker. The volume is very low even after enabling speakers through Audio Manager. Please let me know where I might be going wrong.
Note: I am guessing that my audio is not getting recorded properly because if I play the recorded file through my phone music player, then even there I am not able to play that file on speaker.
below is my code which is used to start the recording:
try{
MediaRecorder mediaRecorder = new MediaRecorder();
if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_COMMUNICATION"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_UPLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_DOWNLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("MIC"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("DEFAULT"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
}
else
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(getTempFilename());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start();
Below is the Media Playback Code :
final MediaPlayer mp=new MediaPlayer();
String uri= getIntent().getStringExtra("fileURI"); //fetches the string defining the path of file to be played
Uri fileURI = Uri.parse(uri);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
try{
try {
mp.setDataSource(getApplicationContext(), fileURI);
} catch (IOException e) {
e.printStackTrace();
}
}
mp.prepare();
}catch(Exception e){e.printStackTrace();}
mp.start();
The problem I am having at the moment is that my pause is not always working.
What I have is an MediaPlayer in main activity that is operated via ActionBarSherlock and onClick listeners. MediaPlayer is using ArrayList with URLs of MP3 files(some of them 1sec long).
Pause code:
if (player.isPlaying()) {
if (player != null) {
player.pause();
swapPlayIcon(1);
isPaused = true;
pauseMenuButt.setVisible(false);
playMenuButt.setVisible(true);
}
}
swapPlayIcon(int) handles only visibility and drawable swaps.
Start code:
Iterator<Uri> iterUri = tracks.iterator();
while (iterUri.hasNext()) {
Uri tmpUri = iterUri.next();
try {
player.reset();
player.setDataSource(String.valueOf(tmpUri));
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
EDIT
After more testing I have found out that the problem appear in "in between" state.
What I mean is that when my MP3 file is 2sec long and I click pause its not stopping becouse it just have ended reading one file and now moved on to the next one.
I have added:
} else {
pauseLocked = true;
}
to pause if statment and it does not land in it at all while testing.
So im not sure about the "in between" problem that I have found out previously.
Your pause button is not working because there's no way for its message to ever reach the MediaPlayer until you've started the last URL. You are entirely blocking the thread your MediaPlayer is running on (I'm assuming it's the UI thread since the MediaPlayer is in your main activity). If you're creating your MediaPlayer on the same thread as your UI, you should use the asynchronous version of prepare: prepareAsync. You need to respond once you receive the onMediaPrepared callback and then start the media. Once that happens you must do nothing and wait for the media to finish, only then should you load another url.
I am trying to make camera application which takes 3 continuous shots.
i have tried to call "takePicture" several times by putting it in a loop.
but no success.
please help on this matter.
a little help will be appreciated.
You never should call PictureCallback.onPictureTaken() from your code; this callback receives data from the system when it is ready, as response to Camera.takePicture().
The latter call will only succeed if the camera is opened and preview is started. Therefore, simply calling Camera.takePicture() in a loop will not work (see e.g. Android 2.3.1 Camera takePicture() Multiple images with one button click). The correct way to handle this is to keep a counter of shots processed in your onPictureTaken(), and if it is less than 3, then restart camera preview and issue (synchroneously) another Camera.takePicture(). After this, onPictureTaken() should return, to allow processing of the next captured frame.
I use it like this when doing a PhotoBurst. It is also handling the FRameLayout holding the preview to start the PhotoBurst:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
Parameters param = camera.getParameters();
param.setPictureSize(640, 480);
camera.setParameters(param);
// Or write to sdcard
outStream = new FileOutputStream(String.format(
Environment.getExternalStorageDirectory().getPath()+"/foto%d.jpg",
System.currentTimeMillis()));
outStream.write(data);
outStream.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.fromFile(Environment.getExternalStorageDirectory())));
Log.i(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpg");
try {
stillCount++;
camera.startPreview();
if (stillCount < 10) {
preview.mCamera.takePicture(shutterCallback, rawCallback,
jpegCallback);
if (stillCount == 9) {
frameLayout.setClickable(true);
}
} else {
stillCount = 0;
takePictureButton.setEnabled(true);
frameLayout.setClickable(true);
}
} catch (Exception e) {
Log.d(TAG, "Error starting preview: " + e.toString());
}
}
};
I got the solution.
i was calling mCamera.startPreview(); out of my loop.
preview is must to take shots, and not including mCamera.startPreview(); was blocking my execution.
I am developing an Android Custom Camera app and trying to implement the video recorder feature in it. And I am using the below code
private boolean prepareMediaRecorder()
{
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try
{
File newFile = File.createTempFile("videocapture", ".mp4", Environment.getExternalStorageDirectory());
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
mediaRecorder.setOutputFile(newFile.getAbsolutePath());
}
catch (IOException e)
{
e.printStackTrace();
finish();
}
try
{
mediaRecorder.prepare();
}
catch (IllegalStateException e)
{
releaseMediaRecorder();
return false;
}
catch (IOException e)
{
releaseMediaRecorder();
return false;
}
return true;
}
But if i use the same code and run it on Galaxy tab, i get a force close at this location of code
mediaRecorder.start();
And the error log looks like this.
http://textuploader.com/?p=6&id=ngcFk
The code works just fine on the same, when i change
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
to
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
Not getting why is it so. Please Help! Thanks!
You should check whether the profile exists. I think for tab it is not supported. You can try that with hasprofile