Search in sources :

Example 1 with MediaProxy

use of com.codename1.media.MediaProxy 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;
}
Also used : Media(com.codename1.media.Media) Paint(android.graphics.Paint) IBinder(android.os.IBinder) MediaProxy(com.codename1.media.MediaProxy) AudioService(com.codename1.media.AudioService)

Aggregations

Paint (android.graphics.Paint)1 IBinder (android.os.IBinder)1 AudioService (com.codename1.media.AudioService)1 Media (com.codename1.media.Media)1 MediaProxy (com.codename1.media.MediaProxy)1