0

When I only run the java file it produces sound. I now want on button click to call the java file and produce the sound in eclipse.

Here is my Java code:

import org.jfugue.player.Player;

public class HelloWorld {
  public static void main(String[] args) {
    Player player = new Player();
    player.play("C D E F G A B");
 }    
}

And my MainActivity.java in eclipse:

public class MainActivity extends ActionBarActivity implements    View.OnClickListener { 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener((OnClickListener) this);
}

@Override
public void onClick(View arg0) {
    // CODE HERE???     
 }
}
teobais
  • 2,820
  • 1
  • 24
  • 36
John
  • 19
  • 1
  • 4
  • you don't call a file (besides on it's name :) but you can call a function in the class that is defined in the file. If you post "the file" as well we might be able to help with the code in onClick – Gavriel Jan 15 '16 at 07:54
  • thank u Gavriel I posted the code :) – John Jan 15 '16 at 15:53
  • although other's answers explain how you can call your java code from onClick, I doubt you'll hear any sound. If it work's you're lucky. If it doesn't then I suggest you read about Android's MediaPlayer: http://www.tutorialspoint.com/android/android_mediaplayer.htm – Gavriel Jan 17 '16 at 06:30
  • Did my answer solve your question? – teobais Feb 15 '16 at 13:41
  • Yes thank you toubou!!! – John Feb 17 '16 at 09:54

3 Answers3

1

Yes, but it mostly depends on the functionality of your application or the specific design you would like to achieve/apply.

I foresee two options, but as I don't know more details about the application, I won't propose anything, just reference them:

A) If it's about a method in a class, you first have to create a new instance of the class and from the newly created instance, call that function.

That would be something like:

public void onClick(View v) {
    BasicFunctions functions = new BasicFunctions();
    functions.play();
} 

And on the BasicFunctions.java file, a play method like:

public void play() {
   //functionality to play the file
}

B) You can always define a static method in order to bypass creating extra instances:

public void onClick(View v) {
    BasicFunctions.play();
}

And on the BasicFunctions.java file, a play method like:

public static void play() {
   //functionality to play the file
}

But it's always up to you and want you want to achieve.


Edit

Seems like you just got the HelloWorld example of the JFugue's site and tried to combine it with your Android application. However, I foresee many issues on it and one of them may be the root cause of your problem.

To begin with, I wasn't aware of this library, but for pure Java, it seems that the HelloWorld example works as expected (instead of a small error, that I assume exists only on my local machine), which that I can hear a sound. Now, according to the library's adjustment that you tried to accompilish to the Android OS, things are not so easy, so I would suggest going for enter link description here, an Android porting project of the library.

The project still doesn't provide any usage examples (so, if solving your issue, you could just fork it and provide one, if you want to, as it will be really helpful for future references, like yours), but its wiki pages seem to be well-explained:

enter image description here

Also, as a side note for the above project, please have a look at this answer.

Community
  • 1
  • 1
teobais
  • 2,820
  • 1
  • 24
  • 36
0

You do this in xml - you set the android:onClick method to your method name in your java class:

<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="myFancyMethod" />

You write the method myFancyMethod in your java class:

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) {
        myFancyMethod(v);
    } 
}); 

// some more code 

public void myFancyMethod(View v) {
    // does something very interesting 
} 

See solution from this thread: How exactly does the android:onClick XML attribute differ from setOnClickListener?

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217
0

In .xml file write this one.

<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="justclick" />

And then inside of you're JAVA code implement the method

public void justclick(View v) {
// does you're function
 }
Tanvir Durlove
  • 142
  • 1
  • 10