【问题标题】:How to Use Intent for Media Player?如何为媒体播放器使用 Intent?
【发布时间】:2019-05-29 18:25:51
【问题描述】:

我想在我的作业应用程序中使用意图。当我单击 button1 时,我的 SoundActivity 打开,播放 sound1.mp3 文件后。但我想当我点击 button2 时,SoundActivity 中正在播放 sound2.mp3 文件..

这是我的 MainActivity.java 代码:

button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            Intent intent = new Intent(this, SoundActivity.class);

            }
        });

button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            Intent intent = new Intent(this, SoundActivity.class);

            }
        });

这是我的声音活动方面:

sound1 = MediaPlayer.create(this, R.raw.bell);
sound2 = MediaPlayer.create(this, R.raw.siren);

sound1.start();

【问题讨论】:

    标签: android audio android-intent android-mediaplayer


    【解决方案1】:
    'Another way is that you just pass a key like which song you want to play on button click listener like'
    
    
    
    
    
    button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                Intent intent = new Intent(this, SoundActivity.class);
                intent.putExtra("SOUND", "sound1");
                startActivity(intent);
    
                }
            });
    
    button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                Intent intent = new Intent(this, SoundActivity.class);
                intent.putExtra("SOUND", "sound2");
                startActivity(intent);
    
                }
            });
    
    
    
    
    
    
    'get this key in sound activity and pass the sound on the basis of condition like'
    
    
    
    
    
    
    Bundle bundle = getIntent().getExtras();
            String sound = bundle.getInt("SOUND");
    
    if(sound.equals(sound1)){
    play_sound = MediaPlayer.create(this, R.raw.bell);
    }else if(sound.equals(sound2)){
    play_sound = MediaPlayer.create(this, R.raw.siren);
    }
    
    play_sound.start();
    
    
    
    
    'Hope you get the best'
    

    【讨论】:

      【解决方案2】:
      'You can send your sound with intent using putt extras like'
      
      button1=findViewById(R.id.button1);
      button2=findViewById(R.id.button2);
      
      button1.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
      
                  Intent intent = new Intent(this, SoundActivity.class);
                  intent.putExtra("SOUND", R.raw.bell);
                  startActivity(intent);
      
                  }
              });
      
      button2.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
      
                  Intent intent = new Intent(this, SoundActivity.class);
                  intent.putExtra("SOUND", R.raw.siren);
                  startActivity(intent);
      
                  }
              });
      
      'in sound actvity you just get the sound using bundle and pass it to media player like'
      
      Bundle bundle = getIntent().getExtras();
              int sound = bundle.getInt("SOUND");
      
      sound1 = MediaPlayer.create(this, sound);
      
      sound1.start();
      
      
      
      
      'Hope you get the solution.'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        相关资源
        最近更新 更多