本文整理汇总了Java中android.media.AudioTrack.ERROR_BAD_VALUE属性的典型用法代码示例。如果您正苦于以下问题:Java AudioTrack.ERROR_BAD_VALUE属性的具体用法?Java AudioTrack.ERROR_BAD_VALUE怎么用?Java AudioTrack.ERROR_BAD_VALUE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.media.AudioTrack
的用法示例。
在下文中一共展示了AudioTrack.ERROR_BAD_VALUE属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startPlayer
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
if (mIsPlayStarted) {
Log.e(TAG, "Player already started !");
return false;
}
mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
Log.e(TAG, "Invalid parameter !");
return false;
}
Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
Log.e(TAG, "AudioTrack initialize fail !");
return false;
}
mIsPlayStarted = true;
Log.d(TAG, "Start audio player success !");
return true;
}
示例2: play
@WorkerThread
public synchronized boolean play(byte[] data, int size) {
if (mAudioTrack != null) {
try {
int ret = mAudioTrack.write(data, 0, size);
switch (ret) {
case AudioTrack.ERROR_INVALID_OPERATION:
Log.w(TAG, "play fail: ERROR_INVALID_OPERATION");
return false;
case AudioTrack.ERROR_BAD_VALUE:
Log.w(TAG, "play fail: ERROR_BAD_VALUE");
return false;
case AudioManager.ERROR_DEAD_OBJECT:
Log.w(TAG, "play fail: ERROR_DEAD_OBJECT");
return false;
default:
return true;
}
} catch (IllegalStateException e) {
Log.w(TAG, "play fail: " + e.getMessage());
return false;
}
}
Log.w(TAG, "play fail: null mAudioTrack");
return false;
}
示例3: createTrackInfo
/**
* Returns an object holding AudioTrack values needed to play a WavFormat.
*/
private TrackInfo createTrackInfo( WavFormat wavFormat ) throws WavException {
int trackChannelCfg = 0;
if ( wavFormat.headerChannels == 1 ) {
trackChannelCfg = AudioFormat.CHANNEL_OUT_MONO;
}
else if ( wavFormat.headerChannels == 2 ) {
trackChannelCfg = AudioFormat.CHANNEL_OUT_STEREO;
}
else {
throw new WavException( String.format( "Unsupported sound channels: %d", wavFormat.headerChannels ) );
}
if ( wavFormat.headerEncoding != 1 ) { // 1 is uncompressed, Linear PCM.
throw new WavException( String.format( "Unsupported sound encoding: %d", wavFormat.headerEncoding ) );
}
if ( wavFormat.headerBits != 16 ) {
throw new WavException( String.format( "Unsupported sound bitness: %d", wavFormat.headerBits ) );
}
int trackEncoding = AudioFormat.ENCODING_PCM_16BIT;
// This is a common range. Dunno how to ask Android for what it likes.
if ( wavFormat.headerRate < 11025 || wavFormat.headerRate > 48000 ) {
throw new WavException( String.format( "Unsupported sound rate: %d", wavFormat.headerRate ) );
}
int trackRate = wavFormat.headerRate;
int trackBufferSize = AudioTrack.getMinBufferSize( trackRate, trackChannelCfg, trackEncoding );
if ( trackBufferSize == AudioTrack.ERROR ) {
throw new WavException( "AudioTrack.getMinBufferSize() returned ERROR" );
}
else if ( trackBufferSize == AudioTrack.ERROR_BAD_VALUE ) {
throw new WavException( "AudioTrack.getMinBufferSize() returned BAD_VALUE" );
}
TrackInfo result = new TrackInfo();
result.channelCfg = trackChannelCfg;
result.encoding = trackEncoding;
result.rate = trackRate;
result.bufferSize = trackBufferSize;
return result;
}
示例4: run
@Override
public void run() {
logger.info( "Audio stream thread started" );
try {
InputStream is = new ByteArrayInputStream( audioBytes );
is.skip( wavFormat.dataChunkPos ); // Skip to the data chunk.
byte[] buf = new byte[512]; // TODO: This should be half TrackInfo's buffer size.
int chunkRemaining = wavFormat.dataChunkSize;
int bytesRead = 0;
while ( !aborting && chunkRemaining > 0 && (bytesRead=is.read( buf )) != -1 ) {
int writeRemaining = Math.min( bytesRead, chunkRemaining );
for ( int bytesWritten=0; !aborting && bytesWritten < writeRemaining; ) {
int writeResult;
synchronized ( track ) {
writeResult = track.write( buf, bytesWritten, writeRemaining-bytesWritten );
}
if ( writeResult == AudioTrack.ERROR_INVALID_OPERATION ) {
throw new IOException( "track.write() returned INVALID_OPERATION" );
}
else if ( writeResult == AudioTrack.ERROR_BAD_VALUE ) {
throw new IOException( "track.write() returned BAD_VALUE" );
}
else if ( writeResult == AudioTrack.ERROR_DEAD_OBJECT ) {
throw new IOException( "track.write() returned ERROR_DEAD_OBJECT" );
}
else if ( writeResult == AudioTrack.ERROR ) {
throw new IOException( "track.write() returned ERROR" );
}
bytesWritten += writeResult;
}
chunkRemaining -= writeRemaining;
}
logger.info( "Audio bytes left unwritten ({}/{})", chunkRemaining, wavFormat.dataChunkSize );
// With MODE_STATIC, post a runnable to the UI thread to play() after writing.
}
catch ( final IOException e ) {
logger.error( "Could not play audio", e );
AudioPlayerView.this.post(new Runnable() {
@Override
public void run() {
Toast.makeText( AudioPlayerView.this.getContext(), String.format( "Error playing audio: %s", e.getMessage() ), Toast.LENGTH_LONG ).show();
}
});
}
finally {
logger.info( "Audio stream thread ended" );
}
}