Using MediaPlayer to play the same file multiple times with overlap - android

What's currently happening with my android application:
I mapped a simple image to a button and have it play a sound on click. On each click, I create a MediaPlayer object with a sound file in my raw folder, I set an OnClickListener for that MediaPlayer object which stops playing the file and releases it, and then I play the MediaPlayer object.
The code for the defined section:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ImageButton start = (ImageButton) findViewById(R.id.imageButton1);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.dvno);
play.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
mp = null;
}
});
play.start();
}
});
}
What's wrong with it:
It works fine and does not crash, but it's terrible for memory and very slow in general. I'd like for users to click the button many times in a row, as fast as they possibly can, and hear the sound play instantaneously over and over with overlap. Creating a new MediaPlayer object on each click and waiting for it to finish and be released consumes too many resources and the sound often lags behind the actual press of the button. I'd like to be able to create a single sound as MediaPlayer object which can be played while overlapping on itself.
Possible Solution:
Create a single final MediaPlayer object in the scope of onCreate rather than onClick and somehow use threading to start the MediaPlayer object on every click. I read that this might be a possible solution, but I haven't ever used threads before and I don't know if they're slow, if not slower, than my current code, so I'd like to know if there's a solution without using threads that might be simpler. Manipulating the state of a single MediaPlayer to overlap on itself seems impossible at this point, but maybe I'm wrong.
Would this crash the program because of illegal states? If not, is this going to be slower than I want it to be? And if not, can anyone suggest a fix to my code?

I suggest you use a SoundPool instead of MediaPlayer for this.
Tutorial is here

Related

VideoView prevent initial black screen by seekTo-Method for streamed videos

How do I prevent the initial black screen of a VideoView? I don't want to use a placeholder, as I therefore would have to create a previewimage for every VideoView (and I'm using several VideoViews...)
A common solution for this is to use videoView.seekto(100) once the video is prepared. But this doesn't work for me. It does set the seeker to the correct position, but the VideoView remains black until the user presses the play-button.
This is my code:
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mVideoView.seekTo(100);
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mVideoView.pause();
}
});
}
I also tried this without the onSeekCompleteListener and without seperatly starting and pausing the video, but it didn't work either..
anyone a hint for solving this issue?
EDIT
I figured out that this is only an issue for streaming videos! It's working like a charm for locally stored ones...
and it actually seems to be an official issue...
https://code.google.com/p/android/issues/detail?id=4124

issues running video in Android

I'm currently working in my Android app. and so far I got to the point where I need my app. to view some videos for the user
initially I thought of storing my videos in my server (currently is local but might move it later) and then I did not find a way how to do it so I choose to load it from a YouTube channel I made it for that purpose "I thought It'd look more porf." any way!!!
So far I run my app. in my phone coz I read that Emulator is not capable of running videos. It runs in my phone but with some issues.
1- I have to pause the video ASA the video layout pops up and wait for the video to buffer or something like that otherwise I got error msg "this video can't be played"
2- later when it loads it starts by it self even I did not press the start button.
3- the video is taking the top half of the screen leaving the bottom have a in White space with gap between the two halves.
4- the video is not clear as I can view it in my laptop or in YouTube app.
here is my .java code
public class video extends Activity {
String SrcPath = "rtsp://v8.cache3.c.youtube.com/CjYLENy73wIaLQltaP8vg4qMsBMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOL6qv2DoMPrUAw=/0/0/0/video.3gp";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video1);
VideoView myVideoView = (VideoView)findViewById(R.id.vview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}}
I really want some helps from you ppl.
any help is appreciated dudes.
Well if you want to play video only during a button press. There are two approaches
1) Remove myVideoView.start(); form the code ,i think it will stop playing of video on load
2)Next approach is do not set default MediaControllerfor video view
you can create custom play button for playing video and give myVideoView.start(); inside it's onclick
Button play = (Button) findViewById(R.id.btnPlay);
play .setOnClickListener(this);
Button pause = (Button) findViewById(R.id.btnPause);
pause .setOnClickListener(this);
Button stop = (Button) findViewById(R.id.btnStop);
stop .setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.btnPlay:
myVideoView.start();
/* the instruccions of the button */
break;
case R.id.btnPause:
/* the instruccions of the button */
break;
case R.id.btnStop:
myVideoView.stop();
/* the instruccions of the button */
break;
}
}
And the thing about clarity of video you are playing rtsp link which got low quality video eve you can see in link itselp you can see .3gp extension which is low quality
If you want to improve the quality . Have an eye on this
YouTube api
REFER

How to produce default sound

I hava a set of buttons. When I click on a button it should produce sound.
Example:
Button b=new Button(this);
b.setText("Press");
b.setOnClickListener(new OnClickListener)[
public void click(View v)
{
b.setSoundEffectsEnabled(true);
});}
This doesn't work though, can anyone help me please.
What do you mean on default sound? If you want to play your own sound, you must create a MediaPlayer like this.
MediaPlayer mediaPlayer = MediaPlayer.create(this, [here is your sound in the raw file]);
and in the click method you need to implement this:
mediaPlayer.start();
or you can use soundpool too.
Hope it helps.
You can also use the build in sound notifications
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, ToneGenerator.MAX_VOLUME);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
If you want to play the default click sound when you click on the button, then setting b.setSoundEffectsEnabled(true) should work (although it doesn't need to be on the listener), but it is dependent on the device option to play audible selection. Try checking the device's sound settings if it is on.

Play a melody on click

When I click on a view, I want to play a melody. The problem is that when I click very fast, the melody starts from the beginning and I want it to play to the end and at this time screen must not react to clicks (because I don't want the melody to be played 10 times if I clicked very fast 10 times). I wrote something like this:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
mp.start();
if(mp.isPlaying()) {
img1.setClickable(false);
}
img1.setClickable(true);
But after one click the screen does not react to click anymore... Help me please with this.
//Init your MediaPlayer when Activity is created, for instance:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
//**************************//
//onClickListener goes here:
if(mp.isPlaying()){
//do nothing
}else{
mp.start();
}
//onClickListener ends here:
Read the android developers guide, it says "If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again"
Upon click event check if the media player has finished its playback. If no, then do nothing, if yes then play again.
To check if finished use this method below:
isPlaying()
You may check this:
http://developer.android.com/reference/android/media/MediaPlayer.html
Hope you find it easy. :)

Pause MediaPlayer when using a Map in android app

I am building a soundboard with about 40 sounds using a map.
(Previous thread)
Does anyone know a good way to pause restart Mediaplayer if the app is moved to the background (incoming call or anything like that)? I'm still very new at this so it is probably something super simple. Thanks anyone who can help.
Map map = new
HashMap();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
...
and then iterate:
for (Map.Entry entry : map.entrySet()) {
final MediaPlayer sound = MediaPlayer.create(entry.getValue());
Button button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.start();
}
});
}
I assume you don't want to play all sounds together, so you can declare the mediaplayer object outside the loop and then you don't need to know which file is being played. You can use sound.pause(); sound.stop(); or sound.reset(); (depends on the action you want to preform) I suggest you look at the android docs to see how to implement it properly.
If you want to create mediaplayer for each file which I doubt you want, save the mediaplayers in a map / list and pause / reset them in loop during onPause().
Override Activity.onPause(). This is called whenever your app goes to the background, Home button or back button or incoming call or magic thunderclouds of doom.

Categories

Resources