我通过下载新文件来启动我的应用程序,如果有任何文件开始播放循环。然后,我每 x 秒启动一个任务来寻找新媒体。一旦调用任务,视频就会停止播放,并出现错误MediaPlayer finalized without being released。
如果有的话,它应该下载新媒体,然后更新播放列表。
从它的日志中我可以看到第一个播放被调用,然后错误出现然后。它只是一次又一次地调用任务。它就像它卡在那里?
我确定这是我在这里遗漏的一些愚蠢问题。我不确定为什么媒体播放器停止了?
主要活动
public class MainActivity extends Activity implements OnCompletionListener {
int playListIndex;
int currentMedia;
String mediaName;
ArrayList<Integer> playList = new ArrayList<Integer>();
File[] filelist;
private VideoView videoView;
private ImageView imageView;
Uri mediaPath;
private Handler mHandler = new Handler();
private SurfaceHolder holder;
// Get this shit from login!
public String channelid = "5e6299eb-asd6600d58fc9";
public String username = "tvasd";
public String linkid = "70casd6600d3f7d7";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// File mediadir = getDir("tvr", Context.MODE_PRIVATE);
// filelist = mediadir.listFiles();
try {
downloadMedia(this);
updateMediaTask(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Delete all files in folder
// if (mediadir.isDirectory()) {
// String[] children = mediadir.list();
// for (int i = 0; i < children.length; i++) {
// new File(mediadir, children[i]).delete();
// }
// }
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("media player", "play next please!");
if (mp != null) {
mp.release();
}
// play next video
currentMedia++;
if (currentMedia > playList.size() - 1) {
currentMedia = 0;
}
Log.d("MEDIA_COUNTER", String.format("%d", currentMedia));
try {
playMedia(currentMedia);
} catch (IOException e) {
e.printStackTrace();
}
}
private void playMedia(int playListIndex) throws IOException {
Log.i("media player", "play media!");
String path = filelist[playListIndex].getAbsolutePath();
FileInputStream fileInputStream = new FileInputStream(path);
final Uri uri = Uri.parse(path);
String filename = filelist[playListIndex].getName();
Log.i("FILENAME", path);
if (filename.contains("image")) {
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setVisibility(View.VISIBLE);
imageView.setImageURI(uri);
mHandler.postDelayed(new Runnable() {
public void run() {
imageView.setVisibility(View.GONE);
imageView.setImageURI(uri);
onCompletion(null);
}
}, 4000);
} else if (filename.contains("video")) {
Log.d("PLAY",String.format("%s", fileInputStream.getFD()));
Log.i("media player", "play video!");
SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("SURFACE", "Created");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
});
MediaPlayer pl = new MediaPlayer();
pl.setOnCompletionListener(this);
pl.setDisplay(holder);
pl.setDataSource(fileInputStream.getFD());
pl.prepare();
pl.start();
}
}
void createPlayList(Context context) {
ContextWrapper cw = new ContextWrapper(context);
File mediadir = cw.getDir("tvr", Context.MODE_PRIVATE);
filelist = mediadir.listFiles();
if (filelist != null)
{
for ( int i = 0;i<filelist.length;i++)
{
Log.i("FOKKER", filelist[i].getName());
Log.i("FOKKER", filelist[i].getAbsolutePath());
}
}
currentMedia = 0;
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
try {
playMedia(currentMedia);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void updateMediaTask(final MainActivity a) throws IOException, JSONException {
int delay = 1000; // delay for 1 sec.
int period = 3000; // repeat every 10 minutes. 60000
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
new updateFiles(a).execute(getBaseContext(),"update");
}
}, delay, period);
}
private void downloadMedia(Context context) throws IOException, JSONException {
new updateFiles(this).execute(context,"new");
}
}
这是updateFiles类:
public class updateFiles extends AsyncTask<Object, Integer, Long> {
public MainActivity activity;
public updateFiles(MainActivity a) { activity = a;}
@Override
protected Long doInBackground(Object... params) {
Context context = (Context) params[0];
String method = (String) params[1];
File mediadir = context.getDir("tvr", Context.MODE_PRIVATE);
JSONArray channels = Json.getJson("http://192.168.2.136:8080/rest/channel/"+ linkid +"/"+ username, "GET");
try {
for (int i=0; i < channels.length(); i++) {
JSONObject channel_data = channels.getJSONObject(i);
String channelID = channel_data.getString("ChannelID").toLowerCase();
JSONArray json = Json.getJson("http://192.168.2.136:8080/rest/program/"+ linkid +"/"+ username +"/" + channelID, "GET");
try {
for (int j=0; j < json.length(); j++) {
JSONObject json_data = json.getJSONObject(j);
String name = json_data.getString("Name").toLowerCase();
name = name.replace("-", "_");
if (name.contains("mp4") || name.contains("png") || name.contains("jpg") || name.contains("jpeg")) {
if (name.contains("mp4")) {
name = "/video_"+name;
} else if (name.contains("png") || name.contains("jpg") || name.contains("jpeg")) {
name = "/image_"+name;
}
String _name = name.replace("/", "");
File file = new File(mediadir, _name);
if(file.exists()) {
Log.i("FILE FOUND", _name);
} else {
new Download().execute(context, name, "http://192.168.2.136:8080/rest/transfer/"+ linkid +"/"+ username +"/" + json_data.getString("ID"));
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
Log.i("UPDATEFILES done", method);
if ( method == "new" ) {
Log.i("XXX", "This is a new method, so play now");
activity.createPlayList(context);
} else {
Log.i("XXX", "This is a update method, so just update, dont play again. Go on playing");
// activity.filelist = mediadir.listFiles();
}
return null;
}
}
日志:_
02-21 16:16:13.429: I/FILE FOUND(24010): video_tvr_webtrailer2.mp4
02-21 16:16:13.429: I/FILE FOUND(24010): video_tvr_consumerintro.mp4
02-21 16:16:13.429: I/JSON(24010): http://192.168.2.136:8080/rest/program/70c8223f-e054-4f33asd3f7d7/asdco/6cfbasdbe-a16600d5ac24
02-21 16:16:13.476: D/JSON result(24010): [{"ChannelID":"6casdd2-47f3-95be-a16600d5ac24","ChannelPath":"\\asda","DateChanged":"2\/15\/2013 10:59:20 AM","DateCreated":"2\/15\/2013 12:58:59 PM","ID":"5e25edasd-b82b-a16600d5f4ef","LastWriteUTC":"\/Date(1352527205000+0200)\/","Name":"Video_05-safety-ccw_mynhardt.wmv","Size":125727228}]
02-21 16:16:13.476: I/JSON(24010): **http://192.asd0/rest/program/7asd4-4f33-9b6b-a1660asd89-be1a-a16600e0bcee**
02-21 16:16:13.523: **D/JSON result(24010): []**
02-21 16:16:13.523: **I/UPDATEFILES done(24010): update**
02-21 16:16:13.523: I/XXX(24010): **This is a update method, so just update, dont play again. Go on playing**