Search in sources :

Example 76 with RequiresApi

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();
}
Also used : PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) RequiresApi(android.support.annotation.RequiresApi)

Example 77 with RequiresApi

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;
            }
        }
    };
}
Also used : MediaFormat(android.media.MediaFormat) DialogInterface(android.content.DialogInterface) Intent(android.content.Intent) View(android.view.View) ByteBuffer(java.nio.ByteBuffer) Button(android.widget.Button) MediaCodec(android.media.MediaCodec) RequiresApi(android.support.annotation.RequiresApi) TargetApi(android.annotation.TargetApi)

Example 78 with RequiresApi

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);
}
Also used : DisplayManager(android.hardware.display.DisplayManager) IOException(java.io.IOException) DisplayMetrics(android.util.DisplayMetrics) File(java.io.File) MediaMuxer(android.media.MediaMuxer) Display(android.view.Display) RequiresApi(android.support.annotation.RequiresApi)

Example 79 with RequiresApi

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();
    }
}
Also used : MediaFormat(android.media.MediaFormat) IOException(java.io.IOException) RequiresApi(android.support.annotation.RequiresApi)

Example 80 with RequiresApi

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));
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager) Intent(android.content.Intent) RequiresApi(android.support.annotation.RequiresApi)

Aggregations

RequiresApi (android.support.annotation.RequiresApi)191 Intent (android.content.Intent)30 NotificationChannel (android.app.NotificationChannel)28 View (android.view.View)25 NotificationManager (android.app.NotificationManager)17 ViewGroup (android.view.ViewGroup)15 Allocation (android.renderscript.Allocation)14 Bitmap (android.graphics.Bitmap)13 RecyclerView (android.support.v7.widget.RecyclerView)13 ViewTreeObserver (android.view.ViewTreeObserver)12 WindowInsets (android.view.WindowInsets)12 TextView (android.widget.TextView)12 ActionBar (android.support.v7.app.ActionBar)11 Toolbar (android.support.v7.widget.Toolbar)11 Handler (android.os.Handler)10 StatFs (android.os.StatFs)9 Button (android.widget.Button)8 Cipher (javax.crypto.Cipher)8 SuppressLint (android.annotation.SuppressLint)7 Uri (android.net.Uri)7