GithubHelp home page GithubHelp logo

bpelogia / audioview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 4ertuk/audioview

0.0 2.0 0.0 174 KB

Android audio view / MediaPlayer wrapper

License: Boost Software License 1.0

Java 100.00%

audioview's Introduction

Download

AudioView

Simple Android audio view with a few controls. Basically it's a MediaPlayer wrapper. You can choose AudioView2 and start a AudioService to start MediaPlayer in service. See below for more info (and demo app).

See picture

See demo app

Usage with Gradle

  1. Add dependency
dependencies {
    implementation 'com.4ert:audioview:0.4'
}
  1. Add layout
<com.keenfin.audioview.AudioView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  1. Set file data source
audioView.setDataSource("/path/to/file");
audioView.setDataSource(Uri);
audioView.setDataSource(FileDescriptor);
audioView.setDataSource(List<String/Uri/FileDescriptor>);
  1. Control playback if needed
audioView.start();
audioView.pause();
audioView.stop();

Usage AudioView2 in AudioService

Multiple AudioView2 with different tags can attach to service and play through it, but only one at a time. It's useful while placing AudioView2 in list or recycler view.

  1. Create AudioView2 in xml or by code
<com.keenfin.audioview.AudioView2
    android:id="@+id/audioview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  1. Start AudioService to handle playback
Intent audioService = new Intent(this, AudioService.class);
audioService.setAction(AudioService.ACTION_START_AUDIO);
startService(audioService);
  1. Assign tag for view and attach to service
AudioView2 audio = itemView.findViewById(R.id.audioview);
audio.setTag(position);
if (!audio.attached())
    audio.setUpControls();
try {
    audio.setDataSource(object.getPath());
} catch (IOException ignored) {
}
  1. Stop service when you do not need it anymore
Intent audioService = new Intent(this, AudioService.class);
stopService(audioService);

Attach to service to implement your own behaviour

You can attach to AudioService to implement your own view or other behaviour.

  1. Add service connection
private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        mService = ((AudioService.AudioServiceBinder) iBinder).getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mService = null;
    }
};
  1. Bind/unbind to service
private void bindAudioService() {
    Intent intent = new Intent(getContext(), AudioService.class);
    getContext().bindService(intent, mServiceConnection, 0);
}

private void unbindAudioService() {
    try {
        getContext().unbindService(mServiceConnection);
    } catch (Exception ignored) {
    }
}
  1. Add broadcast receiver to handle events
private BroadcastReceiver mAudioReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra("status", -1);
        int status = intent.getIntExtra("tag", -1);
        switch (status) {
            case AUDIO_PREPARED:
            case AUDIO_STARTED:
            case AUDIO_PAUSED:
            case AUDIO_STOPPED:
            case AUDIO_PROGRESS_UPDATED:
            case AUDIO_COMPLETED:
            case AUDIO_TRACK_CHANGED:
                break;
        }
    }
};
  1. Register/unregister receiver
private void registerAudioReceiver() {
    getContext().registerReceiver(mAudioReceiver, filter);
}

private void unregisterAudioReceiver() {
    try {
        getContext().unregisterReceiver(mAudioReceiver);
    } catch (Exception ignored) {
    }
}

Styles & options

primaryColor

Set default color for FAB and SeekBar. By default it uses colorAccent from AppTheme.

minified

Use alternative version of layout if true.

selectControls

Show (true by default) or hide rewind/forward buttons. Not available if minified.

showTitle

Show song's title if there is one. Default is true.

<com.keenfin.audioview.AudioView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:minified="true"
    app:primaryColor="@android:color/holo_blue_ligh"
    app:selectControls="false"
    app:showTitle="false"/>

setLoop(boolean)

Loop playlist (or single file).

audioview's People

Contributors

4ertuk avatar jeuler avatar

Watchers

James Cloos avatar Bruno Pelogia avatar

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.