简体   繁体   English

许多Mediaplayer缩减为一个Mediaplayer? 怎么样? 音板

[英]A lot of Mediaplayers shrink to one Mediaplayer? How? Soundboard

I'm a Java/Android Studio beginner and I'm writing a Soundboard. 我是Java / Android Studio的初学者,正在写一个Soundboard。

My Soundboard has about 10-15 Buttons per Page and this is my following Problem 我的共鸣板每页大约有10-15个按钮,这是我的以下问题

I'm declaring a lot of Mediaplayers 我要宣布很多媒体播放器

  public class MainActivity extends AppCompatActivity {

 MediaPlayer X1;
 MediaPlayer X2;
 MediaPlayer X3;
 MediaPlayer X4;
 MediaPlayer X5;
 MediaPlayer X6;


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

    X1 = MediaPlayer.create(this, R.raw.x1);
    X2 = MediaPlayer.create(this, R.raw.x2);
    X3 = MediaPlayer.create(this, R.raw.x3);
    X4 = MediaPlayer.create(this, R.raw.x4);
    X5 = MediaPlayer.create(this, R.raw.x5);
    X6 = MediaPlayer.create(this, R.raw.6);

And in my XML files I'm working like this for each Button 在我的XML文件中,每个按钮的工作方式都是这样

<Button
        android:text="X1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button10"
        android:onClick="X1"
        android:elevation="0dp"
        android:background="android:attr/colorEdgeEffect"
        android:forceHasOverlappingRendering="false"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="normal|italic"
        android:layout_marginLeft="17dp"
        android:layout_marginStart="17dp"
        android:layout_marginTop="317dp" />

Now My Problems is it is a lot of work for each Mediaplayer and with a lot of Mediaplayers the App is crashing/doing problems. 现在,我的问题是每个Mediaplayer都需要做很多工作,而对于许多Mediaplayers来说,应用程序崩溃或发生问题。

How can I sum up all Buttons to one MediaPlayer? 如何将所有Button汇总到一个MediaPlayer?

It is just an example code how I am working could somebody change this code for me? 这只是我工作的示例代码,有人可以为我更改此代码吗? In some Threads I read "oncompletionlistener" but I never worked with it. 在某些主题中,我阅读了“ oncompletionlistener”,但从未使用过。

Stackoverflow would not let me answer, but I'll try again: Stackoverflow不会让我回答,但我会再试一次:

  • You can reuse one reference to media player, no need to define serveral ones. 您可以重用一个对媒体播放器的引用,而无需定义服务器引用。
  • You should determine which raw audio to play inside your onClick handler (ie the method called when button is clicked, you have the view as parameter here) based on button id (or something like that), ie call create on the same reference using another raw audio id. 您应该根据按钮ID(或类似名称)确定在onClick处理程序中播放哪种原始音频(即,单击按钮时调用的方法,此处将视图作为参数),即使用另一个引用在同一引用上创建原始音频ID。

Something like: 就像是:

android:onClick="playFile"

And then you have 然后你有

public void playFile(View view){
       switch(view.getId()) {
    case R.id.button1:
      // play audio file 1
      break;
    case R.id.button2:
      // play audio file 2
      break;
     //....
  }
 }

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

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