Playing audio from MediaStore on a Media Player Android - android

Is there a way to play audio obtained from MediaStore through the use of the MediaPLayer, Or am I going in the completely wrong direction? I've looked and MediaStore.Audio so far but nothing is really helping me. I just need to know if I'm on the right track

First, I assume you have basic knowledge of querying a ContentProvider and working with Cursors. If you dont, I suggest you research it here
Once you have a basic knowledge of how to use a ContentProvider, query the URI MediaStore.Audio.Media.EXTERNAL_CONTENT_URI for the column Audio.Media.DATA, along with any other fields you need.
Let's say you put the returned cursor in yourCursor
String path = yourCursor.getString(getColumnIndex(Audio.Media.DATA));
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
That's a very basic implementation. Get comfy with the android docs if you need to get fancy with it.

Have a look at the bundled music application source:
https://android.googlesource.com/platform/packages/apps/Music
List music using content providers
Use MediaPlayer in order to play the file you will get from the cursor
You also have examples on the android developer website: http://developer.android.com/guide/topics/media/index.html

Related

How can I show an encoded video file in Mediaplayer in Android?

I have a video file that is encoded. For example the first bit of each byte is reversed. I want to read this video file, change the first bits and send the decoded result to Mediaplayer.
How can I do that? How can I create and pass this stream to media player without saving the decoded data on storage?
It is important that I do not want to save a decoded copy of my video and play it on media player. I want to play encoded video directly on mediaplayer using streams or other possible ways.
Short answer: NO, there is no way to do that (obviously by my point of view)
You cannot reproduce from a "custom" stream by manipulating the data just before passing it the MediaPlayer.
Why?
The official MediaPlayer API which is closest to the one needed to achieve your goal is the following:
MediaPlayer mp = new MediaPlayer();
FileInputStream fis = new FileInputStream(yourFile);
mp.setDataSource(fis.getFD());
//...
This snippet allows to play a file starting from a FileInputStream, but more precisely from the underlying FileDescriptor. The FileDescriptor is a class which is marked as final (and it is reasonable because it has to deal with the underlying OS), so you cannot override anything.
Possible workarounds?
As you already pointed out, you can try to modify the real file "in-place" while reproducing the video with the standard MediaPlayer (without creating a deep/separate copy of it): it's very tricky but plausible.
Try to use another player object: ExoPlayer (which is a new standard Android API) or Vitamio
Try a pure native solution (NDK + Android source), which I will not recommend ;)
UPDATE: detail about the 1st workaround
Assuming that "the first bit of each byte is reversed" you can use a FileChannel to manipulate the whole file "in-place" while reading it. You should use a FileChannels created from a RandomAccessFile created in mode "rw" in order to be able to read/write simultaneously.
This pre-elabaration task can run on a separated thread (or inside an IntentService, which is more fashion and reliable); you can wait for few seconds after the elaboration begins and then starting the playback by passing the File reference to the standard MediaPlayer (you need to tune this waiting period considering how fast is the elaboration, like a streaming buffering but easier because performance are almost stable).
In this way you don't need to wait the end of the pre-elaboration before starting the playback.
When the playback stops or you close the app, you need to undo your work by calling the same pre-elaboration task on the played file in order to restore it to its original state.
I hope that this hint can be useful.
Comments and precisations about my answer are welcome, I will update my post if I'll find more information.

Android - ListView - playlist

I am making an audio player, i need to browse files/folders from device and add them to playlist (ListView). The most important for me is to UNDERSTAND what is going on, and how to do this. I think i need to get filenames and fill the ListView with it, but don't know how. Second qeustion is how to play these files in the playlist? Thank you!
You need to use the following steps
Query for the song, ContentResolver class to retrieve tracks on the device
Display the data extracted using Content Resolver in,list view.
Use MediaPlayer class to play audio and the MediaController class to
control playback.
Use a Service instance to play audio when the user is not directly interacting with the app.
Below I have mentioned very nice tutorial by TuPlus that covers everything of your question.
http://code.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764

Is it possible to send XOR'ed data to MediaPlayer?

I have XOR'ed media files.
I want to play them with MediaPlayer class. Is it possible to decrypt media flow on the fly (and be able to rewind them)?
Shortly, I want to have all functionality of MediaPlayer class for XOR'ed media files.
Thanks in advance!
MediaPlayer may be too high level to accomplish this. I would suggest taking a look at AudioTrack. If you read your file in using an InputStream then you could XOR the data before feeding it to an AudioTrack instance. The only downfall to this approach is you will need to implement your own seek mechanism.

Playcount for Media Files?

I want to query meta data from the external storage. That's pretty straight forward with the ContentProvider. But I also want to get the play count of the mp3 file.
I looked into projects like scrobble droid and subsonic but I didn't find a way to retrieve the Play Count if it's there. (e.g. Winamp supports it.)
Is there a way to get the PC if it's there?
You could manually read in the ID3 tags and check for a playcount attribute, but keep in mind, if this is for your own media player application, you'll have to modify the file each time it is played (load it, modify the ID3 tag, save the changes) which I imagine would be inefficient and a battery drain at best. Also, very few other media players actually DO this (Winamp, from what I see, seems to be one of the few), so it's not going to be an accurate playcount if played with, say, the stock media player, or any other media player which does not modify the ID3 tag.
Most media players that track playcounts (iTunes, Zune, etc.) do so by maintaining a database with playcount as an attribute. Obviously this is not portable between media players, but for your own implementation, I would suggest a database as your solution.
I faced the same problem but found some solution. So, android does not provide the data but music players could store this info. What I did:
* decompiled music app
* grep manifest file looking for provider
* profit
The solution is not perfect but I think there is no other ways to get this information on not rooted device.

Android Online Radio Player

Is it possible to get track name while playing radio stream via MediaPlayer?
I would say pretty much with certainty - no, it isn't possible.
I can't see any MediaPlayer methods which suggest it's possible plus the way that metadata such as track name etc is presented in streaming media, will depend on the source, e.g, Shoutcast or otherwise.
If it can be done I'd be interested to know but I'd suspect you'd need to write something like a Shoutcast client (or other client depending on source). You'd still use MediaPlayer for streaming but would need extra code for accessing the metadata.

Categories

Resources