gpt4 book ai didi

java - 使用媒体播放器或音池在 fragment 内的 onClick 中播放声音

转载 作者:太空狗 更新时间:2023-10-29 13:12:29 25 4
gpt4 key购买 nike

我在一个 fragment 中有 2 个 ImageView 设置为可点击,我试图在点击每个 ImageView 时播放声音!我可以在 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);
}
}


}

最佳答案

如果你想使用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;
}
}

关于java - 使用媒体播放器或音池在 fragment 内的 onClick 中播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com