制作音板并使用媒体播放器安卓

时间:2015-04-27 14:11:51

标签: android button global-variables android-mediaplayer soundpool

我对编程非常陌生,这是我的第一次尝试。我有一个带按钮的表格布局,我希望每个按钮播放声音(即音板)。 我可以处理那么好,但我现在有40个按钮,每一个我都称之为媒体播放器的新实例,所以最终我结束了太多而声音停止播放。

每个按钮都有以下代码:

MediaPlayer MP1 = MediaPlayer.Create(this,Resource.Raw.sound01);

B

MediaPlayer MP2 = MediaPlayer.Create(this,Resource.Raw.sound01);

   server:
      port: ${PORT:${SERVER_PORT:0}}
    debug:
    security:
      user:
        password: none
    zuul:
      routes:
        resource:
          path: /resource/**
          url: http://localhost:9000/resource
        user:
          path: /user/**
          serviceId: authentication-service/uaa/user

    spring:
      application:
        name: ui-service
      oauth2:
        sso:
          home:
            secure: false
            path: /,/**/*.html
        client:
          accessTokenUri: http://authentication-service/uaa/oauth/token
          userAuthorizationUri: http://authentication-service/uaa/oauth/authorize
          clientId: acme
          clientSecret: acmesecret
        resource:
          serviceId: ${PREFIX:}resource
          jwt:
            keyValue: |
              -----BEGIN PUBLIC KEY-----
              MYKEY
              -----END PUBLIC KEY-----
    logging:
      level:
        org.springframework.security: DEBUG
    eureka:
      instance:
        metadataMap:
          instanceId: ${spring.application.name}:${spring.application.instance_id:${random.int(10000)}}
      client:
        serviceUrl:
          defaultZone: http://localhost:8888/eureka/

我意识到这是非常重复和错误的代码。我被告知我需要使用媒体播放器的全局实例并在声音播放后释放它,但我不知道如何做其中任何一件事。 我还发现了一个建议做这样的事情:

    Button button1 = FindViewById<Button> (Resource.Id.button1);
    button1.Click += (object sender, EventArgs e) =>
    {
        MP1.Start();
        MP1.Release();
    };

任何直接代码示例的帮助都会对我有很大帮助,谢谢

1 个答案:

答案 0 :(得分:0)

这可能对你有所帮助......

    final MediaPlayer player = new MediaPlayer();
    final Resources res = getResources();

    final int[] buttonIds = { R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9 };
    final int[] soundIds = { R.raw.s1, R.raw.s2, R.raw.s3, R.raw.s4, R.raw.s5, R.raw.s6, R.raw.s7, R.raw.s8, R.raw.s9 };


    // On Click Listener tells the button to play the sound.
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < buttonIds.length; i++) {
                if (v.getId() == buttonIds[i]) {
                    selectedSoundId = soundIds[i];
                    AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
                    player.reset();
                    try {
                        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        player.prepare();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    player.start();
                    break;
                }
            }
        }
    };

    // Matches the button with its correct sound???
    for (int i = 0; i < buttonIds.length; i++) {
        Button soundButton = (Button)findViewById(buttonIds[i]);
        registerForContextMenu(soundButton);
        soundButton.setOnClickListener(listener);
    }

将它放在MainActivity.java的OnCreate方法中。在代码“int selectedSoundId;”中的方法类型之外或者会出现错误。另外,请确保使用按钮,否则会出现其他错误。希望这会有所帮助!!!