【问题标题】:play a sound in onClick within a fragment using media player or soundpool使用媒体播放器或声音池在片段内的 onClick 中播放声音
【发布时间】:2016-11-19 13:57:33
【问题描述】:

我在一个片段中有 2 个图像视图设置为可点击,我试图在每个被点击时播放声音!我可以在活动中做到这一点,但不能在片段中做到这一点!我正在尝试使用媒体播放器,但这会引发错误。

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);
        }
    }


}

【问题讨论】:

  • 发布您的错误日志。
  • 没有错误日志但仍在尝试实现媒体播放器,在我引用媒体播放器和原始文件的代码中,它给了我一个错误,即在创建媒体播放器对象时无法解析方法带有原始文件
  • 我不认为应该有任何问题。它对我有用。你检查过你的进口吗?

标签: java android android-studio android-fragments android-mediaplayer


【解决方案1】:

如果你想使用SoundPool,下面会在片段上触发点击或选择事件时播放不同的声音:

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;
    }
}

【讨论】:

  • 所以要在单击 2 个图像视图时播放此声音,我是否需要将图像视图添加到您发布的 playSoundClick 方法中?我以前从未与 soundpool 合作过
  • 啊我想我明白了,所以我不在 homefragment 中使用这个代码!所以我使用您发布的代码将其创建为一个新的 java 类,然后编辑我在上面发布的主片段代码以从主片段扩展?
  • 太棒了,我喜欢这个结构!非常感谢你,如果我需要声音,我将使用它并从主要片段中扩展我的其他片段!
  • 当我点击图像视图时似乎给了我一个空指针,NullPointerException:尝试在两个文件中调用虚拟方法,第一个在 soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);第二个在 playSound(R.raw.music_marimba_chord); 的 onclick 中;
  • 我的错,忘记调用 super.oncreate();完美,再次感谢您的时间和帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多