gpt4 book ai didi

android - Mediaplayer 被 AsyncTask 停止 - MediaPlayer 在没有被释放的情况下完成

转载 作者:行者123 更新时间:2023-11-29 21:48:49 27 4
gpt4 key购买 nike

我通过下载新文件来启动我的应用程序,如果有任何文件开始播放循环。然后我每 x 秒触发一个任务来寻找新媒体。调用任务后,视频将停止播放,并显示错误 MediaPlayer finalized without being releases

它应该下载新媒体(如果有的话),然后更新播放列表。

从它的日志记录中我可以看到第一个播放被调用,然后错误出现,然后。它只是一次又一次地调用任务。好像卡在里面了?

我确定这是我在这里遗漏的一些愚蠢的问题。我不确定媒体播放器停止的原因?

主 Activity

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**

最佳答案

很久以前就有人问过这个问题。但问题的解决方案不是异步任务,而是在类中创建一个 MediaPlayer 字段,而不是将其作为任何方法的局部变量。

目前在您的情况下,它是 private void playMedia() 方法的一部分。由于这是在一个方法中,一旦方法完成,它就有资格进行垃圾收集。做这样的事情:-

public class MainActivity extends Activity implements OnCompletionListener {

private MediaPlayer pl; // This makes sure mediaplayer is there if activity is alive

关于android - Mediaplayer 被 AsyncTask 停止 - MediaPlayer 在没有被释放的情况下完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15004428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com