java

[Android] wrong playback state when playing MIDI as Ringtone

The Problem

  1. play a MIDI file as Ringtone.
  2. check whether .isPlaying() after .play()
1
2
3
4
      Uri midiUri = Uri.parse( "/sdcard/some/path/to/example.midi");
      Ringtone ringtone = RingtoneManager.getRingtone(mContext, midiUri);
      ringtone.play();
      assertTrue("Couldn't play ringtone " + midiUri, ringtone.isPlaying());

The problems is that the last line always fails.

[Java] Catch Thread Exception

Java has a annoying feature that if any Exception happens, you must report it as throws XXXException.

If you want to run something in a Thread, but still want to catch Exception outside that Thread.

1
2
3
4
5
6
7
8
9
10
    ExecutorService executor = Executors.newCachedThreadPool();
    try {
        executor.execute(new Runnable() {
            public void run () {
                // code might throw Throwable or any other Exceptions.
            }
        });
    } catch (Throwable e) {
            // handling exception here.
    }

[Android] Camera callback not be invoked

The Problem

今天用 android.hardware.Camera 写了个简单的拍照函数,代码大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    private void doTakePicture(int cameraId) throws Exception {
        Camera camera = Camera.open(cameraId);
        camera.lock();

        camera.setPreviewDisplay(mActivity.getSurfaceHolder());
        camera.startPreview();

        mMonitor.reset();

        PictureCallback jpegCallback = new PictureCallback() {
            /* callback to save the picture we take */
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    FileOutputStream fos = new FileOutputStream(mOutFile);
                    fos.write(data);
                    fos.close();
                    Log.i(TAG, "notify take picture done");
                    mMonitor.signal();  // notify done
                } catch (FileNotFoundException e) {
                    Log.e(TAG, "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception: " + e.getMessage());
                }
            }
        };

        camera.takePicture(null, null, jpegCallback);

        // wait at most 4s for picture taken done
        boolean success = mMonitor.waitForSignal(4000);
        assertTrue(success);

        camera.stopPreview();
        camera.unlock();
        camera.release();
        camera = null;
    }

[Android] Force route audio stream to wired headset

I want to force route audio stream to headphone for some tests, no matter headset is on or off.

API has been deprecrated

Andoird's AudioManager has such API before, called setRouting , but it has deprecated for quite a long time. Currently, the available APIs can't meet my demands.

public void setRouting (int mode, int routes, int mask)
This method was deprecated in API level 4. Do not set audio routing directly, use setSpeakerphoneOn(), setBluetoothScoOn() methods instead.

[Android] The Story of MediaPlayer.setDataSource [Part.1]

Android MediaPlayer is a big class for all purpose media type playing, including local file and streaming playing.

The story start from MediaPlayer.setDataSource.

android.media package

android.media package locates in frameworks/base/media, and it's Java source is java/android/media.

This package provide various of interface for media playing/recording/decoding etc, including the MediaPlayer we are going to talk about today.

MediaPlayer.setDataSource()

I would not talk much about MediaPlayer constrution, the part we are insterested is what happens after calling setDataSource()

From developer.android, MediaPlayer provide 4 types of method reloading for setDataSource:

1
2
3
4
5
void setDataSource(String path)
void setDataSource(Context context, Uri uri, Map<String, String> headers)
void setDataSource(Context context, Uri uri)
void setDataSource(FileDescriptor fd, long offset, long length)
void setDataSource(FileDescriptor fd)