use of android.support.annotation.RequiresApi in project MusicLake by caiyonglong.
the class MusicPlayerService method onDestroy.
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onDestroy() {
super.onDestroy();
// Remove any sound effects
final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
sendBroadcast(audioEffectsIntent);
savePlayQueue(true);
// 释放mPlayer
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
// 释放Handler资源
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
mHandler = null;
}
// 释放工作线程资源
if (mWorkThread != null && mWorkThread.isAlive()) {
mWorkThread.quitSafely();
mWorkThread.interrupt();
mWorkThread = null;
}
audioAndFocusManager.abandonAudioFocus();
cancelNotification();
// 注销广播
unregisterReceiver(mServiceReceiver);
unregisterReceiver(mHeadsetReceiver);
unregisterReceiver(mHeadsetPlugInReceiver);
if (mWakeLock.isHeld())
mWakeLock.release();
}
use of android.support.annotation.RequiresApi in project grafika by google.
the class ScreenRecordActivity method onCreate.
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
new AlertDialog.Builder(this).setTitle("Error").setMessage("This activity only works on Marshmallow or later.").setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ScreenRecordActivity.this.finish();
}
}).show();
return;
}
Button toggleRecording = findViewById(R.id.screen_record_button);
toggleRecording.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
if (v.getId() == R.id.screen_record_button) {
if (muxerStarted) {
stopRecording();
((Button) findViewById(R.id.screen_record_button)).setText(R.string.toggleRecordingOn);
} else {
Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(permissionIntent, REQUEST_CODE_CAPTURE_PERM);
findViewById(R.id.screen_record_button).setEnabled(false);
}
}
}
});
mediaProjectionManager = (MediaProjectionManager) getSystemService(android.content.Context.MEDIA_PROJECTION_SERVICE);
encoderCallback = new MediaCodec.Callback() {
@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
Log.d(TAG, "Input Buffer Avail");
}
@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
ByteBuffer encodedData = videoEncoder.getOutputBuffer(index);
if (encodedData == null) {
throw new RuntimeException("couldn't fetch buffer at index " + index);
}
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
info.size = 0;
}
if (info.size != 0) {
if (muxerStarted) {
encodedData.position(info.offset);
encodedData.limit(info.offset + info.size);
muxer.writeSampleData(trackIndex, encodedData, info);
}
}
videoEncoder.releaseOutputBuffer(index, false);
}
@Override
public void onError(@NonNull MediaCodec codec, @NonNull MediaCodec.CodecException e) {
Log.e(TAG, "MediaCodec " + codec.getName() + " onError:", e);
}
@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
Log.d(TAG, "Output Format changed");
if (trackIndex >= 0) {
throw new RuntimeException("format changed twice");
}
trackIndex = muxer.addTrack(videoEncoder.getOutputFormat());
if (!muxerStarted && trackIndex >= 0) {
muxer.start();
muxerStarted = true;
}
}
};
}
use of android.support.annotation.RequiresApi in project grafika by google.
the class ScreenRecordActivity method startRecording.
@RequiresApi(api = Build.VERSION_CODES.M)
private void startRecording() {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay;
if (dm != null) {
defaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
} else {
throw new IllegalStateException("Cannot display manager?!?");
}
if (defaultDisplay == null) {
throw new RuntimeException("No display found.");
}
// Get the display size and density.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int screenDensity = metrics.densityDpi;
prepareVideoEncoder(screenWidth, screenHeight);
try {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/grafika", "Screen-record-" + Long.toHexString(System.currentTimeMillis()) + ".mp4");
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
muxer = new MediaMuxer(outputFile.getCanonicalPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} catch (IOException ioe) {
throw new RuntimeException("MediaMuxer creation failed", ioe);
}
// Start the video input.
mediaProjection.createVirtualDisplay("Recording Display", screenWidth, screenHeight, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, /* flags */
inputSurface, null, /* callback */
null);
}
use of android.support.annotation.RequiresApi in project grafika by google.
the class ScreenRecordActivity method prepareVideoEncoder.
@RequiresApi(api = Build.VERSION_CODES.M)
private void prepareVideoEncoder(int width, int height) {
MediaFormat format = MediaFormat.createVideoFormat(VIDEO_MIME_TYPE, width, height);
// 30 fps
int frameRate = 30;
// Set some required properties. The media codec may fail if these aren't defined.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
// 6Mbps
format.setInteger(MediaFormat.KEY_BIT_RATE, 6000000);
format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
format.setInteger(MediaFormat.KEY_CAPTURE_RATE, frameRate);
format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000000 / frameRate);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
// 1 seconds between I-frames
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
// Create a MediaCodec encoder and configure it. Get a Surface we can use for recording into.
try {
videoEncoder = MediaCodec.createEncoderByType(VIDEO_MIME_TYPE);
videoEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
inputSurface = videoEncoder.createInputSurface();
videoEncoder.setCallback(encoderCallback);
videoEncoder.start();
} catch (IOException e) {
releaseEncoders();
}
}
use of android.support.annotation.RequiresApi in project SpotiQ by ZinoKader.
the class ShortcutUtil method addSearchShortcut.
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public static void addSearchShortcut(Context context, String searchWithPartyTitle) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
Intent openSearch = new Intent(context.getApplicationContext(), SearchActivity.class);
openSearch.putExtra(ApplicationConstants.PARTY_NAME_EXTRA, searchWithPartyTitle);
openSearch.setAction(Intent.ACTION_VIEW);
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, ApplicationConstants.SEARCH_SHORTCUT_ID).setShortLabel("Search songs").setLongLabel("Search for songs to add to the queue").setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_search)).setIntent(openSearch).build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}
Aggregations