2

I am new to android. I have an audio file (mp3 file). I want to create an android app using android studio that plots a graph between loudness(dB) and time of the music file when it is played.How to implement this.

PS: Implementation for the same in HTML is
http://seapip.com/canvas/visualizer3/

My current code in android is:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.prateek.mediaplayer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mohit.mediaplayer.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"
        android:id="@+id/play"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause"
        android:id="@+id/pause"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

MainActivity.java

package com.example.prateek.mediaplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button play,pause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play=(Button)findViewById(R.id.play);
        pause=(Button)findViewById(R.id.pause);

        final MediaPlayer sound=MediaPlayer.create(MainActivity.this,R.raw.media);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sound.start();
            }
        });

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sound.pause();
            }
        });

    }
}
Prateek Ratnaker
  • 817
  • 11
  • 35
  • Have a look at `android.media.audiofx.Visualizer` and see if that will work for you. https://developer.android.com/reference/android/media/audiofx/Visualizer.html – kris larson Aug 23 '16 at 18:50
  • @krislarson Nice link, seems that the html5 audio api is very similair, since canvas drawing in android is also similair it shouldn't be an issue to convert the code. More info about drawing lines on a canvas in a android app: http://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android – seahorsepip Aug 23 '16 at 20:17
  • I can post the code yet again but you don't learn from that, try to create some line drawing method in the android app first by using some tutorials about canvas drawing in android apps. After you are able to draw a simple line from x1,y1 to x2,y2 you might try to copy the logic from the javascript example. – seahorsepip Aug 23 '16 at 20:19
  • @seahorsepip i will try doing that today but two questions 1. the webaudio api and audio api in android are similar.....2. can't we use some API like mpcharts which readily draws real time charts instead of doing this with canvas? – Prateek Ratnaker Aug 24 '16 at 03:48
  • You probably could but I'm not sure if it supports drawing charts that are constantly updated without it taking up too many system resources, you could send a tweet on twitter to the author asking about that or send an e-mail, but don't open an issue on github for questions :P – seahorsepip Aug 24 '16 at 06:47
  • Hi @PrateekRatnaker, did you figure the plot using MPAndroidChart? – swiftlearneer Aug 20 '21 at 14:34

0 Answers0