VideoView를 사용하여 assets 디렉토리에 위치한 동영상을 실행하는 코드를 작성하였는데 계속 오류가 발생 했다.
프로젝트의 /assets/a.mp4에 파일을 저장했다.
Url fileuri= Uri.parse("file:///android_asset/a.mp4");
이 렣게 uri를 생성해서 실행했는데 오류가 발생 한다. 이유를 모르겠다. 구글검색으로 자료를 찾아봐도 해결책을 찾을 수 없었다. 그래서 파일의 위치를 res/raw/a.mp4 로 옮기었다.
그리고 다음과 같이 uri를 생성했다.
Uri videofile = Uri.parse("android.resource://"+getPackageName()+"/raw/a");

이렇게 했더니 정상적으로 동영상이 표시되었다.
아래는 전체 소스코드이다. 물론 layout/main.xml에 VideoView객체를 video_view라는 id로 추가했다.
<?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"
>
<VideoView android:id="@+id/video_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></VideoView>
</LinearLayout>
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.VideoView;
public class VideoPlayer extends Activity {
VideoView videoview ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
videoview = (VideoView) findViewById(R.id.video_view);
try {
Uri videofile = Uri.parse("android.resource://"+getPackageName()+"/raw/a");
videoview.setVideoURI(videofile);
//descriptor.
videoview.start();
videoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), "onCompletion()");
}
});
} catch (Exception ex) {
Log.d(getClass().getName(), "Video failed: '" + ex + "'" );
ex.printStackTrace();
}
}
}
2011/12/06 11:28 2011/12/06 11:28

Trackback Address :: https://youngsam.net/trackback/1685