[android]在 onClick 内播放声音 fragment 使用媒体播放器或soundpool

发布时间: 2016/9/16 18:48:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

2 图像视图在 fragment 设置为可点击和我想每一个单击时播放声音 ! 呢 activity 而不是在一个fragment ! 我正试着用到媒体播放器,但这引发了一个错误。

public class HomeFragment extends Fragment {


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //giving me error cannot resolve method
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.music_marimba_chord);

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        ImageView share = (ImageView)view.findViewById(R.id.share);

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
                startActivity(Intent.createChooser(sharingIntent, "Share via"));

        ImageView send = (ImageView)view.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
                startActivity(intent);
            }
        });



        return view;
    }

    // set fragment to portrait
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser) {
            Activity a = getActivity();
            if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }


}

解决方法 1:

如果您想要使用 SoundPool ,以下会播放不同的声音,当单击或选择事件触发对 fragment :

public abstract class MainFragment extends Fragment {

    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;

    public void onCreate() {
        initSounds(getActivity().getApplicationContext());
    }

    public void initSounds(Context context) {
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap(1);
        soundPoolMap.put(R.raw.music1, soundPool.load(context, R.raw.music1, 1));
        soundPoolMap.put(R.raw.music2, soundPool.load(context, R.raw.music2, 1));
    }

    public void playSound(int soundID) {

        float volume = 0.2f;

        // play sound with same right and left volume, with a priority of 1,
        // zero repeats (i.e play once), and a playback rate of 1f
        soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);
    }

    private void playSoundClick() {
        playSound(R.raw.music1);
    }

    private void playSoundSelect() {
        playSound(R.raw.music2);
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                playSoundClick();
            } else {
                playSoundSelect();
            }
        }
        return true;
    }
}

在这里播放的声音︰

soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);

在那里您可以设置卷左、 右边和优先万一你要这声音要战胜对方,如果另一个正在播放在同一SoundPool

要结合你的工作︰

public class HomeFragment extends MainFragment {


    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        super.onCreate();

        ImageView share = (ImageView)view.findViewById(R.id.share);

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                playSound(R.raw.music1);

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
                startActivity(Intent.createChooser(sharingIntent, "Share via"));

        ImageView send = (ImageView)view.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                playSound(R.raw.music2);

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
                startActivity(intent);
            }
        });

        return view;
    }
}
官方微信
官方QQ群
31647020