我在片段设置为可点击时有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);
}
}
}
答案 0 :(得分:2)
如果您想使用SoundPool
,则在片段上触发click或select事件时,以下内容会发出不同的声音:
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;
}
}