Запись аудиопотока из онлайн
На гитхабе нашла проект, где вроде реализована запись аудиопотока из интернета. Но не могу понять где происходит запись в файл на устройстве.
MainActivity
public class MainActivity extends AppCompatActivity {
Recorder recorder;
String streamURL = "http://159.253.37.137:9914/";
String recordedFileName = "yayin.mp3";
@BindView(R.id.editTextStreamURL)
EditText editTextStreamURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
deleteRecordingFile();
}
@OnClick(R.id.buttonPlay)
public void onClickPlay(View view) {
reCreateRecorder();
deleteRecordingFile();
Toast.makeText(this, recorder.getUrlPath(), Toast.LENGTH_SHORT).show();
recorder.record();
}
@OnClick(R.id.buttonPause)
public void onClickPause(View view) {
recorder.stopRecording();
reCreateRecorder();
}
@OnClick(R.id.buttonPlayFromRecord)
public void onClickPlayFromRecord(View view) {
recorder.playFromRecording();
}
@OnClick(R.id.buttonStopFromRecord)
public void onClickStopFromRecord(View view) {
recorder.stopPlayingFromRecord();
}
public void deleteRecordingFile() {
File file = new File(getCacheDir(), "yayin.mp3");
if (file.exists()) {
file.delete();
}
}
public void reCreateRecorder() {
if (recorder != null) {
if (recorder.isRecording()) recorder.stopRecording();
recorder.player = null;
recorder = null;
}
if (!editTextStreamURL.getText().toString().equals("")){
streamURL = editTextStreamURL.getText().toString();
}
else
{
streamURL = "http://159.253.37.137:9914/";
}
recorder = new Recorder(this, streamURL, recordedFileName);
}
}
Recorder
public class Recorder extends AsyncTask {
private Context context;
private String urlPath;
private String recordedFileName;
private boolean isRecording = false;
private MediaPlayer mediaPlayer;
Player player;
public Recorder() {
}
public Recorder(Context context, String url, String recordedFilePath) {
this.context = context;
this.urlPath = url;
this.recordedFileName = recordedFilePath;
this.mediaPlayer = new MediaPlayer();
player = new Player(context,recordedFileName,urlPath);
}
@Override
protected Object doInBackground(Object[] objects) {
isRecording = true;
try {
URL url = new URL(urlPath);
InputStream inputStream = url.openStream();
File file = new File(context.getCacheDir(), recordedFileName);
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4*1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
if(isCancelled())
break;
outputStream.write(buffer,0,read);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void record()
{
File file = new File(context.getCacheDir(),recordedFileName);
if (file.exists())
{
file.delete();
}
player.play();
this.execute();
}
public void stopRecording()
{
isRecording=false;
this.cancel(true);
player.stop();
}
public void playFromRecording()
{
try {
File file = new File(context.getCacheDir(), recordedFileName);
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopPlayingFromRecord()
{
mediaPlayer.stop();
mediaPlayer.reset();
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public String getUrlPath() {
return urlPath;
}
public void setUrlPath(String urlPath) {
this.urlPath = urlPath;
}
public String getRecordedFileName() {
return recordedFileName;
}
public void setRecordedFileName(String recordedFileName) {
this.recordedFileName = recordedFileName;
}
public boolean isRecording() {
return isRecording;
}
public void setRecording(boolean recording) {
this.isRecording = recording;
}
public MediaPlayer getMediaPlayer() {
return mediaPlayer;
}
public void setMediaPlayer(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
}
Player
public class Player {
// 1. Create a default TrackSelector
Handler mainHandler;
BandwidthMeter bandwidthMeter;
TrackSelection.Factory audioTrackSelectionFactory;
DefaultTrackSelector trackSelector;
// 2. Create the player
SimpleExoPlayer player;
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter defaultBandwidthMeter;
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory;
// This is the MediaSource representing the media to be played.
MediaSource audioSource;
Context context;
String recordedFileName;
String urlPath;
public Player() {
}
public Player(Context context, String recordedFileName, String urlPath) {
this.context = context;
this.recordedFileName = recordedFileName;
this.urlPath = urlPath;
// 1. Create a default TrackSelector
mainHandler = new Handler();
bandwidthMeter = new DefaultBandwidthMeter();
audioTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(audioTrackSelectionFactory);
// 2. Create the player
player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
// Measures bandwidth during playback. Can be null if not required.
bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "yourApplicationName"), defaultBandwidthMeter);
// This is the MediaSource representing the media to be played.
audioSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(urlPath));
}
public void play() {
// Prepare the player with the source.
player.prepare(audioSource);
player.setPlayWhenReady(true);
}
public void stop() {
player.stop();
player.release();
}
}
Как вообще реализовать запись в файл?