當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。