I am making a game which will be using questions stored on a mp3, so for each question there will be a mp3 file.
The user will then press play to hear the mp3 and then add an answer for the mp3 in an edittext field. Which would then show correct or incorrect answer.
When the user clicks confirm answer the mp3 will move to the next question. So when the user presses play it will be question 2. Is it possible to do this and if so how would I go about implementing this. I would highly appreciate any advice on this.
Thanks.
It is recommended to use WAV or OGG format for sounds on Android, but in any case, you can play back your MP3s with the MediaPlayer class. For example:
MediaPlayer mp = MediaPlayer.create(YourActivityClass.this, R.raw.your_mp3_resource);
if(mp != null) {
mp.start();
}
Read the documentation regarding state and calling release() on finished MediaPlayer objects. Alternatively use one MediaPlayer object with the setDataSource() and prepare() methods.
Right now, you can use SoundPool or MediaPlayer
Both support mp3, but based on my experience, SoundPool will not always play Mp3 of more than 100kb. For those files you will have to use MediaPlayer.
One link comparing both.
Related
I've encounter this problem many times , the problem is that I've a videoView in my application but it couldn't play lots of different formats and video files.
for example , if I want to open a mp4 file, I should ask my users to convert the video to a H.264 format and then upload the file and it's very hard.
I'm looking for a better way to play various formats of videos .
Does any one no any way to do so ? have can I do that ?
If you're willing to use a third party library, FFmpegMediaPlayer is a is a reimplementation of Android's MediaPlayer class based on FFmpeg. It adds support for additional formats and protocols not provided by Android's MediaPlayer class. It is licensed under LGPLv2.1.
In android, .mp4 extension is the most common video format which is compatible for the default playback.
Objective:
Implemention of VideoView for the playback of Videos.
Usage of MediaController for the playback controls.
SurfaceView which will hold the playback of Videos.
Containers that are used for playing videos:
VideoView
MediaController
SurfaceView
DisplayMetrics
Read more: http://mrbool.com/how-to-play-video-formats-in-android-using-videoview/28299#ixzz3uUX2Bew0
Or for getting best answer , you need to see this link.
Best way to play a Video file?
I suggest you to try out ExoPlayer. It has really nice features and documentations. Try this codelabs tutorial. https://codelabs.developers.google.com/codelabs/exoplayer-intro/#0
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.
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.
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
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.