本文整理汇总了Java中android.media.session.MediaSession类的典型用法代码示例。如果您正苦于以下问题:Java MediaSession类的具体用法?Java MediaSession怎么用?Java MediaSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MediaSession类属于android.media.session包,在下文中一共展示了MediaSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initMediaSession
import android.media.session.MediaSession; //导入依赖的package包/类
private void initMediaSession() {
mMediaSession = new MediaSession(mContext, MEDIA_SESSION_TAG);
mMediaSession.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
// Consume the media button event here. Should not send it to other apps.
return true;
}
});
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
if (!mMediaSession.isActive()) {
mMediaSession.setActive(true);
}
}
示例2: setupMediaSession
import android.media.session.MediaSession; //导入依赖的package包/类
private void setupMediaSession() {
ComponentName mediaButtonReceiverComponentName = new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mediaButtonReceiverComponentName);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
mediaSession = new MediaSessionCompat(this, "RetroMusicPlayer", mediaButtonReceiverComponentName, mediaButtonReceiverPendingIntent);
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onPlay() {
play();
}
@Override
public void onPause() {
pause();
}
@Override
public void onSkipToNext() {
playNextSong(true);
}
@Override
public void onSkipToPrevious() {
back(true);
}
@Override
public void onStop() {
quit();
}
@Override
public void onSeekTo(long pos) {
seek((int) pos);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
return MediaButtonIntentReceiver.handleIntent(MusicService.this, mediaButtonEvent);
}
});
mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
| MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
示例3: onCreate
import android.media.session.MediaSession; //导入依赖的package包/类
@Override
public void onCreate() {
Log.d (TAG, "onCreate() " + this) ;
super.onCreate();
// Start a new MediaSession
mediaSession = new MediaSession(this, "MuziKarMusicService");
mediaSession.setCallback(mediaSessionCallback);
mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
setSessionToken(mediaSession.getSessionToken());
playbackControlEventBroadcastReceiver = new PlaybackControlEventBroadcastReceiver(this);
audioPlaybackHelper = new AudioPlaybackHelper(getApplicationContext(), new AudioPlaybackHelper.PlayStateChangeCallback() {
@Override
public void onPlaybackStatusChanged(PlaybackState state) {
mediaSession.setPlaybackState(state);
playbackControlEventBroadcastReceiver.update(audioPlaybackHelper.getCurrentMedia(), state, getSessionToken());
}
});
carConnectStatusBroadcastReceiver = new CarConnectStatusBroadcastReceiver( this ) ;
}
示例4: convertToQueue
import android.media.session.MediaSession; //导入依赖的package包/类
private static List<MediaSession.QueueItem> convertToQueue(
Iterable<MediaMetadata> tracks, String... categories) {
List<MediaSession.QueueItem> queue = new ArrayList<>();
int count = 0;
for (MediaMetadata track : tracks) {
// We create a hierarchy-aware mediaID, so we know what the queue is about by looking
// at the QueueItem media IDs.
String hierarchyAwareMediaID = MediaIDHelper.createMediaID(
track.getDescription().getMediaId(), categories);
MediaMetadata trackCopy = new MediaMetadata.Builder(track)
.putString(MediaMetadata.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID)
.build();
// We don't expect queues to change after created, so we use the item index as the
// queueId. Any other number unique in the queue would work.
MediaSession.QueueItem item = new MediaSession.QueueItem(
trackCopy.getDescription(), count++);
queue.add(item);
}
return queue;
}
示例5: getRandomQueue
import android.media.session.MediaSession; //导入依赖的package包/类
/**
* Create a random queue.
*
* @param musicProvider the provider used for fetching music.
* @return list containing {@link MediaSession.QueueItem}'s
*/
public static List<MediaSession.QueueItem> getRandomQueue(MusicProvider musicProvider) {
List<MediaMetadata> result = new ArrayList<>();
for (String genre: musicProvider.getGenres()) {
Iterable<MediaMetadata> tracks = musicProvider.getMusicsByGenre(genre);
for (MediaMetadata track: tracks) {
if (ThreadLocalRandom.current().nextBoolean()) {
result.add(track);
}
}
}
LogHelper.d(TAG, "getRandomQueue: result.size=", result.size());
Collections.shuffle(result);
return convertToQueue(result, MEDIA_ID_MUSICS_BY_SEARCH, "random");
}
示例6: equalsQueue
import android.media.session.MediaSession; //导入依赖的package包/类
private boolean equalsQueue(List<MediaSession.QueueItem> list1,
List<MediaSession.QueueItem> list2) {
if (list1 == list2) {
return true;
}
if (list1 == null || list2 == null) {
return false;
}
if (list1.size() != list2.size()) {
return false;
}
for (int i=0; i<list1.size(); i++) {
if (list1.get(i).getQueueId() != list2.get(i).getQueueId()) {
return false;
}
if (!TextUtils.equals(list1.get(i).getDescription().getMediaId(),
list2.get(i).getDescription().getMediaId())) {
return false;
}
}
return true;
}
示例7: connectToSession
import android.media.session.MediaSession; //导入依赖的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();
}
示例8: connectToSession
import android.media.session.MediaSession; //导入依赖的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();
}
}
示例9: updateMediaSession
import android.media.session.MediaSession; //导入依赖的package包/类
public static void updateMediaSession(boolean updateMetadata,
MediaMetadata.Builder mediaBuilder,
final MediaSession mediaSession,
final int playbackState,
final long position,
final float speed) {
if (mediaSession.isActive()) {
if (updateMetadata) {
mediaSession.setMetadata(mediaBuilder.build());
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder();
stateBuilder.setState(playbackState,
position,
speed);
mediaSession.setPlaybackState(stateBuilder.build());
}
}
示例10: onCreate
import android.media.session.MediaSession; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
// Start a new MediaSession
mSession = new MediaSession(this, "MusicService");
mSession.setCallback(mCallback);
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
setSessionToken(mSession.getSessionToken());
final MediaNotificationManager mediaNotificationManager = new MediaNotificationManager(this);
mPlayback = new PlaybackManager(this, new PlaybackManager.Callback() {
@Override
public void onPlaybackStatusChanged(PlaybackState state) {
mSession.setPlaybackState(state);
mediaNotificationManager.update(mPlayback.getCurrentMedia(), state, getSessionToken());
}
});
}
示例11: create
import android.media.session.MediaSession; //导入依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected MediaSession create() throws Exception {
final MediaSession newMediaSession = new MediaSession(
PlaybackService.this,
mediaSessionTag);
newMediaSession.setFlags(
MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
newMediaSession.setCallback(new MediaSessionCallbackReceiver(PlaybackService.this));
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(remoteControlReceiver.getObject());
final PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(PlaybackService.this, 0, mediaButtonIntent, 0);
newMediaSession.setMediaButtonReceiver(mediaPendingIntent);
return newMediaSession;
}
示例12: updatePlaylist
import android.media.session.MediaSession; //导入依赖的package包/类
@Override
public void updatePlaylist(List<DownloadFile> playlist) {
List<MediaSession.QueueItem> queue = new ArrayList<>();
for(DownloadFile file: playlist) {
Entry entry = file.getSong();
MediaDescription description = new MediaDescription.Builder()
.setMediaId(entry.getId())
.setTitle(entry.getTitle())
.setSubtitle(entry.getAlbumDisplay())
.build();
MediaSession.QueueItem item = new MediaSession.QueueItem(description, entry.getId().hashCode());
queue.add(item);
}
mediaSession.setQueue(queue);
currentQueue = playlist;
}
示例13: setup
import android.media.session.MediaSession; //导入依赖的package包/类
@Override
void setup() {
mHandlerThread = new HandlerThread(getClass().getName(), Process.THREAD_PRIORITY_BACKGROUND);
mHandlerThread.start();
mMediaSession = new MediaSession(mService, mService.getClass().getName());
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS|MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setCallback(new Callback(), new Handler(mHandlerThread.getLooper()));
mMediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(
mService,
1,
new Intent(mService, MediaButtonIntentReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT
));
mMediaSession.setSessionActivity(PendingIntent.getActivity(
mService,
2,
NavUtils.makeLauncherIntent(mService),
PendingIntent.FLAG_UPDATE_CURRENT
));
}
示例14: buildQueueList
import android.media.session.MediaSession; //导入依赖的package包/类
@DebugLog @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<MediaSession.QueueItem> buildQueueList(Context context, long[] songs) {
List<MediaSession.QueueItem> list = new ArrayList<>(15);
if (songs.length == 0) return list;
final OrderPreservingCursor c = new OrderPreservingCursor(context, songs,
MusicProvider.RECENTS_URI, Projections.RECENT_SONGS, "", null);
c.moveToFirst();
int ii=0;
do {
list.add(new MediaSession.QueueItem(
new MediaDescription.Builder()
.setTitle(c.getString(c.getColumnIndex(MusicStore.Cols.NAME)))
.setSubtitle(c.getString(c.getColumnIndex(MusicStore.Cols.ARTIST_NAME)))
.setMediaId(c.getString(c.getColumnIndex(MusicStore.Cols.IDENTITY)))
.build(),
songs[ii]
));
} while (c.moveToNext() && ++ii<15);
c.close();
return list;
}
示例15: onCreate
import android.media.session.MediaSession; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mMediaSession = new MediaSession(this, TAG);
setUpCallBack();
mNotifications = new ArrayList<Botification>();
final IntentFilter filter = new IntentFilter();
filter.addAction(SERVICECMD);
// Attach the broadcast listener
mSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
mHandler = new Handler(){
public void handleMessage(Message msg){
resetNotify(true);
}
};
}