Run an audio file instantly when clicking on the application button (Android) - android

How to run an audio file instantly when clicking on the application button and not when button returns with Android Media Player
Any help is welcome because I did not find solution.

1.you add button
2.
public class Setting extends Activity implements OnClickListener{
private MediaPlayer Music;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_setting);
Music = MediaPlayer.create(this, R.raw.musicname);
findViewById(R.id.button).setOnClickListener(this);
}
public void onClick(View view) {
if (view.getId() == R.id.button) {
Music.start();
}
}
}
only Preview :D

Related

OnClick Button Sound link with an ImageView

I'm trying to learn to program a simple app in (Android Studio), in this app I have 10 images which I programmed them with ImageSwitcher to go next or previous, Now I want to have a button which plays my 10 audios according to each image. Example when the Image I1 is shown and user presses PLAY button then Audio A1 plays, and the same for the rest I2=A2, I3=A3 and ...
Any help would be great and is appreciated.
with best regards
First you need to add your audio files in the raw folder. Then create an array with reference of image array put names of audio accordingly
public class MainActivity extends AppCompatActivity {
Context context = this;
MediaPlayer mp;
int audioList[] = [R.raw.audio1, R.raw.audio2........]
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button b = (Button) findViewById(R.id.Button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(context, audioList[currentIndex]);
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, audioList[currentIndex]);
} mp.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
}
}
Hope this works for you.
Happy Coding

YouTubeStandAlonePlayer(Android Player API) lightbox mode doesn't a have fullscreen button?

I'm trying out the Android Player API and I noticed that while using the YouTubeStandAlonePlayer class, there's no fullscreen button in the player UI when playing the video in lightbox mode.
Is there a way you can customize the player UI to show the fullscreen button?
I look up the docs but don't see any more info
public class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playVideoButton = (Button) findViewById(R.id.start_video_button);
playVideoButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = null;
if (v == playVideoButton) {
intent = YouTubeStandalonePlayer.createVideoIntent(
this, DEVELOPER_KEY, VIDEO_ID, 0, true, true);
}
}

MediaPlayer playback limit

I'm using a MediaPlayer to play my sound effects but after a certain point (28 playbacks to be exact) the MediaPlayer does not play anymore. This is how I create the MediaPlayer:
if(noteManager.rep.get((layer)).notes[j] == 0){
MediaPlayer mp;
switch (j){
case 0:
mp = MediaPlayer.create(InGame.this, R.raw.one);
break;
...
}}
Right after I call:
mp.start();
Also, this code gets executed a couple times a second. It all works fine until the 29th sound effect, after which it stops working (there's no sound). I'm using short wav files. Any thoughts?
Use MediaPlayer outside the click event
public class MainActivity extends AppCompatActivity {
private ImageView Reload;
private TextView Label;
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Reload = (ImageView)findViewById(R.id.iv_Reload);
Label = (TextView)findViewById(R.id.tv_appname);
mp = MediaPlayer.create(getApplicationContext(),R.raw.reloadsound);
Reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.iv_anim));
mp.start();
}
});
}

Stop audio playback

I am new to Android programming and i have a problem that i can not solve without your help...Please, give me a hand... ;-)
When i start playing my audio file, i can not stop it. Instead, I am quitting the application.
I play audio file with this code:
public class Word1Audio extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
}
HOWEVER, when i try to stop it when the back button is pressed by this piece of code
#Override
public void onBackPressed() {
// do something on back.
return;
}
my applications closes down and audio continues to play...
Do you have an idea why the application closes down? And how can i just stop the music and go back to the previous page...??
Thank you for your time and help... :-)
you could try
MediaPlayer mediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
#Override
public void onBackPressed() {
if( mediaPlayer.isPlaying() ) {
mediaPlayer.stop();
}
super.onBackPressed();
}

simple example how to insert audio file in android

i am trying to insert a audio file into the application which is not showing any kind of error but while am running the program then it is showing some kind of exception that to force close kind of dialog box and here is my code
This is my first activity where am trying to invoke the button into the second activity playing the audio file.
public class Audio extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b2=(Button)findViewById(R.id.b1);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 =new Intent(Audio.this,Audio1.class);
startActivity(i1);
}
});
}
}
public class Audio1 extends Activity {
private MediaPlayer eMediaPlayer= new MediaPlayer();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
eMediaPlayer=MediaPlayer.create(this, R.raw.ab);
eMediaPlayer.start();
}
}
audio is my xml file which is empty presently
R.raw.ab is the resource file which is 830kb audio file
you should call
eMediaPlayer.prepare();
before
eMediaPlayer.start();

Categories

Resources