안드로이드 개발 질문/답변
(글 수 45,052)
package my.streammedia;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.webkit.URLUtil; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.widget.VideoView;
public class streammedia extends Activity {
private static final String TAG = "VideoViewDemo"; private VideoView mVideoView; private EditText mPath; private Button mPlay; private Button mPause; private Button mReset; private Button mStop; private String current;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay = (Button) findViewById(R.id.play);
mPause = (Button) findViewById(R.id.pause);
mReset = (Button) findViewById(R.id.reset);
mStop = (Button) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable(){
public void run() {
playVideo();
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(streammedia.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
이렇게 구현을 해봤는데 로컬 영상파일은 재생이 가능한데 URL은 플레이가 안되네요
혹시 에뮬레이터에서는 streaming play가 불가능한가요? 폰에서 구동을 해봐야 되나요?




저도같은 소스를 구해서 보고 하고있는데 에뮬에서 잘 불러와 집니다.
그런데 파일 다운시 저는 getDataSource(String path) 메소드 내에서
File temp = File.createTempFile("mediaplayertmp","dat"); 저부분에서 자꾸 이셉션 떨어져서 헤맸네요메니페프트 파일에 sdk버전을 기입하지 않으니 파일은 다운 되는데
실행 할때 마다 워닝이 뜨네요 어찌 잡아야 할지 ㅠㅜ