Coder Social home page Coder Social logo

rygelouv / android-audio-sensei Goto Github PK

View Code? Open in Web Editor NEW
43.0 5.0 19.0 5.42 MB

High-level library to make android audio recording and playing more simple by handling boring stuff like runtime permissions and by completely abstracting audio playback controller

License: Apache License 2.0

Java 99.62% Kotlin 0.38%
android android-library audio audio-player audio-recorder mediaplayer

android-audio-sensei's Introduction

Introduction

Android Audio Sensei is a high-level library that aims to make working with audio on Android more simple. The reason is simple, when it comes to work with audio there is a lot of work to do before to get the feature you want for example:

  • Requesting permission before recording audio
  • Handling the permission when granted
  • Handling playback control (pause, play, stop) on the MediaPlayer with your UI
  • Reporting the media playing progress using a seekbar or something similar
  • Ensure to release the MediaPlayer when not needed anymore

When it comes to do the same audio playing in a list like a Recyclerview, this becomes more complicated

  • Stop and reset the current playing audio before playing another
  • As Recyclerview's items are not directly bounded to the Activity/Fragment lifecycle, releasing the MediaPlayer may become a problem
  • Most of the times on list of audio track (like in an Audion Player app or a chat app like whatsapp), wen you play hit the pause button, the play button appear and vice versa. Doing the same thing in a list items is a bit problematic
  • Etc...

Audio Sensei take care of all of this for you and provide a very simple way to record and play audio even in a Recyclerview

AudioSensei library does not use any other library to do its job

In your project root build.gradle with:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

and in the app or module build.gradle:

dependencies {
    compile 'com.github.rygelouv:android-audio-sensei:0.1.2'
}

Recording audio

You simply need an instance of AudioSensei class to start recording

Using Recorder()

AudioSensei.Recorder()
        .with(MainActivity.this)
        .name("file_name")
        .to(AudioRecordInfo.AudioPath.APP_PUBLIC_MUSIC)
        .start();

Handling runtime permission

When call startRecording(), AudioSensei check first if the RECORD_AUDIO permission is granted if not the permission is requested so you don't have to do it yourself.

To make sure the AudioSensei will start recording after the permission is granted, you need to override the method onRequestPermissionsResult and tell AudioSensei to do the rest

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
    AudioSensei.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Stop or cancel recording

AudioSensei.getInstance().stopRecording(); // Call this when you are done recording
AudioSensei.getInstance().cancelRecording(); // when the recorder is stopped, AudioSensei does not record any file

Get the last recorded file

To get the last file you just recorded just after the record stop

AudioSensei.getInstance().getLastRecordedOutputFile()

Create a player view

Most of the time an audio player just follow the same pattern. You have 2 buttons, Play and Pause, sometimes the stop button and the view that indicate the progress which mostly a SeekBar. After creating the view most of the next step is to get an instance of MediaPlayer and play/pause/stop depending on which button is clicked. Handling the progress is done by using MediaPlayer.getCurrentPosition()

Now gess what you don't need to do any of that now. With AudioSensei library, you can just do this:

<com.rygelouv.audiosensei.player.AudioSenseiPlayerView
        android:id="@+id/audio_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Even when you put this as item for a Recyclerview, nothing changes. AudioSensei knows if a media is being played and will stop the current media in order to play the new one.

Provide an Audio target

AudioSensei library considers any sound to play as a target be it remote sound (on the internet) or local sound file (on the phone). To set a target you just need to call one of the implementation of the method setAudioTarget

setAudioTarget(String url) // If you want to play remote file in streaming mode. Can also be used for local files
setAudioTarget(int resource) // If you want to play resource in your android project or APK
setAudioTarget(Uri uri) // If you want to play a local file on the user disk storage

You just set a target and you're done!

Using with recyclerView

If you are using AudioSenseiPlayerView in recyclerview, you must tell AudioSensei to track down the lifecycle by adding this code:

AudioSenseiListObserver.getInstance().registerLifecycle(getLifecycle());

Add custom layout

If you want to add you owm custom design (i encourage you to do so), you just need to provide your layout file via the attribute app:custom_layout="@layout/custom_player"

<com.rygelouv.audiosensei.player.AudioSenseiPlayerView
        android:id="@+id/audio_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:custom_layout="@layout/custom_player"
        />

To make this work properly you need to provide same id for player views:

  • button_play : For play button
  • button_pause: For pause button
  • seekbar_audio: For SeekBar that shows audio progress

Register click listeners for the view inside your custom layout

If you added a custom layout as i just shown above, perhaps you gonna need to perform clicks on views inside that custom layout of yours. So to do that, you need to register click listener for your views in AudioSenSeiPlayerView by specifying the view that you want to handle the click and the callback using OnPlayerViewClickListenerlike this:

audioSenseiPlayerView1.registerViewClickListener(R.id.stop, new OnPlayerViewClickListener()
        {
            @Override
            public void onPlayerViewClick(View view)
            {
                Log.i(TAG, "onPlayer view Clicked");
                audioSenseiPlayerView1.stop();
            }
        });

Here, we registered a click for a stop custom actions that we added in the custom layout as the stop button is not provided by default in AudioSenseiPlayerView

After registering the view and the call back, YOU MUST CALL commitClickEvents otherwise your events won't be triggered.

audioSenseiPlayerView1.commitClickEvents();

Get the player rootView

If you want to manipulate the views inside the player yourselft you can get it's rootView

View playerRootView = audioSenseiPlayerView1.getPlayerRootView();

Proguard

If you are using proguard, you need to add this line in your proguard-rules.pro file

-dontwarn com.rygelouv.audiosensei.**

TODO

This is library is still in active development. So you may encounter some bugs please create issues. This is what remains :

  • Handle Audio Focus
  • Fix some bugs
  • Allow to play audio in a background Service to keep playing even when the user leave the app or the current page and provide controls in a notification

Credits

Author: Rygel Louv http://www.rygelouv.wordpress.com/

License

Copyright 2017 Rygelouv.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-audio-sensei's People

Contributors

rygelouv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

android-audio-sensei's Issues

Auto play

can we make it auto play with url on page load?

Audio Stop Playing on RecyclerView Scroll

Hi,
I am using AudioSenseiPlayerView in RecyclerView for multi audios but when i am scrolling RecyclerView, it stop playing audio, please describe how can i keep playing clicked audio on RecyclerView Scroll?

UI problem

Hi
I have a problem with your project. Each time i play a music and scroll up and down other progress bar will moving, and also UI will not response after click play button and just before music start to play.

Can I synchronize the Player view?

Can I synchronize the Player view running in the service with the Player view of the activity?

There is a player playing in the service for background playback. I'm looking for a way to synchronize it with an activity's player when the user opens the activity again,

I hope you can help me.

The way I tried.

Static PLAY_SERVICE;


MainActivity
@onCreate
if(isServiceRunning ==true){
audioplayerView = PLAY_SERVICE.audioplayerView;

audioplayerView .play(); //It is possible to control through the method, but not through the player view of the activity.

}

AudioSensei overload resolution ambiguity

Hi! I implemented AudioSensei into my project and encountered an error. My project is written on Kotlin. The error I get is in line
AudioSensei.Recorder().with(MainActivity.this).name("file_name").to(AudioRecordInfo.AudioPath.APP_PUBLIC_MUSIC).start();

Mistake: Overload resolution ambiguity: public constructor Recorder () defined in com.rygelouv.audiosensei.recorder.AudioSensei.Recorder public open fun Recorder (): AudioSensei.Recorder! defined in com.rygelouv.audiosensei.recorder.AudioSensei

Unable to store the recorded audio file.

Hi, the library is unable to store recorded audio file. The folder "Music" is created into the files folder of the app, but not the audio file.

AudioSensei.Recorder() .with(ChatActivity.this) .name("swapaudimessage") .to(AudioRecordInfo.AudioPath.APP_PUBLIC_MUSIC) .start();

It works with MediaRecorder object, the file is stored, but then the app crashes.
Please help me out in this issue. because I can switch to any other library right now.
Thanks :)

SetAudioTarger method not reading data

When I pass this path: /sdcard/MyFile/myRecod.mp3 to the SetAudioTarget method , the file is not playable when I click on the Play buton and the chronometer is not updated to the duration to of the audio file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.