0

I want my app to get the MediaMetaData (song title/artist/album) of the currently playing audio. I can see this info being printed to logcat on my computer under updateMediaMetadata, so I'm trying to extract that info somehow.

enter image description here

EDIT: So logcat definitely is not the way to go for doing this, when I can make/detect MediaMetadata calls. I'm trying to make a new class that prints out the title of the currently playing MediaMetadata when I call a function but My code isn't compiling and I was wondering if it has something to do with how I set up my class? From what I understand of the documentation:

https://developer.android.com/reference/android/media/MediaMetadata

I'm trying to extend the class to utilize its functions like so:

package com.example.crisisapp;

import android.media.AudioAttributes;
import android.media.MediaMetadata;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.media.session.MediaSession.QueueItem;
import android.util.Log;
import android.os.Bundle;

import java.util.List;

public class MediaController extends android.media.MediaMetadata{

MediaController(Bundle bundle) {
    super(bundle);
}

public void getMediaMetadata(){
    Log.i("myTag", "inside getMediaMetadata()");
    MediaMetadata mc = new MediaMetadata();
    //String title = mc.getString("METADATA_KEY_TITLE");
    //Log.i("myTag", "METADATA_KEY_TITLE = "+title);
}

}

And my MainActivity calls this function like so:

public void startTrackingMusic(View view) {
    Log.i("myTag", "start of startTrackingMusic()");

    MediaController mc = new MediaController();

    final long NANOSEC_PER_SEC = 1000l*1000*1000;
    long startTime = System.nanoTime();
    while ((System.nanoTime()-startTime)< 2*60*NANOSEC_PER_SEC){
        Log.i("myTag", "loop");
        mc.getMediaMetadata();

    }
    Log.i("myTag", "end of startTrackingMusic()");
}

When I try to build my project it gives errors saying I cannot inherit from final MediaMetadata and so on:

enter image description here

What am I missing that needs to be done to declare MediaMetadata correctly and retrieve the Title string?

Martin
  • 1,336
  • 4
  • 32
  • 69
  • seems you need `MediaController.Callback#onMetadataChanged` instead – pskink Mar 10 '19 at 07:54
  • Use Logcat for development purposes only. Use tags in your Log and filter by the application name and tag. This will give you a cleaner and precise Logcat. Read about using Logcat here: https://developer.android.com/studio/debug/am-logcat – MD Naseem Ashraf Mar 10 '19 at 08:26
  • @pskink I'll look into MediaController.Callback#onMetadataChanged, need to figure out how to use this asynchronously to run some code whenever the field changes. – Martin Mar 10 '19 at 10:14
  • @pskink I added an edit to my question with what I'm trying to do now, I'm having some trouble declaring a new MediaMetadata object without running into any build errors if you have any advice – Martin Mar 10 '19 at 19:08
  • "I'm having trouble declaring a new MediaMetadata object without running into build errors though" is not a valid problem statement on this site. Please see the guidance on how to ask an answerable question - that you would need to actually *include the error messages in your question* being the more obvious part. – Chris Stratton Mar 10 '19 at 19:09
  • @ChrisStratton edited to include better problem statement – Martin Mar 10 '19 at 19:27
  • If you can't extend it, you can wrap it... but you are mostly likely going about this whole thing in the wrong way. Close that IDE and spend some time researching usual solutions to the problem you need to solve, here and elsewhere. – Chris Stratton Mar 10 '19 at 19:31
  • @ChrisStratton I've tried creating a MediaMetadata object before but found all the online examples were calling metaRetriever.setDataSource("filename") for a file. https://stackoverflow.com/questions/55036223/use-setdatasource-to-get-mediametadata-from-currently-playing-music-app – Martin Mar 10 '19 at 19:36
  • Don't post multiple question on the same problem. – Chris Stratton Mar 10 '19 at 19:38

0 Answers0