I would like my button (should it be a button or sth else?) to to play music when I hover on it and stop at HOVER_EXIT. What should I implement for
case MotionEvent.ACTION_HOVER_MOVE: to make button play music still, without pauses from ENTER to EXIT and on MOVE dont do anything?
There is also an error - when I try to open a screen with this hoverbutton, app crashes and turns off.
Here is my java code:
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class DisplayActivity extends AppCompatActivity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (Button)findViewById(R.id.button1);
b1.setOnHoverListener(new View.OnHoverListener()
{
#Override
public boolean onHover(View v, MotionEvent event) {
MediaPlayer player=MediaPlayer.create(DisplayActivity.this,R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
player.stop();
break;
}
return true;
}
});}`
public class DisplayActivity extends AppCompatActivity {
private MediaPlayer player;
#Override
protected void onResume() {
super.onResume();
// create media player only when required
player = MediaPlayer.create(this, R.raw.sound);
// this will keep the audio playing, even if you hover for long time
player.setLooping(true);
}
#Override
protected void onPause() {
super.onPause();
// release is as soon as possible
player.release();
player = null;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
findViewById(R.id.button1).setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(
View v,
MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
// if you choose to stop the player, you need to prepare it again by calling player.prepare(); before restarting it.
// I chose to pause it and seek it back to beginning
player.pause();
player.seekTo(0);
break;
}
return true;
}
});
}
}
Hope this helps :)
Related
I am trying to make a simple app to help me learn how to code for Android. The apps principle is simply this: When you press down on a button it starts playing a sound file. When you release the button, the audio stops. Pressing down on the button works, but releasing it does not. Thanks in advance! Here is my java code:
package com.example.siriu.presstoplay;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.MotionEvent;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.pressbutton);
button.setOnTouchListener(handleTouch);
mp = MediaPlayer.create(this, R.raw.test);
}
private View.OnTouchListener handleTouch = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("button", "down");
play();
break;
case MotionEvent.ACTION_UP:
Log.d("button", "up");
pause();
break;
}
return true;
}
};
public void play (){
final MediaPlayer mp = MediaPlayer.create(this, R.raw.test);
mp.start();
Log.d("MediaPlayer", "started");
}
public void pause (){
mp.stop();
Log.d("MediaPlayer", "paused");
}
}
Android MediaPlayer will not stop or pause
Because in your play () method you are starting local declared MediaPlayer of play () method
and in your pause () you are trying to stop MediaPlayer that you have declared global
in this situation your global declared MediaPlayer mp; hasn't start yet
Try this
Remove final MediaPlayer mp = MediaPlayer.create(this, R.raw.test); from Your play() Method
public void play (){
mp.start();
Log.d("MediaPlayer", "started");
}
I wanna make "Play/Stop" button. When button "Play" is clicked, the song must be played and its text should be converted into "Stop" and when "Stop" is clicked, the button text should be changed into "Play" again and song should start again to play from the beginning.
import android.media.MediaPlayer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button btn_playStop;
private MediaPlayer mediaPlayer;
private boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Play Music");
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.zara_sa);
btn_playStop = (Button)findViewById(R.id.btn_play_stop);
btn_playStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() && flag==true){
stopSong();
}
else if (flag == false){
playSong();
}
}
});
}
public void playSong(){
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}
public void stopSong() {
mediaPlayer.stop();
btn_playStop.setText("Play");
flag = false;
}
}
You must call mediaPlayer.prepare(); if you called mediaPlayer.stop(); so in the first time you can just call mediaPlayer.start(); but in the next times you should call mediaPlayer.prepare(); before mediaPlayer.start();
public void playSong(){
try {
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}
package project.kalmas;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class one extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
MediaPlayer mp= MediaPlayer.create(this,R.raw.one);
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
When i click button it play sound then again i click button to stop it wont stop and play sound again which results in double sound playing at one time.Please help
You are creating a new MediaPlayer with every click, instead of keeping a reference to the first one. The MediaPlayer that is playing the sound is different than the MediaPlayer that you are calling isPlaying() on. You need to turn the mp variable into a field so you can keep a reference to it.
I imagine something like this would work:
public class one extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mp = MediaPlayer.create(this,R.raw.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class pushke extends Activity {
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
MediaPlayer mp;
MediaPlayer mp2;
MediaPlayer mp3;
MediaPlayer mp4;
MediaPlayer mp5;
MediaPlayer mp6;
SeekBar seekGuns;
RelativeLayout rguns;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.guns);
initControls();
rguns=(RelativeLayout) findViewById(R.id.rguns);
mp=MediaPlayer.create(this, R.raw.hekler);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp2.isPlaying()){
mp2.pause();
mp2.seekTo(0);
}
if (mp3.isPlaying()){
mp3.pause();
mp3.seekTo(0);
}
if (mp4.isPlaying()){
mp4.pause();
mp4.seekTo(0);
}
if (mp5.isPlaying()){
mp5.pause();
mp5.seekTo(0);
}
if (mp6.isPlaying()){
mp6.pause();
mp6.seekTo(0);
}
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}
});
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekGuns);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
int index = seekGuns.getProgress();
seekGuns.setProgress(index + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
int index = seekGuns.getProgress();
seekGuns.setProgress(index - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
I am trying to connect hardware buttons to my seekbar so when I click on volume buttons, seekbar which is used for volume, increase-decrease depending on which button I pressed.
I got this code which is supposed to be working, but I have no idea why it's not working in my case, everytime I launch my app on phone as emulator, and use hardware volume button app crashes.
I get this in LoGcat but Ican't figure out how to fix it or where is the problem because eclipse is not showing me any errors.
Here is my logcat: http://i.imgur.com/o4qSvCv.png
This is because you have declared two Seekbar variables.
private SeekBar volumeSeekbar = null;
SeekBar seekGuns;
and in your onKeyDown method you are using seekGuns named variable which is not initialized.
So you have two option.
1) Initialize means find the id for seekguns variable.
2) Use volumeSeekbar instead of seekguns in onKeyDown method.
I have a problem where I can't keep the music playing when chaning the screen orientation in my program. I tried adding android:configChanges="keyboardHidden|orientation|screenSize inside the MainActivity in the AndroidManifest.xml file, but although it keeps the music playing, it also disables the different layout for the landscape mode.
Here is the code for the MainActivity:
package com.example.gomoku;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up click listeners for all the buttons.
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View highScoreButton = findViewById(R.id.high_score_button);
highScoreButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.new_button:
startGame();
break;
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
#Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.main);
}
#Override
protected void onPause() {
super.onPause();
Music.stop(this);
}
private void startGame() {
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
}
}
And here is the code for Music:
package com.example.gomoku;
import android.content.Context;
import android.media.MediaPlayer;
public class Music {
private static MediaPlayer mp = null;
/** Stop old song and start new one. */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences.
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
/** Stop the music. */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
Editing the original code;
package com.example.gomoku;
import android.content.Context;
import android.media.MediaPlayer;
public class Music {
//####First change is here####
//adding a variable to store the time of music being played.
private static int pos = 0;
private static MediaPlayer mp = null;
/** Stop old song and start new one. */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences.
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
//####Second change is here####
//this will continue the music from wherever it was paused
mp.seekTo(pos);
}
}
/** Stop the music. */
public static void stop(Context context) {
if (mp != null) {
//####Third change is here####
mp.pause();//to pause the music
//to store current pause time in pos
//as pos is static it will retain the value
pos = mp.getCurrentPosition();
mp.stop();
mp.release();
mp = null;
}
}
}
add this code to stop the app when back key is pressed.
#Override
public void onBackPressed() {
pos = 0;
super.onBackPressed();
mp.release();
System.exit(0);
}
Now this app will:
Play music in loop when it starts.
Continue the music when screen orientation is changed.
Resume music when app is restarted after pressing home button.
Restart song if the back button was pressed.