use of org.videolan.medialibrary.media.MediaWrapper in project vlc-android by videolan.
the class VideoGridFragment method handleContextItemSelected.
protected boolean handleContextItemSelected(MenuItem menu, final int position) {
if (position >= mAdapter.getItemCount())
return false;
final MediaWrapper media = mAdapter.getItem(position);
if (media == null)
return false;
switch(menu.getItemId()) {
case R.id.video_list_play_from_start:
playVideo(media, true);
return true;
case R.id.video_list_play_audio:
playAudio(media);
return true;
case R.id.video_list_play_all:
List<MediaWrapper> playList = new ArrayList<>();
MediaUtils.openList(getActivity(), playList, mAdapter.getListWithPosition(playList, position));
return true;
case R.id.video_list_info:
showInfoDialog(media);
return true;
case R.id.video_list_delete:
removeVideo(media);
return true;
case R.id.video_group_play:
MediaUtils.openList(getActivity(), ((MediaGroup) media).getAll(), 0);
return true;
case R.id.video_list_append:
if (media instanceof MediaGroup)
mService.append(((MediaGroup) media).getAll());
else
mService.append(media);
return true;
case R.id.video_download_subtitles:
MediaUtils.getSubs(getActivity(), media);
return true;
}
return false;
}
use of org.videolan.medialibrary.media.MediaWrapper in project vlc-android by videolan.
the class VideoListAdapter method getListWithPosition.
int getListWithPosition(List<MediaWrapper> list, int position) {
MediaWrapper mw;
int offset = 0;
for (int i = 0; i < getItemCount(); ++i) {
mw = getDataset().get(i);
if (mw instanceof MediaGroup) {
for (MediaWrapper item : ((MediaGroup) mw).getAll()) list.add(item);
if (i < position)
offset += ((MediaGroup) mw).size() - 1;
} else
list.add(mw);
}
return position + offset;
}
use of org.videolan.medialibrary.media.MediaWrapper in project vlc-android by videolan.
the class VideoPlayerActivity method loadMedia.
/**
* External extras:
* - position (long) - position of the video to start with (in ms)
* - subtitles_location (String) - location of a subtitles file to load
* - from_start (boolean) - Whether playback should start from start or from resume point
* - title (String) - video title, will be guessed from file if not set.
*/
@SuppressLint("SdCardPath")
@TargetApi(12)
@SuppressWarnings({ "unchecked" })
protected void loadMedia() {
if (mService == null)
return;
mUri = null;
mIsPlaying = false;
String title = null;
boolean fromStart = false;
String itemTitle = null;
int positionInPlaylist = -1;
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
long savedTime = 0L;
final boolean hasMedia = mService.hasMedia();
final boolean isPlaying = mService.isPlaying();
/*
* If the activity has been paused by pressing the power button, then
* pressing it again will show the lock screen.
* But onResume will also be called, even if vlc-android is still in
* the background.
* To workaround this, pause playback if the lockscreen is displayed.
*/
final KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(KEYGUARD_SERVICE);
if (km != null && km.inKeyguardRestrictedInputMode())
mWasPaused = true;
if (mWasPaused && BuildConfig.DEBUG)
Log.d(TAG, "Video was previously paused, resuming in paused mode");
if (intent.getData() != null)
mUri = intent.getData();
if (extras != null) {
if (intent.hasExtra(Constants.PLAY_EXTRA_ITEM_LOCATION))
mUri = extras.getParcelable(Constants.PLAY_EXTRA_ITEM_LOCATION);
fromStart = extras.getBoolean(Constants.PLAY_EXTRA_FROM_START, false);
// Consume fromStart option after first use to prevent
// restarting again when playback is paused.
intent.putExtra(Constants.PLAY_EXTRA_FROM_START, false);
mAskResume &= !fromStart;
// position passed in by intent (ms)
savedTime = fromStart ? 0L : extras.getLong(Constants.PLAY_EXTRA_START_TIME);
if (!fromStart && savedTime == 0L)
savedTime = extras.getInt(Constants.PLAY_EXTRA_START_TIME);
positionInPlaylist = extras.getInt(Constants.PLAY_EXTRA_OPENED_POSITION, -1);
if (intent.hasExtra(Constants.PLAY_EXTRA_SUBTITLES_LOCATION))
synchronized (mSubtitleSelectedFiles) {
mSubtitleSelectedFiles.add(extras.getString(Constants.PLAY_EXTRA_SUBTITLES_LOCATION));
}
if (intent.hasExtra(Constants.PLAY_EXTRA_ITEM_TITLE))
itemTitle = extras.getString(Constants.PLAY_EXTRA_ITEM_TITLE);
}
final boolean restorePlayback = hasMedia && mService.getCurrentMediaWrapper().getUri().equals(mUri);
MediaWrapper openedMedia = null;
final boolean resumePlaylist = mService.isValidIndex(positionInPlaylist);
final boolean continueplayback = isPlaying && (restorePlayback || positionInPlaylist == mService.getCurrentMediaPosition());
if (resumePlaylist) {
// Provided externally from AudioService
if (BuildConfig.DEBUG)
Log.d(TAG, "Continuing playback from PlaybackService at index " + positionInPlaylist);
openedMedia = mService.getMedias().get(positionInPlaylist);
if (openedMedia == null) {
encounteredError();
return;
}
itemTitle = openedMedia.getTitle();
updateSeekable(mService.isSeekable());
updatePausable(mService.isPausable());
}
mService.addCallback(this);
if (mUri != null) {
MediaWrapper media = null;
if (!continueplayback) {
if (!resumePlaylist) {
// restore last position
media = mMedialibrary.getMedia(mUri);
if (media == null && TextUtils.equals(mUri.getScheme(), "file") && mUri.getPath() != null && mUri.getPath().startsWith("/sdcard")) {
mUri = FileUtils.convertLocalUri(mUri);
media = mMedialibrary.getMedia(mUri);
}
if (media != null && media.getId() != 0L && media.getTime() == 0L)
media.setTime(media.getMetaLong(MediaWrapper.META_PROGRESS));
} else
media = openedMedia;
if (media != null) {
// in media library
if (mAskResume && !fromStart && positionInPlaylist == -1 && media.getTime() > 0) {
showConfirmResumeDialog();
return;
}
mLastAudioTrack = media.getAudioTrack();
mLastSpuTrack = media.getSpuTrack();
} else if (!fromStart) {
// not in media library
if (mAskResume && savedTime > 0L) {
showConfirmResumeDialog();
return;
} else {
long rTime = mSettings.getLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
if (rTime > 0) {
if (mAskResume) {
showConfirmResumeDialog();
return;
} else {
mSettings.edit().putLong(PreferencesActivity.VIDEO_RESUME_TIME, -1).apply();
savedTime = rTime;
}
}
}
}
}
// Start playback & seek
/* prepare playback */
final boolean medialoaded = media != null;
if (!medialoaded) {
if (hasMedia)
media = mService.getCurrentMediaWrapper();
else
media = new MediaWrapper(mUri);
}
if (mWasPaused)
media.addFlags(MediaWrapper.MEDIA_PAUSED);
if (intent.hasExtra(Constants.PLAY_DISABLE_HARDWARE))
media.addFlags(MediaWrapper.MEDIA_NO_HWACCEL);
media.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO);
media.addFlags(MediaWrapper.MEDIA_VIDEO);
// Set resume point
if (!continueplayback) {
if (!fromStart && savedTime <= 0L && media.getTime() > 0L)
savedTime = media.getTime();
if (savedTime > 0L)
mService.saveTimeToSeek(savedTime);
}
// Handle playback
if (resumePlaylist) {
if (continueplayback) {
if (mDisplayManager.isPrimary())
mService.flush();
onPlaying();
} else
mService.playIndex(positionInPlaylist);
} else if (medialoaded)
mService.load(media);
else
mService.loadUri(mUri);
// Get possible subtitles
getSubtitles();
// Get the title
if (itemTitle == null && !TextUtils.equals(mUri.getScheme(), "content"))
title = mUri.getLastPathSegment();
} else if (mService.hasMedia() && mService.hasRenderer()) {
onPlaying();
} else {
mService.loadLastPlaylist(Constants.PLAYLIST_TYPE_VIDEO);
}
if (itemTitle != null)
title = itemTitle;
mTitle.setText(title);
if (mWasPaused) {
// XXX: Workaround to update the seekbar position
mForcedTime = savedTime;
mForcedTime = -1;
showOverlay(true);
}
enableSubs();
}
use of org.videolan.medialibrary.media.MediaWrapper in project vlc-android by videolan.
the class VideoPlayerActivity method switchToPopup.
@TargetApi(Build.VERSION_CODES.N)
public void switchToPopup() {
final MediaWrapper mw = mService != null ? mService.getCurrentMediaWrapper() : null;
if (mw == null)
return;
if (AndroidDevices.hasPiP) {
if (AndroidUtil.isOOrLater)
try {
final int height = mVideoHeight != 0 ? mVideoHeight : mw.getHeight();
final int width = Math.min(mVideoWidth != 0 ? mVideoWidth : mw.getWidth(), (int) (height * 2.39f));
enterPictureInPictureMode(new PictureInPictureParams.Builder().setAspectRatio(new Rational(width, height)).build());
} catch (IllegalArgumentException e) {
// Fallback with default parameters
// noinspection deprecation
enterPictureInPictureMode();
}
else {
// noinspection deprecation
enterPictureInPictureMode();
}
} else {
if (Permissions.canDrawOverlays(this)) {
mSwitchingView = true;
mSwitchToPopup = true;
if (mService != null && !mService.isPlaying())
mw.addFlags(MediaWrapper.MEDIA_PAUSED);
cleanUI();
exitOK();
} else
Permissions.checkDrawOverlaysPermission(this);
}
}
use of org.videolan.medialibrary.media.MediaWrapper in project vlc-android by videolan.
the class BrowserProvider method browse.
@WorkerThread
public static List<MediaBrowserCompat.MediaItem> browse(String parentId) {
List<MediaBrowserCompat.MediaItem> results = new ArrayList<>();
MediaLibraryItem[] list = null;
boolean limitSize = false;
Resources res = VLCApplication.getAppResources();
// Extensions
if (parentId.startsWith(ExtensionsManager.EXTENSION_PREFIX)) {
if (sExtensionServiceConnection == null) {
createExtensionServiceConnection();
try {
extensionLock.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (sExtensionServiceConnection == null)
return null;
String[] data = parentId.split("_");
int index = Integer.valueOf(data[1]);
extensionItems.clear();
if (data.length == 2) {
// case extension root
sExtensionManagerService.connectService(index);
} else {
// case sub-directory
String stringId = parentId.replace(ExtensionsManager.EXTENSION_PREFIX + "_" + String.valueOf(index) + "_", "");
sExtensionManagerService.browse(stringId);
}
try {
extensionLock.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
results = extensionItems;
} else {
switch(parentId) {
case ID_ROOT:
MediaDescriptionCompat.Builder item = new MediaDescriptionCompat.Builder();
// List of Extensions
List<ExtensionListing> extensions = ExtensionsManager.getInstance().getExtensions(VLCApplication.getAppContext(), true);
for (int i = 0; i < extensions.size(); i++) {
ExtensionListing extension = extensions.get(i);
if (extension.androidAutoEnabled() && PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext()).getBoolean(ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName() + "_" + ExtensionsManager.ANDROID_AUTO_SUFFIX, false)) {
item.setMediaId(ExtensionsManager.EXTENSION_PREFIX + "_" + String.valueOf(i)).setTitle(extension.title());
int iconRes = extension.menuIcon();
Bitmap b = null;
Resources extensionRes;
if (iconRes != 0) {
try {
extensionRes = VLCApplication.getAppContext().getPackageManager().getResourcesForApplication(extension.componentName().getPackageName());
b = BitmapFactory.decodeResource(extensionRes, iconRes);
} catch (PackageManager.NameNotFoundException e) {
}
}
if (b != null)
item.setIconBitmap(b);
else
try {
b = ((BitmapDrawable) VLCApplication.getAppContext().getPackageManager().getApplicationIcon(extension.componentName().getPackageName())).getBitmap();
item.setIconBitmap(b);
} catch (PackageManager.NameNotFoundException e) {
b = BitmapFactory.decodeResource(res, R.drawable.icon);
item.setIconBitmap(b);
}
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
}
}
// Last added
item = new MediaDescriptionCompat.Builder().setMediaId(ID_LAST_ADDED).setTitle(res.getString(R.string.last_added_media)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_history_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// History
item = new MediaDescriptionCompat.Builder().setMediaId(ID_HISTORY).setTitle(res.getString(R.string.history)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_history_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// Playlists
item.setMediaId(ID_PLAYLISTS).setTitle(res.getString(R.string.playlists)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_playlist_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// Artists
item.setMediaId(ID_ARTISTS).setTitle(res.getString(R.string.artists)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_artist_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// Albums
item.setMediaId(ID_ALBUMS).setTitle(res.getString(R.string.albums)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_album_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// Songs
item.setMediaId(ID_SONGS).setTitle(res.getString(R.string.songs)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_audio_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
// Genres
item.setMediaId(ID_GENRES).setTitle(res.getString(R.string.genres)).setIconUri(Uri.parse(BASE_DRAWABLE_URI + "ic_auto_genre_normal"));
results.add(new MediaBrowserCompat.MediaItem(item.build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
return results;
case ID_LAST_ADDED:
limitSize = true;
list = VLCApplication.getMLInstance().getRecentAudio();
break;
case ID_HISTORY:
limitSize = true;
list = VLCApplication.getMLInstance().lastMediaPlayed();
break;
case ID_ARTISTS:
list = VLCApplication.getMLInstance().getArtists(VLCApplication.getSettings().getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false));
break;
case ID_ALBUMS:
list = VLCApplication.getMLInstance().getAlbums();
break;
case ID_GENRES:
list = VLCApplication.getMLInstance().getGenres();
break;
case ID_PLAYLISTS:
list = VLCApplication.getMLInstance().getPlaylists();
break;
case ID_SONGS:
list = VLCApplication.getMLInstance().getAudio();
break;
default:
String[] idSections = parentId.split("_");
Medialibrary ml = VLCApplication.getMLInstance();
long id = Long.parseLong(idSections[1]);
switch(idSections[0]) {
case ARTIST_PREFIX:
list = ml.getArtist(id).getAlbums();
break;
case GENRE_PREFIX:
list = ml.getGenre(id).getAlbums();
break;
}
}
if (list != null) {
MediaDescriptionCompat.Builder item = new MediaDescriptionCompat.Builder();
for (MediaLibraryItem libraryItem : list) {
if (libraryItem == null || (libraryItem.getItemType() == MediaLibraryItem.TYPE_MEDIA && ((MediaWrapper) libraryItem).getType() != MediaWrapper.TYPE_AUDIO))
continue;
Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(libraryItem.getArtworkMrl()), 256);
if (cover == null)
cover = DEFAULT_AUDIO_COVER;
item.setTitle(libraryItem.getTitle()).setMediaId(generateMediaId(libraryItem));
item.setIconBitmap(cover);
if (libraryItem.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
item.setMediaUri(((MediaWrapper) libraryItem).getUri()).setSubtitle(MediaUtils.getMediaSubtitle((MediaWrapper) libraryItem));
} else
item.setSubtitle(libraryItem.getDescription());
boolean playable = libraryItem.getItemType() == MediaLibraryItem.TYPE_MEDIA || libraryItem.getItemType() == MediaLibraryItem.TYPE_ALBUM || libraryItem.getItemType() == MediaLibraryItem.TYPE_PLAYLIST;
results.add(new MediaBrowserCompat.MediaItem(item.build(), playable ? MediaBrowserCompat.MediaItem.FLAG_PLAYABLE : MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
if (limitSize && results.size() == MAX_HISTORY_SIZE)
break;
}
}
}
if (results.isEmpty()) {
MediaDescriptionCompat.Builder mediaItem = new MediaDescriptionCompat.Builder();
mediaItem.setMediaId(DUMMY);
mediaItem.setTitle(VLCApplication.getAppContext().getString(R.string.search_no_result));
results.add(new MediaBrowserCompat.MediaItem(mediaItem.build(), 0));
}
return results;
}
Aggregations