2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.media;
20 * The MediaSyncEvent class defines events that can be used to synchronize playback or capture
21 * actions between different players and recorders.
22 * <p>For instance, {@link AudioRecord#startRecording(MediaSyncEvent)} is used to start capture
23 * only when the playback on a particular audio session is complete.
24 * The audio session ID is retrieved from a player (e.g {@link MediaPlayer}, {@link AudioTrack} or
25 * {@link ToneGenerator}) by use of the getAudioSessionId() method.
27 public class MediaSyncEvent {
30 * No sync event specified. When used with a synchronized playback or capture method, the
31 * behavior is equivalent to calling the corresponding non synchronized method.
33 public static final int SYNC_EVENT_NONE = AudioSystem.SYNC_EVENT_NONE;
36 * The corresponding action is triggered only when the presentation is completed
37 * (meaning the media has been presented to the user) on the specified session.
38 * A synchronization of this type requires a source audio session ID to be set via
39 * {@link #setAudioSessionId(int)} method.
41 public static final int SYNC_EVENT_PRESENTATION_COMPLETE =
42 AudioSystem.SYNC_EVENT_PRESENTATION_COMPLETE;
46 * Creates a synchronization event of the sepcified type.
48 * <p>The type specifies which kind of event is monitored.
49 * For instance, event {@link #SYNC_EVENT_PRESENTATION_COMPLETE} corresponds to the audio being
50 * presented to the user on a particular audio session.
51 * @param eventType the synchronization event type.
52 * @return the MediaSyncEvent created.
53 * @throws java.lang.IllegalArgumentException
55 public static MediaSyncEvent createEvent(int eventType)
56 throws IllegalArgumentException {
57 if (!isValidType(eventType)) {
58 throw (new IllegalArgumentException(eventType
59 + "is not a valid MediaSyncEvent type."));
61 return new MediaSyncEvent(eventType);
65 private final int mType;
66 private int mAudioSession = 0;
68 private MediaSyncEvent(int eventType) {
73 * Sets the event source audio session ID.
75 * <p>The audio session ID specifies on which audio session the synchronization event should be
77 * It is mandatory for certain event types (e.g. {@link #SYNC_EVENT_PRESENTATION_COMPLETE}).
78 * For instance, the audio session ID can be retrieved via
79 * {@link MediaPlayer#getAudioSessionId()} when monitoring an event on a particular MediaPlayer.
80 * @param audioSessionId the audio session ID of the event source being monitored.
81 * @return the MediaSyncEvent the method is called on.
82 * @throws java.lang.IllegalArgumentException
84 public MediaSyncEvent setAudioSessionId(int audioSessionId)
85 throws IllegalArgumentException {
86 if (audioSessionId > 0) {
87 mAudioSession = audioSessionId;
89 throw (new IllegalArgumentException(audioSessionId + " is not a valid session ID."));
95 * Gets the synchronization event type.
97 * @return the synchronization event type.
99 public int getType() {
104 * Gets the synchronization event audio session ID.
106 * @return the synchronization audio session ID. The returned audio session ID is 0 if it has
109 public int getAudioSessionId() {
110 return mAudioSession;
113 private static boolean isValidType(int type) {
115 case SYNC_EVENT_NONE:
116 case SYNC_EVENT_PRESENTATION_COMPLETE: