你好我的项目当我点击一个按钮时我会播放一首歌这个按钮是一个片段布局,但是如果我需要将代码放在片段或MainActivity中,我现在不会。所以在这里我把它放在片段中,protected void
的" protected" 在红色下划线为#34;(this,resId)&# 34 ;.有人可以帮助感谢很多。
这是我的 fragment.java
public class thekairi78 extends Fragment {
private AdView adView;
private MediaPlayer mPlayer = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.thekairi78, container,
false);
Button regarde = (Button) getView().findViewById(R.id.regarde);
regarde.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.regarde);
}
});
adView = new AdView(getActivity());
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(AdSize.SMART_BANNER);
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout_admob);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
return rootView;
}
@Override
protected void onPause() {
if(mPlayer != null && mPlayer.isPlaying()){
mPlayer.pause();
}
super.onPause();
}
private void playSound(int resId){
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, resId);
mPlayer.start();
}
}
这里是 MainActivity.java
public class MainActivity extends AppCompatActivity {
private MediaPlayer mPlayer = null;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial = new InterstitialAd(MainActivity.this);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
thekairi78 tab1 = new thekairi78();
return tab1;
case 1:
kenny tab2 = new kenny();
return tab2;
case 2:
autres tab3 = new autres();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TheKairi78";
case 1:
return "Kenny";
case 2:
return "Autres SoundBox";
}
return null;
}
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
@Override
protected void onPause() {
if(mPlayer != null && mPlayer.isPlaying()){
mPlayer.pause();
}
super.onPause();
}
private void playSound(int resId){
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, resId);
mPlayer.start();
}
}