简体   繁体   中英

Should I use Fragments for Audio Processing? (Android)

I am curious what the best practice would be for the following: 1. main activity is just gui with buttons 2. on button press it should start a new audio recorder session, record, and process 3. when finished processing it should return to the main activity with data

So, what would the best practice (architecture) be for doing this? Should I use a fragment in the main activity, or call a second activity, or something else?

I would like it setup so I can call the recording session fragments/activity from several locations in the code and have it return the processed audio.

Thanks for the comments! Any code examples would be amazing! Thank you.

I would recommend a new Activity with a Fragment acting as a controller for the recorder UI and a singleton Class acting as the recorder.

You could use Interfaces to communicate between those parts, but I believe the best approach would be to use EventBus (I think its more reliable).

In your recorder Class you create all the logic behind the scenes. Specially the MediaRecorder status , because that is a little tricky. So, when your recorder changes its status, like recording -> stop, you send a EventBus informing the status. Your Fragment receive those Events and adjust the UI accordingly.

  1. Fragment receive user call and send an EventBus with the request
  2. Recorder Class receives the EventBus, process it and keep the Fragment aware of its status sending Events

That's the way I would do. But's I guess it's a matter of opinion, right?

Best of luck with your code.

A reliable EventBus library. library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM