实时流媒体播放功能

时间:2016-07-13 06:31:02

标签: android playback dvr

我需要开发具有回放功能的实时DVR,并带有实时无线电流。 就像一些应用程序正在做的那样:V Radio Recorder。 主要的是我需要播放功能和直播。 就像用户收听20秒的无线电一样,他可以回放到10秒。

2 个答案:

答案 0 :(得分:0)

首先下载实时流字节字节并在本地缓存它。然后从该缓存播放无线电并进行录制将该文件保存到其他录制目录。 这是代码。在这里我使用AudioTrack但您可以使用媒体播放器并将源设置为缓存文件以进行播放的媒体播放器。

public class MainActivity extends AppCompatActivity {
    private Decoder mDecoder;
    private AudioTrack mAudioTrack;
    File downloadingMediaFile;
    FileOutputStream out;
    Button stop, start;
    boolean isStop;
    Thread DownloadThread;
    String url = "http://stream.laut.fm/darksynthradio?type=.mp3/;stream.mp3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.continer, AudioVisualizationFragment.newInstance())
                .addToBackStack(null)
                .commit();

        stop = (Button) findViewById(R.id.btn_stop);
        start = (Button) findViewById(R.id.btn_start);
        isStop = true;
        stop.setClickable(false);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isStop = true;
                start.setClickable(true);
                stop.setClickable(false);

                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this, "Download Stop", Toast.LENGTH_SHORT).show();


            }
        });
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                downloadingMediaFile = new File("/storage/sdcard0/", new Date() + ".mp3");
                try {
                    out = new FileOutputStream(downloadingMediaFile, true);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                isStop = false;
                stop.setClickable(true);
                start.setClickable(false);
                Download();
                DownloadThread.start();
                Toast.makeText(MainActivity.this, "Download Start", Toast.LENGTH_SHORT).show();

            }
        });

        final int sampleRate = 44100;
        final int minBufferSize = AudioTrack.getMinBufferSize(sampleRate,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT);

        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT,
                minBufferSize,
                AudioTrack.MODE_STREAM);

        mDecoder = new Decoder();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream in = new URL(url)
                            .openConnection()
                            .getInputStream();
                    final Bitstream bitstream = new Bitstream(in);

                    final int READ_THRESHOLD = 2147483647;
                    int framesReaded = READ_THRESHOLD;

                    Header header;
                    for (; framesReaded-- > 0 && (header = bitstream.readFrame()) != null; ) {
                        SampleBuffer sampleBuffer = (SampleBuffer) mDecoder.decodeFrame(header, bitstream);
                        short[] buffer = sampleBuffer.getBuffer();
                        mAudioTrack.write(buffer, 0, buffer.length);
                        bitstream.closeFrame();

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

        mAudioTrack.play();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAudioTrack.stop();
    }

    void Download() {
        DownloadThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream in = new URL(url)
                            .openConnection()
                            .getInputStream();
                    byte buf[] = new byte[16384];
                    do {
                        int numread = 0;
                        numread = in.read(buf);
                        if (numread > 0) {
                            out.write(buf, 0, numread);
                        }
                    } while (!isStop);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

答案 1 :(得分:0)

我找到了wowza服务器来解决我的问题。

在wowza服务器上创建帐户。下载服务器文件。在本地计算机上运行服务器。将您的实时网址放入wowza服务器并选择播放时间。我使用MX播放器播放具有播放功能的流媒体。

这是website Link