Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream only audio data from youtube video on my application?

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.

like image 221
Aishwarya Shiva Avatar asked Feb 12 '12 07:02

Aishwarya Shiva


2 Answers

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.

like image 112
Multimedia Mike Avatar answered Oct 07 '22 17:10

Multimedia Mike


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();
like image 22
Albert Chen Avatar answered Oct 07 '22 17:10

Albert Chen