MediaPlayer播放它不应该播放的文件

时间:2016-01-25 16:22:35

标签: java android

我正在创建一个小SoundPlayer,我有一个问题。每次我点击一个文件的播放按钮,它播放资产文件夹中的所有文件...这是我的代码: 我的一个碎片:

package com.example.admin.fio;

import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.IOException;

public class FragmentDominik extends Fragment implements View.OnClickListener{
    Button button1, button2, button3, button4, button5, button6, button7, button8;
    View view;
    Homescreen home = new Homescreen();
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dominik,container, false);
        return view;
    }

    public void onClick(final View v) { //check for what button is pressed
        switch (v.getId()) {
            case R.id.dominikButton1:
                playSound("dome__armin.mp3");
                break;
            case R.id.dominikButton2:
                playSound("dome__heeey.mp3");
                break;
            case R.id.dominikButton3:
                playSound("dome__heey_was_geht.mp3");
                break;
        }
    }

    public void playSound(String fileName) {
        try {
            AssetFileDescriptor descriptor = view.getContext().getAssets().openFd(fileName);
            home.mP.setDataSource(descriptor.getFileDescriptor());
            home.mP.prepare();
            home.mP.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我的Homescreen.java我正在努力:

package com.example.admin.fio;

import android.os.Bundle;
import android.media.MediaPlayer;
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;

public class Homescreen extends FragmentActivity {
    ViewPager viewPager = null;
    public MediaPlayer mP = new MediaPlayer();
    protected void onCreate(Bundle savedInstanceStats)
    {
        super.onCreate(savedInstanceStats);
        setContentView(R.layout.homescreen);
        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }
}

class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if (position == 0)
            fragment = new FragmentDominik();
        else if (position == 1)
            fragment = new FragmentTobias();
        else if (position == 2)
            fragment = new FragmentTom();
        else if (position == 3)
            fragment = new FragmentNikolas();
        else if (position == 4)
            fragment = new FragmentGroups();
        else if (position == 5)
            fragment = new FragmentOthers();
        else if (position == 6)
            fragment = new FragmentFavorites();
        else if (position == 7)
            fragment = new FragmentConsole();
        return fragment;
    }

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

    public CharSequence getPageTitle(int position)
    {
        String title = new String();
        if(position == 0)
            return "Dominik";
        else if(position == 1)
            return "Tobias";
        else if(position == 2)
            return "Tom";
        else if(position == 3)
            return "Nikolas";
        else if(position == 4)
            return "Groups";
        else if(position == 5)
            return "Others";
        else if(position == 6)
            return "Favorites";
        else if(position == 7)
            return "Console";
        return null;
    }
}

我不知道这是否重要,但这是我的布局文件:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <LinearLayout
                android:layout_marginTop="5dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/dominikButton1"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"/>

                <Button
                    android:id="@+id/dominikButton2"
                    android:layout_marginRight="12dp"
                    android:layout_marginLeft="12.5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal" />

                <Button
                    android:id="@+id/dominikButton3"
                    android:layout_marginRight="5dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:text=""
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
            <LinearLayout
                android:layout_marginTop="5dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/dominikButton4"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"/>

                <Button
                    android:id="@+id/dominikButton5"
                    android:layout_marginRight="12dp"
                    android:layout_marginLeft="12.5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal" />

                <Button
                    android:id="@+id/dominikButton6"
                    android:layout_marginRight="5dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:text=""
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
            <LinearLayout
                android:layout_marginTop="5dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/dominikButton7"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"/>

                <Button
                    android:id="@+id/dominikButton8"
                    android:layout_marginRight="12dp"
                    android:layout_marginLeft="12.5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/round_button"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

提前感谢:)

0 个答案:

没有答案