简体   繁体   English

如何在单击按钮时调用Java类以播放Jfugue

[英]How to call a Java class on button click to play Jfugue

When I only run the java file it produces sound. 当我只运行Java文件时,它会发出声音。 I now want on button click to call the java file and produce the sound in eclipse. 我现在想单击按钮以调用Java文件并在eclipse中产生声音。

Here is my Java code: 这是我的Java代码:

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: 还有我在Eclipse中的MainActivity.java

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???     
 }
}

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. A)如果是关于类中的方法的,则首先必须创建该类的新实例,然后从新创建的实例中调用该函数。

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: BasicFunctions.java文件中, play方法如下:

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

B) You can always define a static method in order to bypass creating extra instances: B)您总是可以定义一个静态方法来绕过创建额外的实例:

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

And on the BasicFunctions.java file, a play method like: BasicFunctions.java文件中, play方法如下:

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. 似乎您刚刚获得JFugue网站的HelloWorld示例,并尝试将其与您的Android应用程序结合。 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. 首先,我不知道该库,但对于纯Java,似乎HelloWorld示例可以按预期工作(而不是一个小错误,我认为仅存在于本地计算机上),我可以听到声音。 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. 现在,根据您尝试对Android OS进行的库调整,事情并不是那么容易,因此我建议在此处输入输入链接描述 ,库的Android移植项目。

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: 该项目仍未提供任何用法示例(因此,如果解决您的问题,您可以将其分叉并提供(如果需要),因为它对于像您这样的将来的引用确实很有帮助),但是它的Wiki页面似乎得到了很好的解释:

在此处输入图片说明

Also, as a side note for the above project, please have a look at this answer. 另外,作为上述项目的补充说明,请查看答案。

You do this in xml - you set the android:onClick method to your method name in your java class: 您可以在xml中进行此操作-您将android:onClick方法设置为Java类中的方法名称:

<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: 您在Java类中编写方法myFancyMethod

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? 从以下线程中查看解决方案: android:onClick XML属性与setOnClickListener有什么不同?

In .xml file write this one. 在.xml文件中编写这一行。

<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 然后在您的Java代码内部实现该方法

public void justclick(View v) {
// does you're function
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM