如何编写代码以编辑录制的语音剪辑

时间:2018-07-11 06:09:29

标签: android

我开发了一个录音机应用程序,现在我在同一应用程序中还有另一个要求。这是我要编辑的录音片段,因为我在google中搜索了很多,但是我只有一个参考资料**(Ringdroid)**。但这有点令人困惑。

这是我的 RecordingListActivity

 private Toolbar toolbar;
    private RecyclerView recyclerViewRecordings;
    private ArrayList<Recording> recordingArraylist;
    private RecordingAdapter recordingAdapter;
    private TextView textViewNoRecordings;
    String recordingUri;

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

        recordingArraylist = new ArrayList<Recording>();
        initViews();
        fetchRecordings();
    }

    private void fetchRecordings() {

        File root = android.os.Environment.getExternalStorageDirectory();
        String path = root.getAbsolutePath() + "/VoiceRecorder/Audios";
        Log.d("Files", "Path: " + path);
        File directory = new File(path);
        File[] files = directory.listFiles();
        Log.d("Files", "Size: "+ files.length);

        if( files!=null ){

            for (int i = 0; i < files.length; i++) {

                Log.d("Files", "FileName:" + files[i].getName());
                String fileName = files[i].getName();
                recordingUri = root.getAbsolutePath() + "/VoiceRecorder/Audios/" + fileName;

                Recording recording = new Recording(recordingUri,fileName,false);
                recordingArraylist.add(recording);
            }
            textViewNoRecordings.setVisibility(View.GONE);
            recyclerViewRecordings.setVisibility(View.VISIBLE);
            setAdaptertoRecyclerView();

        }else{
            textViewNoRecordings.setVisibility(View.VISIBLE);
            recyclerViewRecordings.setVisibility(View.GONE);
        }
    }


    private void setAdaptertoRecyclerView() {
        recordingAdapter = new RecordingAdapter(this,recordingArraylist);
        recyclerViewRecordings.setAdapter(recordingAdapter);
    }

    private void initViews() {

        /** setting up the toolbar  **/
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Recording List");
        toolbar.setTitleTextColor(getResources().getColor(android.R.color.black));
        setSupportActionBar(toolbar);

        /** enabling back button **/
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /** setting up recyclerView **/
        recyclerViewRecordings = (RecyclerView) findViewById(R.id.recyclerViewRecordings);
        recyclerViewRecordings.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
        recyclerViewRecordings.setHasFixedSize(true);
        textViewNoRecordings = (TextView) findViewById(R.id.textViewNoRecordings);

    }
}

这是我的 RecordnigAdapter:

  private Context context;
    private ArrayList<Recording> recordingArrayList;
    private MediaPlayer mPlayer;
    private boolean isPlaying = false;
    private int last_index = -1;
    Recording recording;
    int mLastPosition = 0;

    public RecordingAdapter(Context context, ArrayList<Recording> recordingArrayList){
        this.context = context;
        this.recordingArrayList = recordingArrayList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.recording_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        setUpData(holder,position);

    }

    private void setUpData(ViewHolder holder, int position) {

        for (int i = 0; i < recordingArrayList.size(); i++) {

            recording = recordingArrayList.get(position);
            holder.textViewName.setText(recording.getFileName());
            mLastPosition = position;

            if (recording.isPlaying()) {
                holder.imageViewPlay.setImageResource(R.mipmap.ic_pause);
                TransitionManager.beginDelayedTransition((ViewGroup) holder.itemView);
                holder.seekBar.setVisibility(View.VISIBLE);
                holder.seekUpdation(holder);
            } else {
                holder.imageViewPlay.setImageResource(R.mipmap.ic_play);
                TransitionManager.beginDelayedTransition((ViewGroup) holder.itemView);
                holder.seekBar.setVisibility(View.GONE);
            }
        }

        holder.manageSeekBar(holder);

    }

    @Override
    public int getItemCount() {

        return recordingArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageViewPlay;
        SeekBar seekBar;
        TextView textViewName;
        Button share;
        Button edit;
        private String recordingUri;
        private int lastProgress = 0;
        private Handler mHandler = new Handler();
        ViewHolder holder;

        public ViewHolder(View itemView) {
            super(itemView);

            imageViewPlay = itemView.findViewById(R.id.imageViewPlay);
            seekBar = itemView.findViewById(R.id.seekBar);
            textViewName = itemView.findViewById(R.id.textViewRecordingname);
            share = itemView.findViewById(R.id.share);
            edit = itemView.findViewById(R.id.edit);

            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    File root = android.os.Environment.getExternalStorageDirectory();
                    String path = root.getAbsolutePath() + "/VoiceRecorder/Audios";
                    File f = new File(path);
                    File[] files = f.listFiles();
                    Log.d("Files", "Size: " + files.length);

                    if (files != null) {

                        for (int i = 0; i < files.length; i++) {

                            Log.d("Files", "FileName:" + files[i].getName());
                            String fileName = files[i].getName();
                            recordingUri = root.getAbsolutePath() + "/VoiceRecorder/Audios/" + fileName;
                            Uri uri = Uri.parse(recordingUri);
                            Intent share = new Intent(Intent.ACTION_SEND);
                            share.putExtra(Intent.EXTRA_STREAM, uri);
                            share.setType("audio/*");
                            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            context.startActivity(Intent.createChooser(share, "Share audio File"));
                        }
                    }
                }
            });

            edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    File root = android.os.Environment.getExternalStorageDirectory();
                    String path = root.getAbsolutePath() + "/VoiceRecorder/Audios";
                    File f = new File(path);
                    File[] files = f.listFiles();
                    Log.d("Files", "Size: " + files.length);

                    if (files != null) {

                        for (int i = 0; i < files.length; i++) {

                            Log.d("Files", "FileName:" + files[i].getName());
                            String fileName = files[i].getName();
                            recordingUri = root.getAbsolutePath() + "/VoiceRecorder/Audios/" + fileName;
                            Uri uri = Uri.parse(recordingUri);
                            Intent edit = new Intent(context, AudioEditActivity.class);
                            edit.putExtra(Intent.EXTRA_STREAM, uri);
                            edit.setType("audio/*");
                            context.startActivity(edit);
                        }
                    }
                }
            });

            imageViewPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    Recording recording = recordingArrayList.get(position);

                    recordingUri = recording.getUri();

                    if( isPlaying ){
                        stopPlaying();
                        if( position == last_index ){
                            recording.setPlaying(false);
                            stopPlaying();
                            notifyItemChanged(position);
                        }else{
                            markAllPaused();
                            recording.setPlaying(true);
                            notifyItemChanged(position);
                            startPlaying(recording,position);
                            last_index = position;
                        }

                    }else {
                        if( recording.isPlaying() ){
                            recording.setPlaying(false);
                            stopPlaying();
                            Log.d("isPlayin","True");
                        }else {
                            startPlaying(recording,position);
                            recording.setPlaying(true);
                            seekBar.setMax(mPlayer.getDuration());
                            Log.d("isPlayin","False");
                        }
                        notifyItemChanged(position);
                        last_index = position;
                    }
                }

            });
        }
        public void manageSeekBar(ViewHolder holder){
            holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if( mPlayer!=null && fromUser ){
                        mPlayer.seekTo(progress);
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
        }

        private void markAllPaused() {
            for( int i=0; i < recordingArrayList.size(); i++ ){
                recordingArrayList.get(i).setPlaying(false);
                textViewName.setText(recording.getFileName());
            }
            notifyDataSetChanged();
        }

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                seekUpdation(holder);
            }
        };

        private void seekUpdation(ViewHolder holder) {
            this.holder = holder;
            if(mPlayer != null){
                int mCurrentPosition = mPlayer.getCurrentPosition() ;
                holder.seekBar.setMax(mPlayer.getDuration());
                holder.seekBar.setProgress(mCurrentPosition);
                lastProgress = mCurrentPosition;
            }
            mHandler.postDelayed(runnable, 100);
        }

        private void stopPlaying() {
            try{
                mPlayer.release();
            }catch (Exception e){
                e.printStackTrace();
            }
            mPlayer = null;
            isPlaying = false;
        }

        private void startPlaying(final Recording audio, final int position) {
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(recordingUri);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e("LOG_TAG", "prepare() failed");
            }
            //showing the pause button
            seekBar.setMax(mPlayer.getDuration());
            isPlaying = true;

            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    audio.setPlaying(false);
                    notifyItemChanged(position);
                }
            });
        }}}

以上课程与录制语音剪辑有关,现在我要编辑录制的语音剪辑,请帮助我解决此问题。

0 个答案:

没有答案