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


Java MediaController类代码示例

本文整理汇总了Java中android.media.session.MediaController的典型用法代码示例。如果您正苦于以下问题:Java MediaController类的具体用法?Java MediaController怎么用?Java MediaController使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MediaController类属于android.media.session包,在下文中一共展示了MediaController类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onCreate

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public void onCreate(){
    registerReceiver(button,new IntentFilter(MEDIA_ACTION));
    mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
    try {
        mediaSessionManager.addOnActiveSessionsChangedListener(sessionListener, componentName);
        List<MediaController> controllers = mediaSessionManager.getActiveSessions(componentName);
        mediaController = pickController(controllers);
        if (mediaController != null) {
            mediaController.registerCallback(callback);
            meta = mediaController.getMetadata();
            updateMetadata();
        }
        online = true;
    } catch (SecurityException e) {

    }
}
 
开发者ID:jathak,项目名称:sflauncher,代码行数:19,代码来源:MediaListener.java

示例2: onStartCommand

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public int onStartCommand(Intent i, int startId, int i2){
    if(mediaController==null){
        try {
            List<MediaController> controllers = mediaSessionManager.getActiveSessions(componentName);
            mediaController = pickController(controllers);
            if (mediaController != null) {
                mediaController.registerCallback(callback);
                meta = mediaController.getMetadata();
                updateMetadata();
            }
        } catch (SecurityException e) {

        }
    }
    return START_STICKY;
}
 
开发者ID:jathak,项目名称:sflauncher,代码行数:18,代码来源:MediaListener.java

示例3: getMediaAppsFromControllers

import android.media.session.MediaController; //导入依赖的package包/类
static List<MediaAppEntry> getMediaAppsFromControllers(
        Collection<MediaController> controllers,
        PackageManager packageManager) {
    final List<MediaAppEntry> mediaApps = new ArrayList<>();
    for (final MediaController controller : controllers) {
        String packageName = controller.getPackageName();
        ApplicationInfo info;
        try {
            info = packageManager.getApplicationInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            // This should not happen. If we get a media session for a package, then the
            // package must be installed on the device.
            Log.e(TAG, "Unable to load package details", e);
            continue;
        }
        final Drawable icon = info.loadIcon(packageManager);
        final String name = info.loadLabel(packageManager).toString();
        mediaApps.add(MediaAppEntry.fromSessionToken(
                controller.getSessionToken(), name,
                packageName,
                icon
        ));
    }
    return mediaApps;
}
 
开发者ID:googlesamples,项目名称:android-media-controller,代码行数:26,代码来源:LaunchActivity.java

示例4: onSharedPreferenceChanged

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  if (key.startsWith("player.")) {
    final String packageName = key.substring(7);

    if (sharedPreferences.getBoolean(key, true)) {
      Log.d(TAG, "Player enabled, re-registering callbacks");
      onActiveSessionsChanged(mediaControllers);
    } else {
      Log.d(TAG, "Player disabled, stopping any current tracking");
      final Optional<MediaController> optionalController =
          Iterables.tryFind(
              mediaControllers, input -> input.getPackageName().equals(packageName));

      if (optionalController.isPresent()
          && controllerCallbacks.containsKey(optionalController.get())) {
        MediaController controller = optionalController.get();
        controller.unregisterCallback(controllerCallbacks.get(controller));
        playbackTracker.handleSessionTermination(controller.getPackageName());
        controllerCallbacks.remove(controller);
      }
    }
  }
}
 
开发者ID:peterjosling,项目名称:scroball,代码行数:25,代码来源:ListenerService.java

示例5: getView

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        itemState = MediaItemViewHolder.STATE_PLAYABLE;
        MediaController controller = ((Activity) getContext()).getMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                PlaybackState pbState = controller.getPlaybackState();
                if (pbState == null || pbState.getState() == PlaybackState.STATE_ERROR) {
                    itemState = MediaItemViewHolder.STATE_NONE;
                } else if (pbState.getState() == PlaybackState.STATE_PLAYING) {
                    itemState = MediaItemViewHolder.STATE_PLAYING;
                } else {
                    itemState = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 
开发者ID:mrinalgit-dev,项目名称:MrinalMusicPlayer,代码行数:27,代码来源:MediaBrowserFragment.java

示例6: shouldShowControls

import android.media.session.MediaController; //导入依赖的package包/类
/**
 * Check if the MediaSession is active and in a "playback-able" state
 * (not NONE and not STOPPED).
 *
 * @return true if the MediaSession's state requires playback controls to be visible.
 */
protected boolean shouldShowControls() {
    MediaController mediaController = getMediaController();
    if (mediaController == null ||
        mediaController.getMetadata() == null ||
        mediaController.getPlaybackState() == null) {
        return false;
    }
    switch (mediaController.getPlaybackState().getState()) {
        case PlaybackState.STATE_ERROR:
        case PlaybackState.STATE_NONE:
        case PlaybackState.STATE_STOPPED:
            return false;
        default:
            return true;
    }
}
 
开发者ID:mrinalgit-dev,项目名称:MrinalMusicPlayer,代码行数:23,代码来源:BaseActivity.java

示例7: connectToSession

import android.media.session.MediaController; //导入依赖的package包/类
private void connectToSession(MediaSession.Token token) {
    MediaController mediaController = new MediaController(this, token);
    setMediaController(mediaController);
    mediaController.registerCallback(mMediaControllerCallback);

    if (shouldShowControls()) {
        showPlaybackControls();
    } else {
        LogHelper.d(TAG, "connectionCallback.onConnected: " +
            "hiding controls because metadata is null");
        hidePlaybackControls();
    }

    if (mControlsFragment != null) {
        mControlsFragment.onConnected();
    }

    onMediaControllerConnected();
}
 
开发者ID:mrinalgit-dev,项目名称:MrinalMusicPlayer,代码行数:20,代码来源:BaseActivity.java

示例8: connectToSession

import android.media.session.MediaController; //导入依赖的package包/类
private void connectToSession(MediaSession.Token token) {
    MediaController mediaController = new MediaController(FullScreenPlayerActivity.this, token);
    if (mediaController.getMetadata() == null) {
        finish();
        return;
    }
    setMediaController(mediaController);
    mediaController.registerCallback(mCallback);
    PlaybackState state = mediaController.getPlaybackState();
    updatePlaybackState(state);
    MediaMetadata metadata = mediaController.getMetadata();
    if (metadata != null) {
        updateMediaDescription(metadata.getDescription());
        updateDuration(metadata);
    }
    updateProgress();
    if (state != null && (state.getState() == PlaybackState.STATE_PLAYING ||
            state.getState() == PlaybackState.STATE_BUFFERING)) {
        scheduleSeekbarUpdate();
    }
}
 
开发者ID:mrinalgit-dev,项目名称:MrinalMusicPlayer,代码行数:22,代码来源:FullScreenPlayerActivity.java

示例9: checkForUserVisibleErrors

import android.media.session.MediaController; //导入依赖的package包/类
private void checkForUserVisibleErrors(boolean forceError) {
    boolean showError = forceError;
    // If offline, message is about the lack of connectivity:
    if (!NetworkHelper.isOnline(getActivity())) {
        mErrorMessage.setText(R.string.error_no_connection);
        showError = true;
    } else {
        // otherwise, if state is ERROR and metadata!=null, use playback state error message:
        MediaController controller = getActivity().getMediaController();
        if (controller != null
            && controller.getMetadata() != null
            && controller.getPlaybackState().getState() == PlaybackState.STATE_ERROR
            && controller.getPlaybackState().getErrorMessage() != null) {
            mErrorMessage.setText(controller.getPlaybackState().getErrorMessage());
            showError = true;
        } else if (forceError) {
            // Finally, if the caller requested to show error, show a generic message:
            mErrorMessage.setText(R.string.error_loading_media);
            showError = true;
        }
    }
    mErrorView.setVisibility(showError ? View.VISIBLE : View.GONE);
    LogHelper.d(TAG, "checkForUserVisibleErrors. forceError=", forceError,
        " showError=", showError,
        " isOnline=", NetworkHelper.isOnline(getActivity()));
}
 
开发者ID:ErNaveen,项目名称:UniversalMusicPlayer,代码行数:27,代码来源:MediaBrowserFragment.java

示例10: getView

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int state = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        state = MediaItemViewHolder.STATE_PLAYABLE;
        MediaController controller = ((Activity) getContext()).getMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                if (controller.getPlaybackState().getState() ==
                        PlaybackState.STATE_PLAYING) {
                    state = MediaItemViewHolder.STATE_PLAYING;
                } else if (controller.getPlaybackState().getState() !=
                        PlaybackState.STATE_ERROR) {
                    state = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), state);
}
 
开发者ID:ErNaveen,项目名称:UniversalMusicPlayer,代码行数:26,代码来源:MediaBrowserFragment.java

示例11: onActiveSessionsChanged

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public void onActiveSessionsChanged(List<MediaController> controllers) {
    MediaController2Lollipop p = mMediaControllerRef.get();
    if (p == null) return;

    if (p.mMediaController != null) {
        for (MediaController controller : controllers) {
            if (p.mMediaController == controller) {
                // Current media controller is still alive.
                return;
            }
        }
    }

    MediaController mc = pickBestMediaController(controllers);
    if (mc != null) {
        p.setMediaController(mc);
    } else {
        p.clearMediaController(true);
    }
}
 
开发者ID:AChep,项目名称:AcDisplay,代码行数:22,代码来源:MediaController2Lollipop.java

示例12: run

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public void run(@NonNull MediaController2 mc) {
    final MediaController2Lollipop mcl = (MediaController2Lollipop) mc;
    final MediaController source = mcl.mMediaController;

    if (source != null && mToken.equals(source.getSessionToken())) {
        long now = SystemClock.elapsedRealtime();

        final MediaMetadata metadata = source.getMetadata();
        final PlaybackState playbackState = source.getPlaybackState();

        long delta = SystemClock.elapsedRealtime() - now;
        Log.i(TAG, "Got the new metadata & playback state in " + delta + " millis. "
                + "The media controller is " + source.getPackageName());

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mcl.updateMetadata(metadata);
                mcl.updatePlaybackState(playbackState);
            }
        });
    }
}
 
开发者ID:AChep,项目名称:AcDisplay,代码行数:25,代码来源:MediaController2Lollipop.java

示例13: answerUsingMediaServer

import android.media.session.MediaController; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void answerUsingMediaServer()
{
    Timber.d("Answering using media server method...");

    MediaSessionManager mediaSessionManager =  (MediaSessionManager) getCallModule().getService().getSystemService(Context.MEDIA_SESSION_SERVICE);

    try {
        List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions
                (new ComponentName(getCallModule().getService(), JellybeanNotificationListener.class));

        for (MediaController m : mediaControllerList) {
            if ("com.android.server.telecom".equals(m.getPackageName())) {
                Timber.d("Found telephony media controller!");
                m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
                break;
            }
        }
    } catch (SecurityException e) {
        Timber.e("Notification service not running!");
    }
}
 
开发者ID:matejdro,项目名称:PebbleDialer-Android,代码行数:23,代码来源:AnswerCallAction.java

示例14: pickController

import android.media.session.MediaController; //导入依赖的package包/类
private MediaController pickController(List<MediaController> controllers){
    for(int i=0;i<controllers.size();i++){
        MediaController mc = controllers.get(i);
        if(mc!=null&&mc.getPlaybackState()!=null&&
                mc.getPlaybackState().getState()==PlaybackState.STATE_PLAYING){
            return mc;
        }
    }
    if(controllers.size()>0) return controllers.get(0);
    return null;
}
 
开发者ID:jathak,项目名称:sflauncher,代码行数:12,代码来源:MediaListener.java

示例15: onActiveSessionsChanged

import android.media.session.MediaController; //导入依赖的package包/类
@Override
public void onActiveSessionsChanged(List<MediaController> controllers) {
    mediaController = pickController(controllers);
    if(mediaController==null)return;
    mediaController.registerCallback(callback);
    meta = mediaController.getMetadata();
    updateMetadata();
}
 
开发者ID:jathak,项目名称:sflauncher,代码行数:9,代码来源:MediaListener.java


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