use of com.codename1.media.AudioService in project CodenameOne by codenameone.
the class AndroidImplementation method createBackgroundMedia.
@Override
public Media createBackgroundMedia(final String uri) throws IOException {
int mediaId = nextMediaId++;
Intent serviceIntent = new Intent(getContext(), AudioService.class);
serviceIntent.putExtra("mediaLink", uri);
serviceIntent.putExtra("mediaId", mediaId);
final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
background = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
AudioService.LocalBinder mLocalBinder = (AudioService.LocalBinder) service;
AudioService svc = (AudioService) mLocalBinder.getService();
background = svc;
}
};
boolean boundSuccess = getContext().bindService(serviceIntent, mConnection, getContext().BIND_AUTO_CREATE);
if (!boundSuccess) {
throw new RuntimeException("Failed to bind background media service for uri " + uri);
}
getContext().startService(serviceIntent);
while (background == null) {
Display.getInstance().invokeAndBlock(new Runnable() {
@Override
public void run() {
Util.sleep(200);
}
});
}
while (background.getMedia(mediaId) == null) {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
Util.sleep(200);
}
});
}
Media ret = new MediaProxy(background.getMedia(mediaId)) {
@Override
public void play() {
super.play();
}
@Override
public void cleanup() {
super.cleanup();
getContext().unbindService(mConnection);
}
};
return ret;
}
Aggregations