Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was looking for the same thing and I was able to do it but it was not a straight forward process. </p> <p>First I used the <a href="https://bitbucket.org/artgillespie/tslibraryimport" rel="nofollow">TSLibraryExport</a> to export the song from the iPod into my app directory. The reason is that after many attempts, 1) AVPlayer and MPMusicPlayerController simply won't give you the decibel levels. 2) AVAudioPlayer won't read the MPMediaItemPropertyAssetURL from the MediaPicker no matter what. Seems like the NSURL from the iPod is not the real file path, therefore it just won't read it. At least I couldn't do it this way.</p> <p>So,</p> <p>1) Downlaod the TSLibraryExport and you will see the methods for exporting from the iPod to your app. I just used these from that library:</p> <pre><code>-(IBAction) handleExportTapped {} -(void) updateExportProgress: (NSTimer*) timer {} //this is actually nice to display the progress -(void) setUpAVPlayerForURL: (NSURL*) url {} //this is triggered after the export is done static BOOL coreAudioCanOpenURL (NSURL* url) {} static NSString* myDocumentsDirectory() {} </code></pre> <p>2) Once you are able to export from iPod to your app, initialize the AVAudioPlayer in the setUpAVPlayerForURL or whatever your method name is.</p> <pre><code>-(void) setUpAVPlayerForURL: (NSURL*) url { newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil]; [newPlayer setMeteringEnabled:YES]; [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(getLevel:) userInfo:nil repeats: YES]; [newPlayer play];} </code></pre> <p>newPlayer is an AVAudioPlayer instance variable declared in your header file.</p> <p>3) Now that you have imported the song into your app and it's playing you can show the decibels how you want. In my test I just dumped them into a Label.</p> <pre><code>- (void)getLevel:(NSTimer *)timer { if (newPlayer.playing == YES) { [newPlayer updateMeters]; dBLevel = [newPlayer averagePowerForChannel:1]; //dBlevel is a float ivar declared in my header file dBLabel.text = [NSString stringWithFormat:@"%.01f",dBLevel]; //%.01f is just to show 2 decimals nowPlayingLabel.text = [NSString stringWithFormat:@"%.01f",[newPlayer peakPowerForChannel:1]]; } </code></pre> <p>The bad thing about this is that since you are copying the file, your app would need more space, and second, I think the user might have a file that won't export because of different file types, compatibility, blah blah blah... </p> <p>I hope this helps. I've spent a few days trying to come up with a solution after reading a lot online but this seemed the only one that made it possible. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload