Search in sources :

Example 26 with Runnable

use of java.lang.Runnable in project android_frameworks_base by ResurrectionRemix.

the class MediaPlayer method addSubtitleSource.

/** @hide */
public void addSubtitleSource(InputStream is, MediaFormat format) throws IllegalStateException {
    final InputStream fIs = is;
    final MediaFormat fFormat = format;
    if (is != null) {
        // way to implement timeouts in the future.
        synchronized (mOpenSubtitleSources) {
            mOpenSubtitleSources.add(is);
        }
    } else {
        Log.w(TAG, "addSubtitleSource called with null InputStream");
    }
    getMediaTimeProvider();
    // process each subtitle in its own thread
    final HandlerThread thread = new HandlerThread("SubtitleReadThread", Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
    thread.start();
    Handler handler = new Handler(thread.getLooper());
    handler.post(new Runnable() {

        private int addTrack() {
            if (fIs == null || mSubtitleController == null) {
                return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
            }
            SubtitleTrack track = mSubtitleController.addTrack(fFormat);
            if (track == null) {
                return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
            }
            // TODO: do the conversion in the subtitle track
            Scanner scanner = new Scanner(fIs, "UTF-8");
            String contents = scanner.useDelimiter("\\A").next();
            synchronized (mOpenSubtitleSources) {
                mOpenSubtitleSources.remove(fIs);
            }
            scanner.close();
            synchronized (mIndexTrackPairs) {
                mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
            }
            Handler h = mTimeProvider.mEventHandler;
            int what = TimeProvider.NOTIFY;
            int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
            Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, contents.getBytes());
            Message m = h.obtainMessage(what, arg1, 0, trackData);
            h.sendMessage(m);
            return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
        }

        public void run() {
            int res = addTrack();
            if (mEventHandler != null) {
                Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
                mEventHandler.sendMessage(m);
            }
            thread.getLooper().quitSafely();
        }
    });
}
Also used : MediaFormat(android.media.MediaFormat) Scanner(java.util.Scanner) HandlerThread(android.os.HandlerThread) Message(android.os.Message) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Runnable(java.lang.Runnable) Handler(android.os.Handler) Pair(android.util.Pair)

Example 27 with Runnable

use of java.lang.Runnable in project android_frameworks_base by crdroidandroid.

the class SvcMonitor method startMonitor.

private void startMonitor(Intent intent) {
    if (tMonitor != null) {
        Log.d(TAG, "thread already active");
        return;
    }
    javaProc = intent.getStringExtra("java");
    halProc = intent.getStringExtra("hal");
    period = intent.getIntExtra("period", 1000);
    if (javaProc == null || halProc == null || period < 100) {
        Log.d(TAG, "Failed starting monitor, invalid arguments.");
        stopSelf();
        return;
    }
    Runnable monitor = new MonitorRunnable(this);
    tMonitor = new Thread(monitor);
    tMonitor.start();
}
Also used : Runnable(java.lang.Runnable) Thread(java.lang.Thread)

Aggregations

Runnable (java.lang.Runnable)27 Handler (android.os.Handler)17 HandlerThread (android.os.HandlerThread)15 Context (android.content.Context)10 MediaFormat (android.media.MediaFormat)10 Message (android.os.Message)10 Pair (android.util.Pair)10 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 AssetFileDescriptor (android.content.res.AssetFileDescriptor)5 SubtitleController (android.media.SubtitleController)5 Anchor (android.media.SubtitleController.Anchor)5 RenderingWidget (android.media.SubtitleTrack.RenderingWidget)5 ErrnoException (android.system.ErrnoException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileDescriptor (java.io.FileDescriptor)5 IOException (java.io.IOException)5 Thread (java.lang.Thread)5 Scanner (java.util.Scanner)5 Override (java.lang.Override)1