Android media player stop playing while in background - java

I'm making music player app with simple functionality. But when I listen music on my phone with Android 6, sometimes music stops playing until I turn on display again with power button. Then next song is playing, so it seems like it's problem with loading next song. I tried to write new app just to test it out, for this purpose I used this tutorial:
https://code.tutsplus.com/tutorials/background-audio-in-android-with-mediasessioncompat--cms-27030
To this example I added ArrayList with paths to songs. In mediaPlayer onCompletionListener I increase track counter and load new song to media player.
My code:
private void initMediaPlayer() {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setVolume(1.0f, 1.0f);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer)
{
onTrackCompletion();
}
});
private void onTrackCompletion()
{
NextTrack();
Play();
}
private void NextTrack()
{
playlistPosition++;
if (playlistPosition == playlists.get(playlistCurrent).size){
playlistPosition = 0;
}
sendAction(ACTION_TRACK_NEXT);
if(mMediaPlayer.isPlaying()){
Pause();
}
loadSong();
Play();
}
private void loadSong()
{
String path = playlists.get(playlistCurrent).getPath(playlistPosition);
if(path == null || path == "")
{
return;
}
try
{
try
{
mMediaPlayer.setDataSource(path);
} catch( IllegalStateException e ) {
mMediaPlayer.release();
initMediaPlayer();
mMediaPlayer.setDataSource(path);
}
initMediaSessionMetadata();
} catch (IOException e) {
return;
}
try {
mMediaPlayer.prepare();
} catch (IOException e) {}
sendTrackData();
}
I don't know anymore why this doesn't work. In manifest I have WAKE_LOCK permission. I also set wake lock for Media player.
Edit:
Today I tried to move loading song into onPlayFromMediaId. I made broadcast from AutoActivity where is Media player to Main Activity and send back onPlayFromMediaId with path to song. But seems like this doesn't work either.I also find out that changing volume with buttons also wake up app.
Edit2:
I made many tests and added debug string in many places in code. And I found out that app stops at mediaplayer.prepare() until I trigger any action on phone (turn on display, volume up/down, click headset button). But I don't know how to fix this bug. I tried to use prepareAsync, but didn't help.

Unless you use foreground service, the system will kill your process and mediaplayer will stop.
below is a part from from a foreground service ( notification example).
builder.setContentTitle(aMessage) // required
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(this.getString(R.string.app_name)) // required
.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{0L})
.setPriority(Notification.PRIORITY_HIGH);
int mId = 1489;
startForeground(mId, builder.build());
The above code is tested and working fine.

Related

JavaFX MediaPlayer sound briefly stops after playing again an audio that has been paused

I'm using JavaFX to create a media player, and everything works fine, but I have a small issue, when I call mediaPlayer.play() after I paused the audio/video, I get a weird sound glitch, like the audio plays, then pauses por like 100ms and then plays again, normally.
my code looks like this
choosing a file
public void chooseFileClicked(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);
path = file.toURI().toString();
if (mediaPlayer != null) {
mediaPlayer.stop();
}
if (path != null) {
media = new Media(path);
mediaPlayer = new MediaPlayer(media);
playButton.setDisable(false);
playButton.setSelected(true);
try { // if its a video
mediaView.setMediaPlayer(mediaPlayer);
mediaView.fitHeightProperty().bind(screenBackground.widthProperty());
mediaView.fitWidthProperty().bind(screenBackground.heightProperty());
}
catch(Exception ex) { // if its audio, get other stuff
//
}
mediaPlayer.play();
}
and the pause/play
public void playButtonClicked(ActionEvent e) {
if (playButton.isSelected()) {
mediaPlayer.play();
}
else {
mediaPlayer.pause();
}
}
I tried searching on internet about this, but I can't find any information about it.
I don't know if this is just a sound glitch that happens because I'm in the IDE and everything is slower there, or if this really its a bug in my program and it can be fixed

MediaPlayer loading sound from Uri not working

I have a simple voice recorder app. When the user records something it gets saved on the external storage and after that it gets played from there.
Everything works fine except on a Huawei P8 Lite smartphone with Android 6.
Here is how I play the sound:
Thread th=new Thread(new Runnable() {
#Override
public void run() {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(view.getContext(), uri);
mp.prepare();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//send message to handler
}
});
th.start();
The Uri does exist at this moment and also the file does exist in the destination. Why is my mediaPlayer not playing anything? The permissions are given at this point where it tries to create the MediaPlayer so it should not be a permission issue.
All I get in the logcat is:
W/MediaPlayer-JNI: MediaPlayer finalized without being released
I/MediaPlayer: setDataSource(34, 0, 576460752303423487)
I/MediaPlayer: [HSM] stayAwake true uid: 10255, pid: 22592
E/MediaPlayer: error (-19, 0)
E/MediaPlayer: Error (-19,0)
Like I said it works fine on all other devices I tested (Android 10 & Android 9)
EDIT
Here is what I tried:
I created Emulators with every API level from API 18 to 30 and it works fine on every emulator.
I reseted my Huawei P8 Lite smartphone to factory settings.
I checked if the sound file is saved properly and if I can open and play it manualy when I navigate in the folder where it gets saved (it works perfectly so it has nothing to do with the code that saves the sound)
I really run out of ideas.
Hope someone can help, Thanks!
You need to player prepare asynchronously. player. prepare() replace with below code:
player.prepareAsync()

onFaceDetection called only once or twice while running but works perfectly when debugging with breakpoints

This is the code I am using for face detection, the problem is when I debug this code with android studio the onFaceDetection method is called multiple times and face is detected perfectly(When i put a break point inside the method). But when I run it without any break points the method is called only 2-3 times and face detection doesn't take place. Any help regarding this would be much appreciated, as you can see from the code I've tried stopping and starting face detection.
void setFaceDetectionListener() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mFaceDetectionListener = new Camera.FaceDetectionListener() {
Handler faceDetectionHandler;
#Override
public void onFaceDetection(final Camera.Face[] faces, final Camera camera) {
if(faceDetectionHandler == null){//Initialize
faceDetectionHandler = new Handler();
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_SHOW_YOUR_FACE.toString(),
Toast.LENGTH_SHORT).show();
}
faceDetectionHandler.post(new Runnable() {
#Override
public void run() {
Log.e("faceDetect", "No of faces = " + faces.length);
if (!is_face_detected) {
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_DETECTING_YOUR_FACE.toString(),
Toast.LENGTH_SHORT).show();
is_face_detected = faces.length > 0;
}
if (faces.length > 0) {
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_FACE_DETECTED.toString(),
Toast.LENGTH_SHORT).show();
camera.stopFaceDetection();
} else {
camera.stopFaceDetection();
camera.startFaceDetection();
}
}
});
}
};
}
}
This was ignorance on my part, apparently you can't have face detection running while the media recorder is running, So guys don't try to run face detection while you are recording with the camera simultaneously.
If you really wanted to detect faces while recording then you should use the
onPreviewFrame(byte[] pixelData, Camera camera)
method in
Camera.PreviewCallback()
convert the pixelData to RGB_565 bitmap and supply it to the FaceDetector.findfaces method. But in my experience I find this method to be very unreliable.

Android MediaPlayer pause

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.

Enabling Camera Flash While Recording Video

I need a way to control the camera flash on an Android device while it is recording video. I'm making a strobe light app, and taking videos with a flashing strobe light would result in the ability to record objects that are moving at high speeds, like a fan blade.
The flash can only be enabled by starting a video preview and setting FLASH_MODE_TORCH in the camera's parameters. That would look like this:
Camera c = Camera.open();
Camera.Parameters p = c.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();
Once the preview has started, I can flip that parameter back and forth to turn the light on and off. This works well until I try to record a video. The trouble is that in order to give the camera to the MediaRecorder, I first have to unlock it.
MediaRecorder m = new MediaRecorder();
c.unlock(); // the killer
m.setCamera(c);
After that unlock, I can no longer change the camera parameters and therefore have no way to change the flash state.
I do not know if it is actually possible to do this since I'm not the best at java-hacking, but here is what I do know:
Camera.unlock() is a native method, so I can't really see the mechanism behind the way it locks me out
Camera.Parameter has a HashMap that contains all of its parameters
Camera.setParameters(Parameters) takes the HashMap, converts it to a string, and passes it to a native method
I can eliminate all the parameters but TORCH-MODE from the HashMap and the Camera will still accept it
So, I can still access the Camera, but it won't listen to anything I tell it. (Which is kind of the purpose of Camera.unlock())
Edit:
After examining the native code, I can see that in CameraService.cpp my calls to Camera.setParameters(Parameters) get rejected because my Process ID does not match the Process ID the camera service has on record. So it would appear that that is my hurdle.
Edit2:
It would appear that the MediaPlayerService is the primary service that takes control of the camera when a video is recording. I do not know if it is possible, but if I could somehow start that service in my own process, I should be able to skip the Camera.unlock() call.
Edit3:
One last option would be if I could somehow get a pointer to the CameraHardwareInterface. From the looks of it, this is a device specific interface and probably does not include the PID checks. The main problem with this though is that the only place that I can find a pointer to it is in CameraService, and CameraService isn't talking.
Edit4: (several months later)
At this point, I don't think it is possible to do what I originally wanted. I don't want to delete the question on the off chance that someone does answer it, but I'm not actively seeking an answer. (Though, receiving a valid answer would be awesome.)
I encountered a similar issue. The user should be able to change the flash mode during recording to meet their needs depending on the light situation. After some investigative research i came to the following solution:
I assume, that you've already set up a proper SurfaceView and a SurfaceHolder with its necessary callbacks. The first thing i did was providing this code (not declared variables are globals):
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();
recorder = new MediaRecorder();
} catch (IOException e) {
e.printStackTrace();
}
}
My next step was initializing and preparing the recorder:
private void initialize() {
camera.unlock();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setVideoFrameRate(20);
recorder.setOutputFile(filePath);
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
It's important to note, that camera.unlock() has to be called BEFORE the whole initialization process of the media recorder. That said also be aware of the proper order of each set property, otherwise you'll get an IllegalStateException when calling prepare() or start(). When it comes to recording, i do this. This will usually be triggered by a view element:
public void record(View view) {
if (recording) {
recorder.stop();
//TODO: do stuff....
recording = false;
} else {
recording = true;
initialize();
recorder.start();
}
}
So now, i finally can record properly. But what's with that flash? Last but not least, here comes the magic behind the scenes:
public void flash(View view) {
if(!recording) {
camera.lock();
}
parameters.setFlashMode(parameters.getFlashMode().equals(Parameters.FLASH_MODE_TORCH) ? Parameters.FLASH_MODE_OFF : Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
if(!recording) {
camera.unlock();
}
}
Everytime i call that method via an onClick action i can change the flash mode, even during recording. Just take care of properly locking the camera. Once the lock is aquired by the media recorder during recording, you don't have to lock/unlock the camera again. It doesn't even work. This was tested on a Samsung Galaxy S3 with Android-Version 4.1.2. Hope this approach helps.
After preparing media recorder, use camera.lock(), and then set whatever parameters you want to set to camera.
But before starting recording you need to call camera.unlock(), and after you stop media recorder you need to call camera.lock() to start preview.
Enjoy!!!
Try this.. hopefully it will work.. :)
private static Torch torch;
public Torch() {
super();
torch = this;
}
public static Torch getTorch() {
return torch;
}
private void getCamera() {
if (mCamera == null) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {
Log.e(TAG, "Camera.open() failed: " + e.getMessage());
}
}
}
public void toggleLight(View view) {
toggleLight();
}
private void toggleLight() {
if (lightOn) {
turnLightOff();
} else {
turnLightOn();
}
}
private void turnLightOn() {
if (!eulaAgreed) {
return;
}
if (mCamera == null) {
Toast.makeText(this, "Camera not found", Toast.LENGTH_LONG);
button.setBackgroundColor(COLOR_WHITE);
return;
}
lightOn = true;
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
button.setBackgroundColor(COLOR_WHITE);
return;
}
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes == null) {
button.setBackgroundColor(COLOR_WHITE);
return;
}
String flashMode = parameters.getFlashMode();
Log.i(TAG, "Flash mode: " + flashMode);
Log.i(TAG, "Flash modes: " + flashModes);
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
button.setBackgroundColor(COLOR_LIGHT);
startWakeLock();
} else {
Toast.makeText(this, "Flash mode (torch) not supported",
Toast.LENGTH_LONG);
button.setBackgroundColor(COLOR_WHITE);
Log.e(TAG, "FLASH_MODE_TORCH not supported");
}
}
}
private void turnLightOff() {
if (lightOn) {
button.setBackgroundColor(COLOR_DARK);
lightOn = false;
if (mCamera == null) {
return;
}
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
return;
}
List<String> flashModes = parameters.getSupportedFlashModes();
String flashMode = parameters.getFlashMode();
if (flashModes == null) {
return;
}
Log.i(TAG, "Flash mode: " + flashMode);
Log.i(TAG, "Flash modes: " + flashModes);
if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
stopWakeLock();
} else {
Log.e(TAG, "FLASH_MODE_OFF not supported");
}
}
}
}
private void startPreview() {
if (!previewOn && mCamera != null) {
mCamera.startPreview();
previewOn = true;
}
}
private void stopPreview() {
if (previewOn && mCamera != null) {
mCamera.stopPreview();
previewOn = false;
}
}
private void startWakeLock() {
if (wakeLock == null) {
Log.d(TAG, "wakeLock is null, getting a new WakeLock");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Log.d(TAG, "PowerManager acquired");
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
Log.d(TAG, "WakeLock set");
}
wakeLock.acquire();
Log.d(TAG, "WakeLock acquired");
}
private void stopWakeLock() {
if (wakeLock != null) {
wakeLock.release();
Log.d(TAG, "WakeLock released");
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Eula.show(this)) {
eulaAgreed = true;
}
setContentView(R.layout.main);
button = findViewById(R.id.button);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
disablePhoneSleep();
Log.i(TAG, "onCreate");
}
To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the <uses-feature> manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus feature, your Manifest should include the following:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
A sample that checks for torch support might look something like this:
//Create camera and parameter objects
private Camera mCamera;
private Camera.Parameters mParameters;
private boolean mbTorchEnabled = false;
//... later in a click handler or other location, assuming that the mCamera object has already been instantiated with Camera.open()
mParameters = mCamera.getParameters();
//Get supported flash modes
List flashModes = mParameters.getSupportedFlashModes ();
//Make sure that torch mode is supported
//EDIT - wrong and dangerous to check for torch support this way
//if(flashModes != null && flashModes.contains("torch")){
if(flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
if(mbTorchEnabled){
//Set the flash parameter to off
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
else{
//Set the flash parameter to use the torch
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
//Commit the camera parameters
mCamera.setParameters(mParameters);
mbTorchEnabled = !mbTorchEnabled;
}
To turn the torch on, you simply set the camera parameter Camera.Parameters.FLASH_MODE_TORCH
Camera mCamera;
Camera.Parameters mParameters;
//Get a reference to the camera/parameters
mCamera = Camera.open();
mParameters = mCamera.getParameters();
//Set the torch parameter
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
//Comit camera parameters
mCamera.setParameters(mParameters);
To turn the torch off, set Camera.Parameters.FLASH_MODE_OFF

Categories

Resources