mainplayer.java
package com.RGSoft; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView; public class mainplayer extends Activity private void initControls() private void startStreamingAudio() { try { final ProgressBar progressBar = (ProgressBar) |
streamingMediaPlayer.java
package com.RGSoft; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Handler; import android.util.Log; import android.widget.Button; import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView;
// Track for display by progressBar
private long mediaLengthInKb, mediaLengthInSeconds;
private int totalKbRead = 0;
// Create Handler to call View updates on the main UI thread. private final Handler handler = new Handler(); private MediaPlayer mediaPlayer; private File downloadingMediaFile; private boolean isInterrupted; private Context context; private int counter = 0; /** * Progressivly download the media to a temporary location and update {
this.mediaLengthInKb = mediaLengthInKb;
this.mediaLengthInSeconds = mediaLengthInSeconds;
// 쓰레드 시작 Runnable r = new Runnable() { public void run() { try { downloadAudioIncrement(mediaUrl); } catch (IOException e) { Log.e(getClass().getName(), new Thread(r).start();
}
/** * Download the url stream to a temporary location and then call // 파일 URL 주소로 부터 연결 URLConnection cn = new URL(mediaUrl).openConnection(); cn.connect(); InputStream stream = cn.getInputStream(); if (stream == null) { Log.e(getClass().getName(), // 캐시 폴더 만들고 .dat파일 생성 downloadingMediaFile = new File(context.getCacheDir(), // 다시 파일 생성
FileOutputStream out = new FileOutputStream(downloadingMediaFile);
byte buf[] = new byte[16384];
int totalBytesRead = 0, incrementalBytesRead = 0;
// 캐시 영역에 파일 저장
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
totalBytesRead += numread;
incrementalBytesRead += numread;
totalKbRead = totalBytesRead/1000;
testMediaBuffer();
fireDataLoadUpdate();
} while (validateNotInterrupted());
// 파일 전송이 끝나면 종료
stream.close();
if (validateNotInterrupted()) {
fireDataFullyLoaded();
}
} private boolean validateNotInterrupted() // mediaPlayer.release();
}
return false;
} else {
return true;
}
}
/** * Test whether we need to transfer buffered data to the MediaPlayer. * Interacting with MediaPlayer on non-main UI thread can causes crashes else if ( mediaPlayer.getDuration() - mediaPlayer private void startMediaPlayer() mediaPlayer = createMediaPlayer(bufferedFile); // 음악 파일 생성 후 재생 // We have pre-loaded enough content and started the MediaPlayer private MediaPlayer createMediaPlayer(File mediaFile) throws IOException // It appears that for security/permission reasons, /** * Transfer buffered data to the MediaPlayer. * NOTE: Interacting with a MediaPlayer on a non-main UI thread can cause // Copy the currently downloaded content to a new buffered File. File bufferedFile = new File(context.getCacheDir(), // Pause the current player now as we are about to create and // Create a new MediaPlayer rather than try to re-prepare // Restart if at end of prior buffered content or mediaPlayer was if (wasPlaying || atEndOfFile){ mediaPlayer.start(); } private void fireDataLoadUpdate() private void fireDataFullyLoaded() handler.postDelayed(notification,1000);
}
}
public void interrupt() /** * Move the file in oldLocation to newLocation. */ public void moveFile(File oldLocation, File newLocation) throws IOException } |
player.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px">
<TextView android:id="@+id/text_kb_streamed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Streaming .mp3 Audio Tutorial"/>
<Button android:id="@+id/button_stream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
style="?android:attr/buttonStyleSmall"
android:text="Start Streaming"/>
<ProgressBar android:id="@+id/progress_bar"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
style="?android:attr/progressBarStyleHorizontal"/>
<ImageButton android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
style="?android:attr/buttonStyleSmall"
android:src="@drawable/button_pause"/>
</LinearLayout>
'IT_Programming > Android_Java' 카테고리의 다른 글
Android NFC (0) | 2013.08.05 |
---|---|
안드로이드 바코드 인식기(OCR) 오픈 소스 zxing 분석 (0) | 2013.08.05 |
[android] Volley - Network & ImageLoading 라이브러리 ( google I/O 2013 ) (0) | 2013.08.03 |
Volley 잘 쓰기 (0) | 2013.08.03 |
Google Map Android V2 - 요약정리 (0) | 2013.07.30 |