当前位置: 首页>>代码示例>>Java>>正文


Java AudioTrack.ERROR_INVALID_OPERATION属性代码示例

本文整理汇总了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;
}
 
开发者ID:Piasy,项目名称:RxAndroidAudio,代码行数:26,代码来源:StreamAudioPlayer.java

示例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" );
	}
}
 
开发者ID:Vhati,项目名称:OpenUHS,代码行数:53,代码来源:AudioPlayerView.java


注:本文中的android.media.AudioTrack.ERROR_INVALID_OPERATION属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。