本文整理汇总了Java中android.media.AudioTrack.ERROR_INVALID_OPERATION属性的典型用法代码示例。如果您正苦于以下问题:Java AudioTrack.ERROR_INVALID_OPERATION属性的具体用法?Java AudioTrack.ERROR_INVALID_OPERATION怎么用?Java AudioTrack.ERROR_INVALID_OPERATION使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.media.AudioTrack
的用法示例。
在下文中一共展示了AudioTrack.ERROR_INVALID_OPERATION属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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" );
}
}