0

現在のコード:

   MediaPlayer mp;

    button1=(ImageButton)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), Activity1.class);
            startActivity(i);
        }

    });

では、ボタンはどのようにして新しいアクティビティを開始し、その間にサウンドを再生するのでしょうか?

私が試したこと: playSound( ); のようなさまざまな方法 メソッドで。

デフォルトのAndroidサウンドのみを再生します。raw ディレクトリに保存されている特定のサウンドが必要です。したがって、ボタンが押されると、アクティビティを起動するインテントと特定のサウンドの両方が起動されます。

エラー:

MediaPlayer mp; を入れようとすると、ボタンの上に、変数 mp が既に定義されていることが示されます。サウンドも再生されるように、アクティビティの起動コードを追加する人が必要です。

4

3 に答える 3

0
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), Uri.parse(""));
        mp.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer player) {
                // TODO Auto-generated method stub
                player.start();
                Intent i = new Intent(getApplicationContext(), Activity1.class);
                startActivity(i);
            }
        });
于 2015-03-24T14:54:58.943 に答える
0


まず、sound.mp3 を raw フォルダーMediaPlayer mp;に入れる必要があります。

button1=(ImageButton)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        try {
                    mp = MediaPlayer.create(Music.this, R.raw.sound);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mp.start();
        Intent i = new Intent(getApplicationContext(), Activity1.class);
        startActivity(i);
    }

});
于 2015-03-24T15:09:28.203 に答える