流式传输来自远程服务的音频并在扬声器中播放

时间:2019-07-09 07:20:03

标签: java android audio-streaming speaker headphones

我有一个在RTP Stream class的帮助下运行的流服务。我可以从远程IP流音频。我已使用wireshark检查连接,并且它正在接收来自服务器的数据包。现在,我想在Android扬声器或耳机中播放接收到的音频。我尝试使用Mediaplayer class,但似乎缺少某些内容。我该如何继续前进?

public class audioService extends Service {

AudioStream audioStream;
Handler handler;
Runnable runnable;
Context context;

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate(){
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    new Thread(new audioRunnable()).start();
    return super.onStartCommand(intent, flags, startId);
}

private class audioRunnable implements Runnable {
    @Override
        public void run() {

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
            StrictMode.setThreadPolicy(policy);
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioManager.setSpeakerphoneOn(true);
            AudioGroup audioGroup = new AudioGroup();
            audioGroup.setMode(AudioGroup.MODE_ECHO_SUPPRESSION);
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(audioManager.STREAM_RING);
            InetAddress inetAddress;

            try {

                inetAddress = InetAddress.getByName("10.0.0.0");
                audioStream = new AudioStream(inetAddress);
                audioStream.setCodec(AudioCodec.PCMU);
                audioStream.setMode(RtpStream.MODE_NORMAL);
                InetAddress inetAddressRemote = InetAddress.getByName("192.00.00.00");
                audioStream.associate(inetAddressRemote, 8088);
                audioStream.join(audioGroup);

                // start the media player
                mediaPlayer.setDataSource(context, Uri.parse("10.0.0.0"));
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

}

注意:我拥有清单文件中的权限 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

0 个答案:

没有答案