Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to upload mp3 from mobile and trim it? #3

Open
FazliMolaJan opened this issue Dec 10, 2019 · 7 comments
Open

how to upload mp3 from mobile and trim it? #3

FazliMolaJan opened this issue Dec 10, 2019 · 7 comments

Comments

@FazliMolaJan
Copy link

No description provided.

@avik-capitalnumbers
Copy link

Hi @FazliMolaJan ,
I have implemented this concept. so i am trying to give this solution.

if (checkStoragePermission()) {
pickUpFile();
} else {
requestStoragePermission();
}

private void pickUpFile(){
Intent intent=new Intent();
intent.setType(FileUtils.MIME_TYPE_AUDIO);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,CHOOSE_AUDIO);
}

@OverRide
protected void onActivityResult(int requestCode, int resultCode, @nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CHOOSE_AUDIO){

        if(resultCode == RESULT_OK){
            //the selected audio.
            Uri uri = data.getData();
            Log.d("Audio_Path","::::"+ RealPathUtil.getRealPath(this,uri));
            audioGraph(RealPathUtil.getRealPath(this,uri));

        }
    }

}


private void audioGraph(String mFilename) {

    mFile = new File(mFilename);
    mLoadingLastUpdateTime = Utility.getCurrentTime();
    mLoadingKeepGoing = true;
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setTitle("Loading ...");
    mProgressDialog.show();

    final SoundFile.ProgressListener listener =
            new SoundFile.ProgressListener() {
                public boolean reportProgress(double fractionComplete) {

                    long now = Utility.getCurrentTime();
                    if (now - mLoadingLastUpdateTime > 100) {
                        mProgressDialog.setProgress(
                                (int) (mProgressDialog.getMax() * fractionComplete));
                        mLoadingLastUpdateTime = now;
                    }
                    return mLoadingKeepGoing;
                }
            };

    // Load the sound file in a background thread
    Thread mLoadSoundFileThread = new Thread() {
        public void run() {
            try {
                mRecordedSoundFile = SoundFile.create(mFile.getAbsolutePath(), listener);
                if (mRecordedSoundFile == null) {
                    mProgressDialog.dismiss();
                    String name = mFile.getName().toLowerCase();
                    String[] components = name.split("\\.");
                    String err;
                    if (components.length < 2) {
                        err = "No Extension";
                    } else {
                        err = "Bad Extension";
                    }
                    final String finalErr = err;
                    Log.e(" >> ", "" + finalErr);
                    return;
                }
                mPlayer = new SamplePlayer(mRecordedSoundFile);
            } catch (final Exception e) {
                mProgressDialog.dismiss();
                e.printStackTrace();
                return;
            }
            mProgressDialog.dismiss();
            if (mLoadingKeepGoing) {
                Runnable runnable = new Runnable() {
                    public void run() {

                        audioWaveform.setIsDrawBorder(true);
                        finishOpeningSoundFile(mRecordedSoundFile, 0);
                        //txtAudioRecord.setBackgroundResource(R.drawable.ic_stop_btn1);
                        //txtAudioRecordTime.setVisibility(View.INVISIBLE);
                        txtStartPosition.setVisibility(View.VISIBLE);
                        txtEndPosition.setVisibility(View.VISIBLE);
                        markerEnd.setVisibility(View.VISIBLE);
                        markerStart.setVisibility(View.VISIBLE);
                       // llAudioCapture.setVisibility(View.GONE);
                       // rlAudioEdit.setVisibility(View.VISIBLE);
                        //txtAudioUpload.setVisibility(View.VISIBLE);

                       // txtAudioReset.setVisibility(View.VISIBLE);
                       // txtAudioCrop.setVisibility(View.GONE);
                       // txtAudioDone.setVisibility(View.VISIBLE);

                    }
                };
                mHandler.post(runnable);
            }
        }
    };
    mLoadSoundFileThread.start();
}

I hope it will be helpful for you.

@siddhant792
Copy link

Hi @FazliMolaJan ,
I have implemented this concept. so i am trying to give this solution.

if (checkStoragePermission()) {
pickUpFile();
} else {
requestStoragePermission();
}

private void pickUpFile(){
Intent intent=new Intent();
intent.setType(FileUtils.MIME_TYPE_AUDIO);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,CHOOSE_AUDIO);
}

@OverRide
protected void onActivityResult(int requestCode, int resultCode, @nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CHOOSE_AUDIO){

        if(resultCode == RESULT_OK){
            //the selected audio.
            Uri uri = data.getData();
            Log.d("Audio_Path","::::"+ RealPathUtil.getRealPath(this,uri));
            audioGraph(RealPathUtil.getRealPath(this,uri));

        }
    }

}


private void audioGraph(String mFilename) {

    mFile = new File(mFilename);
    mLoadingLastUpdateTime = Utility.getCurrentTime();
    mLoadingKeepGoing = true;
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setTitle("Loading ...");
    mProgressDialog.show();

    final SoundFile.ProgressListener listener =
            new SoundFile.ProgressListener() {
                public boolean reportProgress(double fractionComplete) {

                    long now = Utility.getCurrentTime();
                    if (now - mLoadingLastUpdateTime > 100) {
                        mProgressDialog.setProgress(
                                (int) (mProgressDialog.getMax() * fractionComplete));
                        mLoadingLastUpdateTime = now;
                    }
                    return mLoadingKeepGoing;
                }
            };

    // Load the sound file in a background thread
    Thread mLoadSoundFileThread = new Thread() {
        public void run() {
            try {
                mRecordedSoundFile = SoundFile.create(mFile.getAbsolutePath(), listener);
                if (mRecordedSoundFile == null) {
                    mProgressDialog.dismiss();
                    String name = mFile.getName().toLowerCase();
                    String[] components = name.split("\\.");
                    String err;
                    if (components.length < 2) {
                        err = "No Extension";
                    } else {
                        err = "Bad Extension";
                    }
                    final String finalErr = err;
                    Log.e(" >> ", "" + finalErr);
                    return;
                }
                mPlayer = new SamplePlayer(mRecordedSoundFile);
            } catch (final Exception e) {
                mProgressDialog.dismiss();
                e.printStackTrace();
                return;
            }
            mProgressDialog.dismiss();
            if (mLoadingKeepGoing) {
                Runnable runnable = new Runnable() {
                    public void run() {

                        audioWaveform.setIsDrawBorder(true);
                        finishOpeningSoundFile(mRecordedSoundFile, 0);
                        //txtAudioRecord.setBackgroundResource(R.drawable.ic_stop_btn1);
                        //txtAudioRecordTime.setVisibility(View.INVISIBLE);
                        txtStartPosition.setVisibility(View.VISIBLE);
                        txtEndPosition.setVisibility(View.VISIBLE);
                        markerEnd.setVisibility(View.VISIBLE);
                        markerStart.setVisibility(View.VISIBLE);
                       // llAudioCapture.setVisibility(View.GONE);
                       // rlAudioEdit.setVisibility(View.VISIBLE);
                        //txtAudioUpload.setVisibility(View.VISIBLE);

                       // txtAudioReset.setVisibility(View.VISIBLE);
                       // txtAudioCrop.setVisibility(View.GONE);
                       // txtAudioDone.setVisibility(View.VISIBLE);

                    }
                };
                mHandler.post(runnable);
            }
        }
    };
    mLoadSoundFileThread.start();
}

I hope it will be helpful for you.

can you please send me your source code on [email protected] ?

@aweklin
Copy link

aweklin commented Oct 3, 2020

@avik-capitalnumbers how do you use this in a Fragment? The implemented one with activity only showed the progress bar but not showing the trimming view.

@avik-capitalnumbers
Copy link

Yes, its possible. but before that, you have to learn about it how to implement this things in to fragment.

@aweklin
Copy link

aweklin commented Oct 4, 2020

Yes, its possible. but before that, you have to learn about it how to implement this things in to fragment.

I am able to record new audio within fragment with this library before processing other things. This was possible by calling the startActivityForResult static method and listen in the onActivityResult override method.

However, my requirement goes further to allow user select an already existing audio file and able to trim it before proceeding.
Can you please help with that?

@Wow-Gp
Copy link

Wow-Gp commented Oct 23, 2020

Hi @FazliMolaJan ,
I have implemented this concept. so i am trying to give this solution.

if (checkStoragePermission()) {
pickUpFile();
} else {
requestStoragePermission();
}

private void pickUpFile(){
Intent intent=new Intent();
intent.setType(FileUtils.MIME_TYPE_AUDIO);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,CHOOSE_AUDIO);
}

@OverRide
protected void onActivityResult(int requestCode, int resultCode, @nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CHOOSE_AUDIO){

        if(resultCode == RESULT_OK){
            //the selected audio.
            Uri uri = data.getData();
            Log.d("Audio_Path","::::"+ RealPathUtil.getRealPath(this,uri));
            audioGraph(RealPathUtil.getRealPath(this,uri));

        }
    }

}


private void audioGraph(String mFilename) {

    mFile = new File(mFilename);
    mLoadingLastUpdateTime = Utility.getCurrentTime();
    mLoadingKeepGoing = true;
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setTitle("Loading ...");
    mProgressDialog.show();

    final SoundFile.ProgressListener listener =
            new SoundFile.ProgressListener() {
                public boolean reportProgress(double fractionComplete) {

                    long now = Utility.getCurrentTime();
                    if (now - mLoadingLastUpdateTime > 100) {
                        mProgressDialog.setProgress(
                                (int) (mProgressDialog.getMax() * fractionComplete));
                        mLoadingLastUpdateTime = now;
                    }
                    return mLoadingKeepGoing;
                }
            };

    // Load the sound file in a background thread
    Thread mLoadSoundFileThread = new Thread() {
        public void run() {
            try {
                mRecordedSoundFile = SoundFile.create(mFile.getAbsolutePath(), listener);
                if (mRecordedSoundFile == null) {
                    mProgressDialog.dismiss();
                    String name = mFile.getName().toLowerCase();
                    String[] components = name.split("\\.");
                    String err;
                    if (components.length < 2) {
                        err = "No Extension";
                    } else {
                        err = "Bad Extension";
                    }
                    final String finalErr = err;
                    Log.e(" >> ", "" + finalErr);
                    return;
                }
                mPlayer = new SamplePlayer(mRecordedSoundFile);
            } catch (final Exception e) {
                mProgressDialog.dismiss();
                e.printStackTrace();
                return;
            }
            mProgressDialog.dismiss();
            if (mLoadingKeepGoing) {
                Runnable runnable = new Runnable() {
                    public void run() {

                        audioWaveform.setIsDrawBorder(true);
                        finishOpeningSoundFile(mRecordedSoundFile, 0);
                        //txtAudioRecord.setBackgroundResource(R.drawable.ic_stop_btn1);
                        //txtAudioRecordTime.setVisibility(View.INVISIBLE);
                        txtStartPosition.setVisibility(View.VISIBLE);
                        txtEndPosition.setVisibility(View.VISIBLE);
                        markerEnd.setVisibility(View.VISIBLE);
                        markerStart.setVisibility(View.VISIBLE);
                       // llAudioCapture.setVisibility(View.GONE);
                       // rlAudioEdit.setVisibility(View.VISIBLE);
                        //txtAudioUpload.setVisibility(View.VISIBLE);

                       // txtAudioReset.setVisibility(View.VISIBLE);
                       // txtAudioCrop.setVisibility(View.GONE);
                       // txtAudioDone.setVisibility(View.VISIBLE);

                    }
                };
                mHandler.post(runnable);
            }
        }
    };
    mLoadSoundFileThread.start();
}

I hope it will be helpful for you.

You saved my time.. Much Gratitude

@ghost
Copy link

ghost commented Apr 22, 2022

Will please share the complete source code @ [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants