0

我的音板有问题,我对编程很陌生,我需要一些专业人士的帮助

问题是当我按下主页按钮或返回按钮时我的音板不会停止我需要它来暂停声音或在这里停止它是代码希望你能提供帮助

package com.example.firstly;

import java.io.IOException; 
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Mymenu extends Activity {    
        int selectedSoundId; 

        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 

            setContentView(R.layout.activity_main); 
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            final MediaPlayer player = new MediaPlayer(); 
            final Resources res = getResources(); 

            //just keep them in the same order, e.g. button01 is tied to backtoyou 
            final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl, 
                                      R.id.amel, R.id.krz, R.id.mar, 
                                      R.id.sra, R.id.bab, R.id.har, 
                                      R.id.kur,  }; 
            final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama, 
                                      R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama, 
                                      R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama, 
                                      R.raw.oookurwa_ama,  }; 

            View.OnClickListener listener = new View.OnClickListener() { 
                public void onClick(View v) { 
                    //find the index that matches the button's ID, and then reset 
                    //the MediaPlayer instance, set the data source to the corresponding 
                    //sound effect, prepare it, and start it playing. 
                    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) { 
                                // TODO Auto-generated catch block 
                                e.printStackTrace(); 
                            } catch (IllegalStateException e) { 
                                // TODO Auto-generated catch block 
                                e.printStackTrace(); 
                            } catch (IOException e) { 
                                // TODO Auto-generated catch block 
                                e.printStackTrace(); 
                            } 
                            try { 
                                player.prepare(); 
                            } catch (IllegalStateException e) { 
                                // TODO Auto-generated catch block 
                                e.printStackTrace(); 
                            } catch (IOException e) { 
                                // TODO Auto-generated catch block 
                                e.printStackTrace(); 
                            } 
                            player.start(); 
                            break; 
                        } 
                    } 
                } 
            }; 


            //set the same listener for every button ID, no need 
            //to keep a reference to every button 
            for(int i = 0; i < buttonIds.length; i++) { 
                Button soundButton = (Button)findViewById(buttonIds[i]); 
                registerForContextMenu(soundButton); 
                soundButton.setOnClickListener(listener); }
            } 


        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();


        } 

    } 
4

1 回答 1

0

在您的活动中停止或暂停播放器的暂停,使用下面的代码

package com.example.firstly;

import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Mymenu extends Activity {
    int selectedSoundId;
    MediaPlayer player;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        player = new MediaPlayer();
        final Resources res = getResources();

        // just keep them in the same order, e.g. button01 is tied to backtoyou
        final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl, R.id.amel,
                R.id.krz, R.id.mar, R.id.sra, R.id.bab, R.id.har, R.id.kur, };
        final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama,
                R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama,
                R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama,
                R.raw.oookurwa_ama, };

        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                // find the index that matches the button's ID, and then reset
                // the MediaPlayer instance, set the data source to the
                // corresponding
                // sound effect, prepare it, and start it playing.
                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) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            player.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        player.start();
                        break;
                    }
                }
            }
        };

        // set the same listener for every button ID, no need
        // to keep a reference to every button
        for (int i = 0; i < buttonIds.length; i++) {
            Button soundButton = (Button) findViewById(buttonIds[i]);
            registerForContextMenu(soundButton);
            soundButton.setOnClickListener(listener);
        }
    }

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        player.stop(); // to stop the player
        player.release(); // if you want to pause the song use player.pause();

    }

}
于 2013-09-22T13:43:29.347 回答