Media player stops when its current file is deleted

This commit is contained in:
masensio 2015-08-10 14:58:56 +02:00 committed by tobiasKaminsky
parent 4e013b5bb6
commit 722afd6519
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
2 changed files with 65 additions and 0 deletions

View file

@ -38,6 +38,9 @@ import android.content.SharedPreferences;
import android.content.SyncRequest;
import android.content.pm.PackageManager;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
@ -74,6 +77,9 @@ import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.media.MediaService;
import com.owncloud.android.media.MediaServiceBinder;
import com.owncloud.android.operations.CopyFileOperation;
import com.owncloud.android.operations.CreateFolderOperation;
import com.owncloud.android.operations.MoveFileOperation;
@ -165,6 +171,8 @@ public class FileDisplayActivity extends HookActivity
private OCFile mWaitingToSend;
private Collection<MenuItem> mDrawerMenuItemstoShowHideList;
private MediaServiceBinder mMediaServiceBinder = null;
private MediaServiceConnection mMediaServiceConnection = null;
private String searchQuery;
@ -1600,6 +1608,44 @@ public class FileDisplayActivity extends HookActivity
mUploaderBinder = null;
}
}
};
private MediaServiceConnection newMediaConnection(){
return new MediaServiceConnection();
}
/** Defines callbacks for service binding, passed to bindService() */
private class MediaServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName component, IBinder service) {
if (component.equals(new ComponentName(FileDisplayActivity.this, MediaService.class))) {
Log_OC.d(TAG, "Media service connected");
mMediaServiceBinder = (MediaServiceBinder) service;
}else {
return;
}
}
@Override
public void onServiceDisconnected(ComponentName component) {
if (component.equals(new ComponentName(FileDisplayActivity.this,
MediaService.class))) {
Log_OC.e(TAG, "Media service disconnected");
mMediaServiceBinder = null;
}
}
};
@Override
public void onSavedCertificate() {
startSyncFolderOperation(getCurrentDir(), false);
}
/**
@ -1669,6 +1715,7 @@ public class FileDisplayActivity extends HookActivity
if (result.isSuccess()) {
OCFile removedFile = operation.getFile();
tryStopPlaying(removedFile);
FileFragment second = getSecondFragment();
if (second != null && removedFile.equals(second.getFile())) {
if (second instanceof PreviewMediaFragment) {
@ -1689,6 +1736,21 @@ public class FileDisplayActivity extends HookActivity
}
}
public void setMediaServiceConnection() {
mMediaServiceConnection = newMediaConnection();// mediaServiceConnection;
if (mMediaServiceConnection != null) {
bindService(new Intent(this, MediaService.class), mMediaServiceConnection,
Context.BIND_AUTO_CREATE);
}
}
private void tryStopPlaying(OCFile file){
if (mMediaServiceConnection != null && file.isAudio()){
if (mMediaServiceBinder.isPlaying(file)){
mMediaServiceBinder.pause();
}
}
}
/**
* Updates the view associated to the activity after the finish of an operation trying to move a

View file

@ -62,6 +62,7 @@ import com.owncloud.android.media.MediaControlView;
import com.owncloud.android.media.MediaService;
import com.owncloud.android.media.MediaServiceBinder;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
import com.owncloud.android.ui.fragment.FileFragment;
@ -692,6 +693,8 @@ public class PreviewMediaFragment extends FileFragment implements
mMediaServiceConnection,
Context.BIND_AUTO_CREATE);
// follow the flow in MediaServiceConnection#onServiceConnected(...)
((FileDisplayActivity) getActivity()).setMediaServiceConnection();
}
/** Defines callbacks for service binding, passed to bindService() */