Search in sources :

Example 1 with NSData

use of cli.MonoTouch.Foundation.NSData in project playn by threerings.

the class CAFLoader method load.

public static void load(String path, int bufferId) {
    // mmap (if possible) the audio file for efficient reading/uploading
    NSError[] err = new NSError[1];
    NSData data = NSData.FromFile(path, NSDataReadingOptions.wrap(READ_OPTS), err);
    if (err[0] != null) {
        throw new RuntimeException(err[0].ToString());
    }
    // read the CAFF metdata to find out the audio format and the data offset/length
    BinaryReader br = new BinaryReader(data.AsStream());
    if (!new String(br.ReadChars(4)).equals("caff"))
        throw new RuntimeException("Input file not CAFF: " + path);
    // rest of caf file header
    br.ReadBytes(4);
    CAFDesc desc = null;
    int offset = 8, dataOffset = 0, dataLength = 0;
    do {
        String type = new String(br.ReadChars(4));
        int size = (int) BitConverter.ToInt64(reverse(br.ReadBytes(8)), 0);
        offset += 12;
        if (type.equals("data")) {
            dataOffset = offset;
            dataLength = size;
        } else if (type.equals("desc")) {
            desc = new CAFDesc(br.ReadBytes(size));
            if ("ima4".equalsIgnoreCase(desc.formatID))
                throw new RuntimeException("Cannot use compressed CAFF. " + "Use AIFC for compressed audio on iOS.");
        } else {
            br.ReadBytes(size);
        }
        offset += size;
    } while (dataOffset == 0);
    br.Close();
    // upload the audio data to OpenAL straight from the mmap'd file
    AL.BufferData(bufferId, desc.GetALFormat(), IntPtr.Add(data.get_Bytes(), dataOffset), dataLength, (int) desc.sampleRate);
    // now dispose the mmap'd file to free up resources
    data.Dispose();
    // finally freak out if OpenAL didn't like what we sent it
    ALError error = AL.GetError();
    if (error.Value != ALError.NoError) {
        throw new RuntimeException(error.ToString());
    }
}
Also used : NSData(cli.MonoTouch.Foundation.NSData) NSError(cli.MonoTouch.Foundation.NSError) ALError(cli.OpenTK.Audio.OpenAL.ALError) BinaryReader(cli.System.IO.BinaryReader)

Example 2 with NSData

use of cli.MonoTouch.Foundation.NSData in project playn by threerings.

the class IOSAssets method getRemoteImage.

@Override
public Image getRemoteImage(String url, float width, float height) {
    final IOSAsyncImage image = new IOSAsyncImage(platform.graphics().ctx, width, height);
    new NSUrlConnection(new NSUrlRequest(new NSUrl(url)), new NSUrlConnectionDelegate() {

        private NSMutableData data = new NSMutableData();

        @Override
        public void ReceivedData(NSUrlConnection conn, NSData data) {
            this.data.AppendData(data);
        }

        @Override
        public void FailedWithError(NSUrlConnection conn, NSError error) {
            onFailure(new Exception(error.get_LocalizedDescription()));
        }

        @Override
        public void FinishedLoading(NSUrlConnection conn) {
            try {
                setImageLater(image, UIImage.LoadFromData(this.data), Scale.ONE);
            } catch (Throwable cause) {
                onFailure(cause);
            }
        }

        protected void onFailure(final Throwable cause) {
            setErrorLater(image, cause);
        }
    }, true);
    return image;
}
Also used : NSData(cli.MonoTouch.Foundation.NSData) NSUrl(cli.MonoTouch.Foundation.NSUrl) NSUrlConnection(cli.MonoTouch.Foundation.NSUrlConnection) NSError(cli.MonoTouch.Foundation.NSError) NSUrlConnectionDelegate(cli.MonoTouch.Foundation.NSUrlConnectionDelegate) NSMutableData(cli.MonoTouch.Foundation.NSMutableData) NSUrlRequest(cli.MonoTouch.Foundation.NSUrlRequest) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with NSData

use of cli.MonoTouch.Foundation.NSData in project playn by threerings.

the class IOSNet method sendRequest.

protected void sendRequest(NSUrlRequest req, final Callback<Response> callback) {
    new NSUrlConnection(req, new NSUrlConnectionDelegate() {

        private NSMutableData data;

        private int rspCode = -1;

        private NSDictionary headers;

        @Override
        public void ReceivedResponse(NSUrlConnection conn, NSUrlResponse rsp) {
            // if we are redirected, we may accumulate data as we bounce through requests, so we reset
            // our data accumulator each time we receive the response headers
            data = new NSMutableData();
            // note our most recent response code
            if (rsp instanceof NSHttpUrlResponse) {
                NSHttpUrlResponse hrsp = (NSHttpUrlResponse) rsp;
                rspCode = hrsp.get_StatusCode();
                headers = hrsp.get_AllHeaderFields();
            }
        }

        @Override
        public void ReceivedData(NSUrlConnection conn, NSData data) {
            this.data.AppendData(data);
        }

        @Override
        public void FailedWithError(NSUrlConnection conn, NSError error) {
            String errmsg = error.get_LocalizedDescription();
            Exception exn = rspCode > 0 ? new HttpException(rspCode, errmsg) : new Exception(errmsg);
            platform.notifyFailure(callback, exn);
        }

        @Override
        public void FinishedLoading(NSUrlConnection conn) {
            platform.notifySuccess(callback, new ResponseImpl(rspCode) {

                @Override
                protected Map<String, List<String>> extractHeaders() {
                    Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
                    for (NSObject key : headers.get_Keys()) {
                        // iOS concatenates all repeated headers into a single header separated by commas,
                        // which is known to be a fucking stupid thing to do, but hey, they're doing it!
                        headerMap.put(key.ToString(), Collections.singletonList(headers.get_Item(key).ToString()));
                    }
                    return headerMap;
                }

                @Override
                public String payloadString() {
                    return NSString.op_Implicit(NSString.FromData(data, NSStringEncoding.wrap(NSStringEncoding.UTF8)));
                }

                @Override
                public byte[] payload() {
                    int length = Convert.ToInt32(data.get_Length());
                    byte[] bytes = new byte[length];
                    Marshal.Copy(data.get_Bytes(), bytes, 0, length);
                    return bytes;
                }
            });
        }
    }, true);
}
Also used : NSObject(cli.MonoTouch.Foundation.NSObject) NSData(cli.MonoTouch.Foundation.NSData) HashMap(java.util.HashMap) NSDictionary(cli.MonoTouch.Foundation.NSDictionary) NSError(cli.MonoTouch.Foundation.NSError) NSUrlConnectionDelegate(cli.MonoTouch.Foundation.NSUrlConnectionDelegate) NSString(cli.MonoTouch.Foundation.NSString) NSHttpUrlResponse(cli.MonoTouch.Foundation.NSHttpUrlResponse) NSUrlResponse(cli.MonoTouch.Foundation.NSUrlResponse) NSUrlConnection(cli.MonoTouch.Foundation.NSUrlConnection) NSMutableData(cli.MonoTouch.Foundation.NSMutableData) List(java.util.List)

Aggregations

NSData (cli.MonoTouch.Foundation.NSData)3 NSError (cli.MonoTouch.Foundation.NSError)3 NSMutableData (cli.MonoTouch.Foundation.NSMutableData)2 NSUrlConnection (cli.MonoTouch.Foundation.NSUrlConnection)2 NSUrlConnectionDelegate (cli.MonoTouch.Foundation.NSUrlConnectionDelegate)2 NSDictionary (cli.MonoTouch.Foundation.NSDictionary)1 NSHttpUrlResponse (cli.MonoTouch.Foundation.NSHttpUrlResponse)1 NSObject (cli.MonoTouch.Foundation.NSObject)1 NSString (cli.MonoTouch.Foundation.NSString)1 NSUrl (cli.MonoTouch.Foundation.NSUrl)1 NSUrlRequest (cli.MonoTouch.Foundation.NSUrlRequest)1 NSUrlResponse (cli.MonoTouch.Foundation.NSUrlResponse)1 ALError (cli.OpenTK.Audio.OpenAL.ALError)1 BinaryReader (cli.System.IO.BinaryReader)1 FileNotFoundException (java.io.FileNotFoundException)1 HashMap (java.util.HashMap)1 List (java.util.List)1