Android播放来自片段的声音

时间:2015-12-28 05:16:15

标签: java android android-fragments android-mediaplayer

我发现了多个类似的帖子,但一直无法解决,所以发布我的代码求助。

我有3个片段使用带有PagerAdapter的ViewPager来允许在片段之间滑动。这个项目没有碎片,我可以从MainActivity点击的按钮播放声音。

这是有效的代码。

MainActivity

    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    public class MainActivity extends AppCompatActivity {

    MediaPlayer example1Sound;
    MediaPlayer example2Sound;
    MediaPlayer example3Sound;
    MediaPlayer example4Sound;
    MediaPlayer example5Sound;
    MediaPlayer example6Sound;
    MediaPlayer example7Sound;
    MediaPlayer example8Sound;
    MediaPlayer example9Sound;

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

    example1Sound = MediaPlayer.create(this, R.raw.example1);
    example2Sound = MediaPlayer.create(this, R.raw.example2);
    example3Sound = MediaPlayer.create(this, R.raw.example3);
    example4Sound = MediaPlayer.create(this, R.raw.example4);
    example5Sound = MediaPlayer.create(this, R.raw.example5);
    example6Sound = MediaPlayer.create(this, R.raw.example6);
    example7Sound = MediaPlayer.create(this, R.raw.example7);
    example8Sound = MediaPlayer.create(this, R.raw.example8);
    example9Sound = MediaPlayer.create(this, R.raw.example9);


    }

    public void playExampleSound1(View view) {
    example1Sound.start();
    }

    public void playExampleSound2(View view) {
    example2Sound.start();
    }

    public void playExampleSound3(View view) {
    example3Sound.start();
    }

    public void playExampleSound4(View view) {
    example4Sound.start();
    }

    public void playExampleSound5(View view) {
    example5Sound.start();
    }

    public void playExampleSound6(View view) {
    example6Sound.start();
    }

    public void playExampleSound7(View view) {
    example7Sound.start();
    }

    public void playExampleSound8(View view) {
    example8Sound.start();
    }

    public void playExampleSound9(View view) {
    example9Sound.start();
    }
    }

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.anjosoft.MainActivity">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:layout_column="0"
            android:background="@drawable/button1"
            android:onClick="playExampleSound1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton2"
            android:layout_column="1"
            android:background="@drawable/button2"
            android:onClick="playExampleSound2" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton3"
            android:layout_column="2"
            android:background="@drawable/button3"
            android:onClick="playExampleSound3" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton4"
            android:layout_column="0"
            android:background="@drawable/button4"
            android:onClick="playExampleSound4" />

         <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton5"
            android:layout_column="1"
            android:background="@drawable/button5"
            android:onClick="playExampleSound5" />

         <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton6"
            android:layout_column="2"
            android:background="@drawable/button6"
            android:onClick="playExampleSound6" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton7"
            android:layout_column="0"
            android:background="@drawable/button7"
            android:onClick="playExampleSound7" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton8"
            android:layout_column="1"
            android:background="@drawable/button8"
            android:onClick="playExampleSound8" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton9"
            android:layout_column="2"
            android:background="@drawable/button9"
            android:onClick="playExampleSound9" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send Sound"
            android:id="@+id/checkBox"
            android:layout_column="1" />
     </TableRow>
</TableLayout>

我知道它不漂亮。应该制作一个阵列,但它仍然有效。

所以这就是我的问题。使用相同的意识形态,我将相同的东西与片段绑在一起。

MainActivity.java

    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.os.Bundle;

    public class MainActivity extends FragmentActivity {
    ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager)findViewById(R.id.pager);
        PagerAdapter padapter = new       PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(padapter);
    }
    }

PagerAdapter.java

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;

    public class PagerAdapter extends FragmentPagerAdapter{
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new FragmentOne();
            case 1:
                return new FragmentTwo();
            case 2:
                return new FragmentThree();
            default:
                break;
        }
        return null;
    }

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

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

FragmentOne.java

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class FragmentOne extends Fragment {

    MediaPlayer example1Sound;
    MediaPlayer example2Sound;
    MediaPlayer example3Sound;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
        example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
        example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);


        return    inflater.inflate(R.layout.fragment_one_layout,container,false);


    }

    public void playExampleSound1(View view){
        example1Sound.start();
    }

    public void playExampleSound2(View view) {
        example2Sound.start();
    }

    public void playExampleSound3(View view) {
        example3Sound.start();
    }
 }

fragnment_one_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"          android:layout_height="match_parent"
    android:background="#f70808">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton"
                android:layout_column="0"
                android:background="@drawable/button1"
                android:onClick="playExampleSound1"
                android:nestedScrollingEnabled="false" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton2"
                android:layout_column="1"
                android:background="@drawable/button2" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton3"
                android:layout_column="2"
                android:background="@drawable/button3" />
        </TableRow>
    </TableLayout>
    </RelativeLayout>

点击图片按钮会崩溃应用程序。我知道MediaPlayer可以在应用程序内部工作,因为我测试了插入example1Sound.start();像这样...

来自FragmentOne.java的

片段

       example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
       example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
       example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
       example1Sound.start();

片段加载时播放声音。如何让onclick工作?

2 个答案:

答案 0 :(得分:0)

你可以为每个按钮使用onClickListeners,它可以工作。

我认为,使用inflater将fragnment_one_layout.xml链接到FragmentOne.java 但是fragnment_one_layout.xml无法访问FragmentOne.java,因此 语句onClick =&#34; methodName&#34;无法访问所需的方法。   mediaPlayer对象与您的问题无关。

答案 1 :(得分:0)

您可以创建一个将声音从“活动”重定向到“片段”的功能! 在活动中

 fun onSound(sound:Int){
    mp = MediaPlayer.create(this,soundFile)
    mp.start
 } 

来自片段

 onResume{
   (activity as ActivityName).onSound(R.raw.sound)

}