Merge changes from topic "startAudioSource_issues" am: 45da2609b7
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1678048 Change-Id: I1c13ec912645eb807c60370408d4c3f28bae1531
This commit is contained in:
@@ -35,7 +35,13 @@ public class HwAudioSource extends PlayerBase {
|
|||||||
private final AudioDeviceInfo mAudioDeviceInfo;
|
private final AudioDeviceInfo mAudioDeviceInfo;
|
||||||
private final AudioAttributes mAudioAttributes;
|
private final AudioAttributes mAudioAttributes;
|
||||||
|
|
||||||
private int mNativeHandle;
|
/**
|
||||||
|
* The value of the native handle encodes the HwAudioSource state.
|
||||||
|
* The native handle returned by {@link AudioSystem#startAudioSource} is either valid
|
||||||
|
* (aka > 0, so successfully started) or hosting an error code (negative).
|
||||||
|
* 0 corresponds to an untialized or stopped HwAudioSource.
|
||||||
|
*/
|
||||||
|
private int mNativeHandle = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor for a hardware audio source based player.
|
* Class constructor for a hardware audio source based player.
|
||||||
@@ -127,29 +133,38 @@ public class HwAudioSource extends PlayerBase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the playback from {@link AudioDeviceInfo}.
|
* Starts the playback from {@link AudioDeviceInfo}.
|
||||||
|
* Starts does not return any error code, caller must check {@link HwAudioSource#isPlaying} to
|
||||||
|
* ensure the state of the HwAudioSource encoded in {@link mNativeHandle}.
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
Preconditions.checkState(!isPlaying(), "HwAudioSource is currently playing");
|
Preconditions.checkState(!isPlaying(), "HwAudioSource is currently playing");
|
||||||
baseStart();
|
|
||||||
mNativeHandle = AudioSystem.startAudioSource(
|
mNativeHandle = AudioSystem.startAudioSource(
|
||||||
mAudioDeviceInfo.getPort().activeConfig(),
|
mAudioDeviceInfo.getPort().activeConfig(),
|
||||||
mAudioAttributes);
|
mAudioAttributes);
|
||||||
|
if (isPlaying()) {
|
||||||
|
baseStart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the HwAudioSource player is playing.
|
* Checks whether the HwAudioSource player is playing.
|
||||||
|
* It checks the state of the HwAudioSource encoded in {@link HwAudioSource#isPlaying}.
|
||||||
|
* 0 corresponds to a stopped or uninitialized HwAudioSource.
|
||||||
|
* Negative value corresponds to a status reported by {@link AudioSystem#startAudioSource} to
|
||||||
|
* indicate a failure when trying to start the HwAudioSource.
|
||||||
|
*
|
||||||
* @return true if currently playing, false otherwise
|
* @return true if currently playing, false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isPlaying() {
|
public boolean isPlaying() {
|
||||||
return mNativeHandle != 0;
|
return mNativeHandle > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the playback from {@link AudioDeviceInfo}.
|
* Stops the playback from {@link AudioDeviceInfo}.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
baseStop();
|
|
||||||
if (mNativeHandle > 0) {
|
if (mNativeHandle > 0) {
|
||||||
|
baseStop();
|
||||||
AudioSystem.stopAudioSource(mNativeHandle);
|
AudioSystem.stopAudioSource(mNativeHandle);
|
||||||
mNativeHandle = 0;
|
mNativeHandle = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user