I've been looking at this example https://stackoverflow.com/a/8974361/1191501 and it works perfectly. But my problem is how do I reference the recorded audio so it can be played back straight away?
the output code is:
recorder.setOutputFile("/sdcard/audio/"+filename);
and this definitely records the audio.
and then to playback the audio, I was using:
player.setDataSource();
but I don't know how to reference the filename bit so it plays back. Any ideas?
I had similar problems playing audio from the SD card at one point. This is what did it for me:
private void playMedia() {
String path = Environment.getExternalStorageDirectory() + "/audio_stuff.mp3";
mediaPlayer = MediaPlayer.create(this, Uri.parse(path));
mediaPlayer.start();
}
Make sure to release your MediaPlayer instance and set it to null when you are done. And just in case, make sure your SD card is not mounted when you try to play your audio file. :)
Looking here,
player.setDataSource("/sdcard/audio/"+filename);
player.prepare();
player.start();
would work I would think.
Related
[First App] I am creating a sort of Alarm app that allows user to select alarm sound from either sd-card or app-supplied sounds. Since, the app essentially plays alarms, I want the volume to be 'alarm volume' of the device. I am able to achieve this for sd card sounds. But, I am unable to setAudioStreamType for raw resource sounds.
I am using following code :
MediaPlayer m_player = new MediaPlayer();
m_player.setAudioStreamType(AudioManager.STREAM_ALARM);
switch (bin_name) { //bin_name = various user selectable music files
default:
m_player = MediaPlayer.create(context, R.raw.blu);
break;
}
m_player.setLooping(true);
m_player.start();
My blu.mp3 plays at media volume only. Upon checking the documentation for MediaPlayer.create(Context context, int resid), I found this :
Note that since prepare() is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)), audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.
I also tried finding code samples for above method but none of them showed how to set AudioStreamType to AudioManager.STEAM_ALARM. I will accept answers with alternative ways that simply play the sound with infinite loop. How to achieve this ?
As the documentation you are referring to says, you must create and prepare the MediaPlayer yourself. Haven't tried with the STREAM_ALARM but I'm using following snippet to play on STREAM_VOICE_CALL
Uri uri = Uri.parse("android.resource://com.example.app/" + R.raw.hdsweep);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setDataSource(context, uri);
mMediaPlayer.prepare();
mMediaPlayer.start()
I am working with streaming video and I want to change from one source to another dinamically.
First I set the video uri to the VideoView
view.setVideoURI(myUri);
And I know that I am capable of changing it afterwards by doing (this is in onPrepare method but it could go somewhere else where I have access to the MediaPlayer).
#Override
public void onPrepared(MediaPlayer mp)
{
Uri newUri = getOtherUri();
mp.reset();
mp.setDataSource(getApplicationContext(), newUri);
mp.prepare();
mp.start();
}
The thing is, I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
I tried to create a new VideoView with the new Uri and then change one object for the other, and likewise with the media player. However, none of that seems to work.
Is it possible to replace a video while it is playing in Android?
Any comments would be appreciated.
There is no option to reset the video without using mediaplayer.stopPlayback() or mediaplayer.reset. The reason is that; the previous object of the mediaplayer has to be released before you can play other video .
I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
Well, this cannot be achieved as the mediaplayer has to be reset. So there will be lag while changing videos. And to satisfy you, you can see these behavior in any videoplayer app like youtube or mxplayer.
So the only thing you can do is to show progressbar while loading or changing video.
Hope it helps. Cheeers.:)
Due to login confusion, i'm repeating this question. If any moderator sees this, i'd like to keep this one current, as I no longer can access my former login.
A bit of a problem has presented itself to me. I am trying to play a sound continously looping in my android app.
With MediaPlayer: MP3 plays alright, but there is a gap at the end which does not exist in the file. I read it had to do with the decoder, and that ogg should work. Tried using ogg, but still get the gap, which is definitely not on the file.
With SoundPool classes and ogg (using this fellow's interesting class: http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html),
the sound starts, and a fraction of a second later, it restarts. so I get a stutering half a second of the beginning of every file, without advancing further, because it is always going back to the beginning.
Is there something really wrong with media player and it's ability to loop audio? How about the freakishy stuttering soundpool?
Thank you very much for any assistance!
Note: Karthi_Heno suggested I do this:
MediaPlayer mediaPlayer;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
try {
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("music.ogg");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
} catch (IOException e) {
textView.setText("Couldn't load music file, " + e.getMessage());
mediaPlayer = null;
}
However, when i do this, getassets gives filenotfound, even though there IS a filein assets. any thoughts either on thisassets problem, or my audio loop question?
thanks. Yep, i'm an android newb alright, can't even get sound to loop alright.
I have ONE object MediaPlayer mediaplayer. I use it to play different sounds, one after another.
mediaplayer = MediaPlayer.create(context, ResIdMusicONE);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
// some other user input
mediaplayer = MediaPlayer.create(context, ResIdMusicTWO);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
Sometimes is works fine. But sometimes the two sounds are played at the same time. And at positions, where mediaplayer should already have been released and be equal null.
Thanks for the help.
If I were you I would use the SoundPool class for this. With SoundPool you can set the number of streams to play at the same time, so by setting that to 1 you can just call play() over and over and the most recent call to play() will be the only sound that you hear.
Take a look at my post a while back. It has an example of the SoundPool class in the question.
Edit:
Have you tried creating a new instance and calling the prepare() every time you want to start a new sound?
mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
Though actually now that I think about it, I'm pretty sure you only need to do that if you are using a file from the sdcard not from your resources... Hmmm.
I have prepared a code to just play a simple mp4 file from my res folder. The coding is something like this:
public class VideoPlayer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView video = (VideoView)findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://company.software.myapp/"
+ R.raw.myvideo);
MediaController mc = new MediaController(this);
video.setMediaController(mc);
video.setVideoURI(uri);
//video.requestFocus();
video.start();
}
}
Now though there is no error in playing. The activity automatically generates a dialog saying "sorry this video cannot be played", but I can hear the audio and it plays till end. What is the problem?
Thanx a lot commonsware.com... but i found the solution to the problem... And astonishingly its the PC processor which is the culprit... I checked n a higher configuration and guess wat... it worked perfectly fine... though sometimes if we do some processing in the background the dialog box does come up but on clicking ok it starts playing the video after some time...
But i confirm that this technique of playing file from resource is ok as far as i know...
sorry to waste ur precious time in a mundane hardware problem... but hope it'll be useful for other people who get this problem...
Android supports 3gp and mp4 format, but still sometimes there are problems in playing an mp4 content.
one thing what I have found out from my research is that, this might be because the resolution problem with the video.
I think that you should re-size the resolution of your mp4 video. This might help.
I have not attempted to play a video clip out of a resource, and I am not certain that it works.
As a test, put the video clip on the SD card and use that as the source of your video.
If you get the same symptoms, then either the MP4 file has issues or it is something with your test environment (e.g., you are using the emulator and don't have a quad-core CPU).
If the SD card test works, though, then I suspect the problem is packaging it as a resource.