BeatBox完整代码及最终截图

2017-05-23  本文已影响0人  圈圈_勿忘初心
1 2 工程目录结构
//BeatBox.java

package com.bignerdranch.android.beatbox;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BeatBox {
    private static final String TAG="BeatBox";
    private static final String SOUNDS_FOLDER="sample_sounds";
    private static final int MAX_SOUNDS=5;
    private AssetManager mAssets;
    private List<Sound> mSounds=new ArrayList<>();
    private SoundPool mSoundPool;
    public BeatBox(Context context){
        mAssets=context.getAssets();
        mSoundPool=new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC,0);
        loadSounds();
    }
    public void play(Sound sound){
        Integer soundId=sound.getSoundId();
        if (soundId==null){
            return;
        }
        mSoundPool.play(soundId,1.0f,1.0f,1,0,1.0f);
    }
    public void release(){
        mSoundPool.release();
    }
    private void loadSounds(){
        String[] soundNames;
        try{
            soundNames=mAssets.list(SOUNDS_FOLDER);
            Log.i(TAG,"Found"+soundNames.length+"sounds");
        }catch (IOException ioe){
            Log.e(TAG,"Could not list assets",ioe);
            return;
        }
        for (String filename:soundNames){
            try{
                String assetPath=SOUNDS_FOLDER+"/"+filename;
                Sound sound=new Sound(assetPath);
                load(sound);
                mSounds.add(sound);
            }catch (IOException ioe){
                Log.e(TAG,"Couldn't load sound "+filename,ioe);
            }
        }
    }
    public List<Sound> getSounds(){
        return mSounds;
    }
    private void load(Sound sound)throws IOException{
        AssetFileDescriptor afd=mAssets.openFd(sound.getAsserPath());
        int soundId=mSoundPool.load(afd,1);
        sound.setSoundId(soundId);
    }
}
//BeatBoxActivity.java

package com.bignerdranch.android.beatbox;

import android.support.v4.app.Fragment;

public class BeatBoxActivity extends SingleFragmentActivity {

    @Override
    protected Fragment createFragment(){
        return BeatBoxFragment.newInstance();
    }
}
//BeatBoxFragment.java

package com.bignerdranch.android.beatbox;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.List;

public class BeatBoxFragment extends Fragment {
    private BeatBox mBeatBox;
    public static BeatBoxFragment newInstance(){
        return new BeatBoxFragment();
    }
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mBeatBox=new BeatBox(getActivity());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.fragment_beat_box,container,false);
        RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.fragment_beat_box_recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        recyclerView.setAdapter(new SoundAdapter(mBeatBox.getSounds()));
        return view;
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        mBeatBox.release();
    }
    private class SoundHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private Button mButton;
        private Sound mSound;
        public SoundHolder(LayoutInflater inflater,ViewGroup container){
            super(inflater.inflate(R.layout.list_item_sound,container,false));
            mButton=(Button)itemView.findViewById(R.id.list_item_sound_botton);
            mButton.setOnClickListener(this);
        }
        public void bindSound(Sound sound){
            mSound=sound;
            mButton.setText(mSound.getName());
        }
        @Override
        public void onClick(View v){
            mBeatBox.play(mSound);
        }
    }
    private class SoundAdapter extends RecyclerView.Adapter<SoundHolder>{
        private List<Sound> mSounds;
        public SoundAdapter(List<Sound> sounds){
            mSounds=sounds;
        }
        @Override
        public SoundHolder onCreateViewHolder(ViewGroup parent,int viewType){
            LayoutInflater inflater=LayoutInflater.from(getActivity());
            return new SoundHolder(inflater,parent);
        }
        @Override
        public void onBindViewHolder(SoundHolder soundHolder,int position){
            Sound sound=mSounds.get(position);
            soundHolder.bindSound(sound);
        }
        @Override
        public int getItemCount(){
            return mSounds.size();
        }
    }
}
//SingleFragmentActivity.java

package com.bignerdranch.android.beatbox;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;


public abstract class SingleFragmentActivity extends AppCompatActivity{
    protected abstract Fragment createFragment();

    @LayoutRes
    protected int getLayoutResId(){
        return R.layout.activity_fragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());

        FragmentManager fm=getSupportFragmentManager();
        Fragment fragment=fm.findFragmentById(R.id.fragment_container);

        if(fragment==null){
            fragment=createFragment();
            fm.beginTransaction().add(R.id.fragment_container,fragment).commit();
        }
    }
}
//Sound.java

package com.bignerdranch.android.beatbox;

public class Sound {
    private String mAssetPath;
    private String mName;
    private Integer mSoundId;
    public Sound(String assetPath){
        mAssetPath=assetPath;
        String[] components=assetPath.split("/");
        String filename=components[components.length-1];
        mName=filename.replace(".wav","");
    }
    public String getAsserPath(){
        return mAssetPath;
    }
    public String getName(){
        return mName;
    }
    public Integer getSoundId(){
        return mSoundId;
    }
    public void setSoundId(Integer soundId){
        mSoundId=soundId;
    }
}
<!-- activity_fragment.xml-->

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
/>
<!-- fragment_beat_box.xml-->

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_beat_box_recycler_view"
    />
<!-- list_item_sound.xml-->

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_margin="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/list_item_sound_botton"
        android:layout_gravity="center"
        tools:text="Sound name"/>
</FrameLayout>
<!-- colors.xml-->

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="red">#F44336</color>
    <color name="dark_red">#C3352B</color>
    <color name="gray">#607D8B</color>
    <color name="soothing_blue">#0083BF</color>
    <color name="dark_blue">#005A8A</color>
</resources>
<!-- styles.xml-->

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryDark">@color/dark_red</item>
        <item name="colorAccent">@color/gray</item>
        <item name="android:colorBackground">@color/soothing_blue</item>
        <item name="android:buttonStyle">@style/BeatBoxButton</item>
    </style>
    <style name="BeatBoxButton" parent="android:style/Widget.Holo.Button">
        <item name="android:background">@drawable/button_beat_box</item>
    </style>

</resources>
上一篇下一篇

猜你喜欢

热点阅读