我正在播放广播流。我想在流中的歌曲更改时生成通知。我正在使用streamscraper (http://code.google.com/p/streamscraper) 来获取当前流的元数据,并尝试在元数据更改时生成通知。
这是我为实现它而创建的异步任务。
public class updateMetadata extends
AsyncTask<String, Void, PlaylistSong<BaseArtist, BaseAlbum>> {
private static final String TAG = updateMetadata.class.getSimpleName();
private PlaylistSong<BaseArtist, BaseAlbum> oldSong = null;
private PlaylistSong<BaseArtist, BaseAlbum> newSong = null;
private metadataHarvester fetchMetadata = new metadataHarvester();
private String streamUrl = null;
@Override
protected PlaylistSong<BaseArtist, BaseAlbum> doInBackground(String... urls) {
for (String url : urls) {
try {
oldSong = fetchMetadata.prepareRadioSong(url);
} catch (Exception e) {
e.printStackTrace();
}
streamUrl = url;
}
newSong = oldSong;
while (newSong == oldSong) {
try {
newSong = fetchMetadata.prepareRadioSong(streamUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
return newSong;
}
@Override
protected void onPostExecute(PlaylistSong<BaseArtist, BaseAlbum> song) {
Log.v(TAG, "New Song: " + song.getTitle());
}
}
该应用程序建立在两个链接的项目之上。项目 A 是初始化应用程序的地方,项目 B 包含集合和播放逻辑。我想在项目 B 中使用这个任务。这是播放功能(在项目 B 中)。
protected synchronized void play(final IMediaPlayerWrapper mp) {
streamURL = streamFetcher.getStreamUrl();
Log.i(TAG, "Stream URL: " + streamURL);
try {
Log.i(TAG, "Testing metadata harvester");
radioSong = metadata.prepareRadioSong(streamURL);
} catch (Exception e) {
Log.w(TAG, e);
}
updateMetadata(radioSong);
currentPlaylistManager.clearPlaylist();
currentPlaylistManager.appendSongAtEnd(radioSong);
listenerInformer.informCurrentSongChangeListener(radioSong);
try {
mp.setSong(radioSong, streamURL);
mp.play();
if (mp.isPlaying()) {
setPlayerState(PlayerState.PLAY);
}
} catch (Exception e) {
Log.w(TAG, e);
setPlayerState(PlayerState.ERROR);
}
updateMetadata metadataChecker = new updateMetadata();
metadataChecker.execute(streamURL);
}
当我尝试执行它时,应用程序崩溃了。
这是完整的错误跟踪:
04-04 23:34:34.975: E/WindowManager(18080): Activity ch.ethz.dcg.pancho2.view.radioplayer.RadioPlayerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e8f0 that was originally added here
04-04 23:34:34.975: E/WindowManager(18080): android.view.WindowLeaked: Activity ch.ethz.dcg.pancho2.view.radioplayer.RadioPlayerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e8f0 that was originally added here
04-04 23:34:34.975: E/WindowManager(18080): at android.view.ViewRoot.<init>(ViewRoot.java:258)
04-04 23:34:34.975: E/WindowManager(18080): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-04 23:34:34.975: E/WindowManager(18080): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-04 23:34:34.975: E/WindowManager(18080): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-04 23:34:34.975: E/WindowManager(18080): at android.app.Dialog.show(Dialog.java:241)
04-04 23:34:34.975: E/WindowManager(18080): at android.app.ProgressDialog.show(ProgressDialog.java:107)
04-04 23:34:34.975: E/WindowManager(18080): at android.app.ProgressDialog.show(ProgressDialog.java:90)
04-04 23:34:34.975: E/WindowManager(18080): at ch.ethz.dcg.pancho2.view.radioplayer.RadioPlayerActivity$5.onClick(RadioPlayerActivity.java:276)
04-04 23:34:34.975: E/WindowManager(18080): at android.view.View.performClick(View.java:2485)
04-04 23:34:34.975: E/WindowManager(18080): at android.view.View$PerformClick.run(View.java:9080)
04-04 23:34:34.975: E/WindowManager(18080): at android.os.Handler.handleCallback(Handler.java:587)
04-04 23:34:34.975: E/WindowManager(18080): at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 23:34:34.975: E/WindowManager(18080): at android.os.Looper.loop(Looper.java:130)
04-04 23:34:34.975: E/WindowManager(18080): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 23:34:34.975: E/WindowManager(18080): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:34:34.975: E/WindowManager(18080): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 23:34:34.975: E/WindowManager(18080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 23:34:34.975: E/WindowManager(18080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 23:34:34.975: E/WindowManager(18080): at dalvik.system.NativeStart.main(Native Method)
我对异步任务的理解感到困惑。这不是使用它们的正确方法吗?
编辑:答案单独添加。