13

I want to develop an application that will display a list of videos from YouTube and when the user clicks a Youtube video, my application will start playing its audio. I came across a solution to download the YouTube video first and extract audio, but to speed up the process I want to directly transfer the audio bits to my application's audio player instead of video. In other words, I want to stream YouTube audio instead of video in my application.

1
  • 1
    I do not believe YouTube offers an audio-only download/stream. If you have control over how the video is being played then you can simply hide (and possibly save CPU power by not decoding) the video, but I suspect that's the best you can do. Also, what platform/language is this on? It may matter.
    – perelman
    Feb 12, 2012 at 8:12

2 Answers 2

6

It sounds like you are already most of the way there. Your application has a way to list the videos and download selected ones. You just need an infrastructure for playing the audio directly from the video files.

YouTube videos can either be FLV or MP4 files. Inside these files can be MP3 or AAC audio (some other audio codecs are possible, but you won't encounter those on YouTube). This means your app needs to know how to take apart FLV and MP4 files directly, and how to decode MP3 and AAC audio directly to PCM. There are libraries to help with these tasks, depending on your language and platform.

3

please try extract the audio from video stream using the following code:

// the video URL is the following
    String url = "http://137.110.92.231/~chenyu/BBC.mp4"; 

    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
1
  • 1
    From where the MediaPlayer class is? Any external library? Please, add more explanation. Mar 2, 2021 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.