将MediaPlayer放在片段中的位置?

时间:2013-06-12 23:35:16

标签: android android-fragments media-player

当我到达一个新片段(通过滑动)时,我想让我的声音文件自动播放。现在,声音没有播放。

之间可能存在根本区别
public void OnAttach(Activity Fragment1){ 

public void OnAttach(Fragment Fragment1){

感谢您的帮助。代码:

package com.example.test21;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity implements

ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ActionBar actionBar = getActionBar();
        actionBar.hide();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
             switch (position) {
                case 0:
             return new Fragment0();
                case 1:
             return new Fragment1();  
                case 2:
             return new Fragment2();
                default:
             return null;
        }}

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

    public static class Fragment0 extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_0, null); 
            return view;
        }
}

public static class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, null); 
        return view;
    }
    public void OnAttach(Activity Fragment1){
        super.onAttach(Fragment1);
        MediaPlayer mp = MediaPlayer.create(Fragment1.getApplication(), R.raw.s1);
        mp.start();
    }
}
        //This fragment is supposed to start the MediaPlayer, but is doesn't
    public static class Fragment2 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_2, null); 
            return view;
        }
    }

}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

如果你想在开始新片段时播放声音,你可以把它放在方法onCreateView()上。

答案 2 :(得分:0)

媒体播放器的创建需要上下文,但片段没有上下文

要在片段中创建Media Player,您需要使用getActivity()获取上下文

示例:

// creating media player in a fragment
    happyBirthdaySong = MediaPlayer.create(getActivity(), R.raw.happy_birthday_female);
    happyBirthdaySong.setLooping(true);
    happyBirthdaySong.start();