I had this problem the other day where I needed to get just the audio url from a post. I was doing this from within “The Loop”. There may be several ways to do this. Here is the way that worked for me.

1
 $audioUrl2 = strtok(get_post_meta(get_the_ID(), 'enclosure', true), "\n");

The get_post_meta uses the ID of the current post to get the field that holds the URL of the media. It then returns a string. The strtok function removes remaining characters that might follow the URL and returns it cleanly.

Here is a second method you can try

1
2
3
4
5
$media = get_attached_media(null,1);
 
foreach($media as $m) {
  var_dump(wp_get_attachment_url($m->ID));
}