private void next() { int index = mMediaList.indexOf(mCurrentMedia); mPrevious.push(mCurrentMedia); if (mRepeating == RepeatType.Once) mCurrentMedia = mMediaList.get(index); else if (mShuffling && mPrevious.size() < mMediaList.size()) { while (mPrevious.contains( mCurrentMedia = mMediaList.get((int) (Math.random() * mMediaList.size())))) ; } else if (!mShuffling && index < mMediaList.size() - 1) { mCurrentMedia = mMediaList.get(index + 1); } else { if (mRepeating == RepeatType.All && mMediaList.size() > 0) mCurrentMedia = mMediaList.get(0); else { stop(); return; } } if (mLibVLCPlaylistActive) { if (mRepeating == RepeatType.None) mLibVLC.next(); else if (mRepeating == RepeatType.Once) mLibVLC.playIndex(index); else mLibVLC.playIndex(mMediaList.indexOf(mCurrentMedia)); } else { mLibVLC.readMedia(mCurrentMedia.getLocation(), true); } mHandler.sendEmptyMessage(SHOW_PROGRESS); setUpRemoteControlClient(); showNotification(); updateWidget(this); updateRemoteControlClientMetadata(); saveCurrentMedia(); }
@Override public void load( List<String> mediaPathList, int position, boolean libvlcBacked, boolean noVideo) throws RemoteException { mLibVLCPlaylistActive = libvlcBacked; Log.v( TAG, "Loading position " + ((Integer) position).toString() + " in " + mediaPathList.toString()); mEventManager.addHandler(mEventHandler); mMediaList.clear(); mPrevious.clear(); if (mLibVLCPlaylistActive) { for (int i = 0; i < mediaPathList.size(); i++) mMediaList.add(new Media(mediaPathList.get(i), i)); } else { DatabaseManager db = DatabaseManager.getInstance(AudioService.this); for (int i = 0; i < mediaPathList.size(); i++) { String path = mediaPathList.get(i); Media media = db.getMedia(AudioService.this, path); if (media == null) { Log.v(TAG, "Creating on-the-fly Media object for " + path); media = new Media(path, false); } mMediaList.add(media); } } if (mMediaList.size() > position) { mCurrentMedia = mMediaList.get(position); } if (mCurrentMedia != null) { if (mLibVLCPlaylistActive) { mLibVLC.playIndex(position); } else { mLibVLC.readMedia(mCurrentMedia.getLocation(), noVideo); } setUpRemoteControlClient(); showNotification(); updateWidget(AudioService.this); updateRemoteControlClientMetadata(); } AudioService.this.saveMediaList(); AudioService.this.saveCurrentMedia(); }
@Override public void showWithoutParse(String URI) throws RemoteException { Log.v(TAG, "Showing playing URI " + URI); // Show an URI without interrupting/losing the current stream if (!mLibVLC.isPlaying()) return; mEventManager.addHandler(mEventHandler); mMediaList.clear(); mPrevious.clear(); // Prevent re-parsing the media, which would mean losing the connection mCurrentMedia = new Media( getApplicationContext(), URI, 0, 0, Media.TYPE_AUDIO, null, URI, VLCApplication.getAppContext().getString(R.string.unknown_artist), VLCApplication.getAppContext().getString(R.string.unknown_genre), VLCApplication.getAppContext().getString(R.string.unknown_album), 0, 0, "", -1, -1); mMediaList.add(mCurrentMedia); // Notify everyone mHandler.sendEmptyMessage(SHOW_PROGRESS); showNotification(); executeUpdate(); }
private void play() { if (mCurrentMedia != null) { setUpRemoteControlClient(); mLibVLC.play(); mHandler.sendEmptyMessage(SHOW_PROGRESS); showNotification(); updateWidget(this); } }
private void previous() { int index = mMediaList.indexOf(mCurrentMedia); if (mPrevious.size() > 0) mCurrentMedia = mPrevious.pop(); else if (index > 0) mCurrentMedia = mMediaList.get(index - 1); else return; if (mLibVLCPlaylistActive) { if (mRepeating == RepeatType.None) mLibVLC.previous(); else if (mRepeating == RepeatType.Once) mLibVLC.playIndex(index); else mLibVLC.playIndex(mMediaList.indexOf(mCurrentMedia)); } else { mLibVLC.readMedia(mCurrentMedia.getLocation(), true); } mHandler.sendEmptyMessage(SHOW_PROGRESS); setUpRemoteControlClient(); showNotification(); updateWidget(this); updateRemoteControlClientMetadata(); saveCurrentMedia(); }
private VlcMediaPlayer(PlayerEventProcessor processor) { Log.v(TAG, Logging.getCurrentMethodName(processor)); mEventsProcessor = processor; try { mLibVlcInstance = LibVLC.getInstance(); } catch (LibVlcException e) { e.printStackTrace(); mEventsProcessor.onError(); } EventManager.getInstance().addHandler(mVlcEventHandler); }
private void stop() { mLibVLC.stop(); mEventManager.removeHandler(mEventHandler); setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); mCurrentMedia = null; mMediaList.clear(); mPrevious.clear(); mHandler.removeMessages(SHOW_PROGRESS); hideNotification(); executeUpdate(); changeAudioFocus(false); }
@Override public void onCreate() { super.onCreate(); // Get libVLC instance try { mLibVLC = LibVLC.getInstance(); } catch (LibVlcException e) { e.printStackTrace(); } Thread.setDefaultUncaughtExceptionHandler(new VlcCrashHandler()); mCallback = new HashMap<IAudioServiceCallback, Integer>(); mMediaList = new ArrayList<Media>(); mPrevious = new Stack<Media>(); mEventManager = EventManager.getInstance(); mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(), RemoteControlClientReceiver.class.getName()); // Make sure the audio player will acquire a wake-lock while playing. If we don't do // that, the CPU might go to sleep while the song is playing, causing playback to stop. PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); IntentFilter filter = new IntentFilter(); filter.setPriority(Integer.MAX_VALUE); filter.addAction(ACTION_REMOTE_BACKWARD); filter.addAction(ACTION_REMOTE_PLAYPAUSE); filter.addAction(ACTION_REMOTE_PLAY); filter.addAction(ACTION_REMOTE_PAUSE); filter.addAction(ACTION_REMOTE_STOP); filter.addAction(ACTION_REMOTE_FORWARD); filter.addAction(ACTION_REMOTE_LAST_PLAYLIST); filter.addAction(Intent.ACTION_HEADSET_PLUG); filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY); filter.addAction(VLCApplication.SLEEP_INTENT); registerReceiver(serviceReceiver, filter); final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); boolean stealRemoteControl = pref.getBoolean("steal_remote_control", false); if (!Util.isFroyoOrLater() || stealRemoteControl) { /* Backward compatibility for API 7 */ filter = new IntentFilter(); if (stealRemoteControl) filter.setPriority(Integer.MAX_VALUE); filter.addAction(Intent.ACTION_MEDIA_BUTTON); mRemoteControlClientReceiver = new RemoteControlClientReceiver(); registerReceiver(mRemoteControlClientReceiver, filter); } AudioUtil.prepareCacheFolder(this); }
private void updateWidget(Context context) { Log.d(TAG, "Updating widget"); Intent i = new Intent(); i.setClassName(WIDGET_PACKAGE, WIDGET_CLASS); i.setAction(ACTION_WIDGET_UPDATE); if (mCurrentMedia != null) { i.putExtra("title", mCurrentMedia.getTitle()); i.putExtra("artist", mCurrentMedia.getArtist()); } else { i.putExtra("title", "VLC mini player"); i.putExtra("artist", ""); } i.putExtra("isplaying", mLibVLC.isPlaying()); Bitmap cover = mCurrentMedia != null ? AudioUtil.getCover(this, mCurrentMedia, 64) : null; i.putExtra("cover", cover); sendBroadcast(i); }
@Override public float getRate() throws RemoteException { return mLibVLC.getRate(); }
public void onStopPlayback() { Log.v(TAG, Logging.getCurrentMethodName()); mLatestCommand = FromLogicToPlayer.STOP; mLibVlcInstance.stop(); }
public void detachSurface() { Log.v(TAG, Logging.getCurrentMethodName()); mLibVlcInstance.detachSurface(); }
@Override public void setTime(long time) throws RemoteException { mLibVLC.setTime(time); }
@Override public int getLength() throws RemoteException { return (int) mLibVLC.getLength(); }
public boolean isPlaying() { return mLibVlcInstance.isPlaying(); }
public void onPausePlayback() { Log.v(TAG, Logging.getCurrentMethodName()); mLatestCommand = FromLogicToPlayer.PAUSE; mLibVlcInstance.pause(); }
@Override public boolean isPlaying() throws RemoteException { return mLibVLC.isPlaying(); }
public void onResumePlayback() { Log.v(TAG, Logging.getCurrentMethodName()); mLatestCommand = FromLogicToPlayer.RESUME; mLibVlcInstance.play(); }
/** * @param n Node to populate * @param path Path to populate * @param depth Depth of iteration (0 = 1st level of nesting, 1 = 2 level of nesting, etc) */ private void populateNode(DirectoryAdapter.Node n, String path, int depth) { if (path == null) { // We're on the storage list String storages[] = Util.getMediaDirectories(); for (String storage : storages) { File f = new File(storage); DirectoryAdapter.Node child = new DirectoryAdapter.Node(f.getName(), getVisibleName(f)); child.isFile = false; this.populateNode(child, storage, 0); n.addChildNode(child); } return; } File file = new File(path); if (!file.exists() || !file.isDirectory()) return; ArrayList<String> files = new ArrayList<String>(); LibVLC.nativeReadDirectory(path, files); StringBuilder sb = new StringBuilder(100); /* If no sub-directories or I/O error don't crash */ if (files == null || files.size() < 1) { // return } else { for (int i = 0; i < files.size(); i++) { String filename = files.get(i); /* Avoid infinite loop */ if (filename.equals(".") || filename.equals("..") || filename.startsWith(".")) continue; DirectoryAdapter.Node nss = new DirectoryAdapter.Node(filename); nss.isFile = false; sb.append(path); sb.append("/"); sb.append(filename); String newPath = sb.toString(); sb.setLength(0); // Don't try to go beyond depth 10 as a safety measure. if (LibVLC.nativeIsPathDirectory(newPath) && depth < 10) { ArrayList<String> files_int = new ArrayList<String>(); LibVLC.nativeReadDirectory(newPath, files_int); if (files_int.size() < 8) { /* Optimisation: If there are more than 8 sub-folders, don't scan each one, otherwise when scaled it is very slow to load */ String mCurrentDir_old = mCurrentDir; mCurrentDir = path; this.populateNode(nss, newPath, depth + 1); mCurrentDir = mCurrentDir_old; } } else { if (acceptedPath(newPath)) nss.setIsFile(); else continue; } n.addChildNode(nss); } Collections.sort(n.children); } DirectoryAdapter.Node up = new DirectoryAdapter.Node(".."); n.children.add(0, up); }
/** * Played video will be rendered to the provided surface * * @param surface * @param scaleCallback * @param width * @param height */ public void attachSurface( Surface surface, SurfaceScaleCallback scaleCallback, int width, int height) { Log.v(TAG, Logging.getCurrentMethodName()); mLibVlcInstance.attachSurface(surface, scaleCallback, width, height); }
private void pause() { setUpRemoteControlClient(); mHandler.removeMessages(SHOW_PROGRESS); // hideNotification(); <-- see event handler mLibVLC.pause(); }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); int state = intent.getIntExtra("state", 0); if (mLibVLC == null) { Log.w(TAG, "Intent received, but VLC is not loaded, skipping."); return; } // skip all headsets events if there is a call TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (telManager != null && telManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) return; /* * Launch the activity if needed */ if (action.startsWith(ACTION_REMOTE_GENERIC) && !mLibVLC.isPlaying() && mCurrentMedia == null) { Intent iVlc = new Intent(context, MainActivity.class); iVlc.putExtra(START_FROM_NOTIFICATION, true); iVlc.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(iVlc); } /* * Remote / headset control events */ if (action.equalsIgnoreCase(ACTION_REMOTE_PLAYPAUSE)) { if (mLibVLC.isPlaying() && mCurrentMedia != null) pause(); else if (!mLibVLC.isPlaying() && mCurrentMedia != null) play(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_PLAY)) { if (!mLibVLC.isPlaying() && mCurrentMedia != null) play(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_PAUSE)) { if (mLibVLC.isPlaying() && mCurrentMedia != null) pause(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_BACKWARD)) { previous(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_STOP)) { stop(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_FORWARD)) { next(); } else if (action.equalsIgnoreCase(ACTION_REMOTE_LAST_PLAYLIST)) { loadLastPlaylist(); } /* * headset plug events */ if (mDetectHeadset) { if (action.equalsIgnoreCase(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { Log.i(TAG, "Headset Removed."); if (mLibVLC.isPlaying() && mCurrentMedia != null) pause(); } else if (action.equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG) && state != 0) { Log.i(TAG, "Headset Inserted."); if (!mLibVLC.isPlaying() && mCurrentMedia != null) play(); } } /* * Sleep */ if (action.equalsIgnoreCase(VLCApplication.SLEEP_INTENT)) { stop(); } }