想知道是否有人可以帮助我弄清楚如何在 Android Mediaplayer 中播放本地音频/视频文件...我可以使用保存 URL 地址的变量从 URL 流式传输音频/视频,但是如何处理本地文件?如何使用localFile变量调用本地Files进行播放?感谢大家!
package com.ryanleckey2;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class Audio_Video_Stream extends BT_activity_base{
private String localFile = "";
private String localFileName = "";
private String dataURL = "";
private String currentURL = "";
public String thisActivityName = "Screen_Video";
//movie controller
String videoUrl;
VideoView videoView;
MediaController mediaController;
public int isLoading;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_video);
//fill JSON properties...
dataURL = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "dataURL", "");
currentURL = dataURL;
localFileName = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "localFileName", "");
//setup the saveAsFileName
if(localFileName.length() > 1){
//use the file name in the JSON data...
localFile = localFileName;
//Log.i("ZZ", thisActivityName + ":videoUrl : " + videoUrl);
videoView = (VideoView) findViewById(R.id.videoView);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
isLoading = 0;
}
}
///////////////////////////////////////////////////
//activity life-cycle overrides
//onStart
@Override
protected void onStart(){
super.onStart();
//Log.i("ZZ", thisActivityName + ":onStart");
}
//onResume
@Override
public void onResume() {
super.onResume();
if(isLoading == 1){
if(videoView != null){
videoView = null;
}
}
startMovie();
//Log.i("ZZ", thisActivityName + ":onResume");
}
//onPause
@Override
public void onPause() {
//Log.i("ZZ", thisActivityName + ":onPause");
super.onPause();
if(videoView != null){
videoView = null;
}
}
//onStop
@Override
protected void onStop(){
super.onStop();
//Log.i("ZZ", thisActivityName + ":onStop");
}
//onDestroy
@Override
public void onDestroy() {
//Log.i("ZZ", thisActivityName + ":onDestroy");
super.onDestroy();
}
//activity life-cycle overrides
///////////////////////////////////////////////////
//starts movie
public void startMovie(){
//show progress for a few seconds..
showProgress("Starting media stream...", "Please be patient. If you're on 3G this could take a bit.\nWi-Fi is much better for streaming.");
isLoading = 1;
try{
//set audio/video link (mp4,mp3 format )
mediaController = new MediaController(this);
Uri uri = Uri.parse(currentURL);
//Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(mOnPreparedListener);
videoView.start();
}catch(Exception je){
hideProgress();
showAlert("Invalid URL","The URL to the media could not be determined. This screen will close.");
finish();
}
}
//onPrepared...
private MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener(){
public void onPrepared(MediaPlayer mp){
//Log.i("ZZ", thisActivityName + ":OnPreparedListener");
isLoading = 0;
// mp.setLooping(true);
hideProgress();
}
};
}