Calling Async Task

Go To StackoverFlow.com

7

I am streaming a radio Stream. I want to generate a notification when the song in the stream changes. I am using streamscraper (http://code.google.com/p/streamscraper) to get the metadata of the current stream, and I try to generate a notification when the metadata changes.

Here is the Async Task that I created to implement this.

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());
    }
}

The application is built on two linked projects. Project A is where the Application is initialised and Project B contains the collections and the playing logic. I want to use this task in Project B. Here is the play function (in project 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);
    }

When I try to execute it, the application crashes.

Here, is the complete error trace:

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)

I am confused in my understanding of Async Tasks. Is this not the correct way to use them?

EDIT: Answer added separately.

2012-04-04 21:43
by rahulthewall
Basically you're trying to show dialog after you're exit activity. There are various reasons for the above to happen, but I don't see anything about dialog in your code. Do you use dialog - Enrique Marcos 2012-04-04 21:59
No, I am trying to Log the new song after the current song has changed. No dialog or anything at this stage. I already have a onSongChanged listener set up. I can just call that when I am sure the checking for new songs bit is working - rahulthewall 2012-04-04 22:38
r u trying to show any dialog or progress dialog in your RadioPlayerActivity onclick method - Ads 2012-06-13 10:02
If the TimerTask solves your problem, please add the solution as an answer and accept it. So this question does not show up as unanswered anymore - Dirk Jäckel 2012-06-21 07:09
@DirkJäckel Done - rahulthewall 2012-06-22 07:50


2

Solved using TimerTask.

private void pollMetadata() {

    lastSong = radioSong;
    newSong = lastSong;
    Log.i(TAG, "Old Song: " + lastSong.getTitle());
    Log.i(TAG, "New Song: " + newSong.getTitle());

    updateMetadataTask = new TimerTask() {

        @Override
        public void run() {
            try {
                newSong = metadata.prepareRadioSong(streamURL);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.i(TAG, "Old Song: " + lastSong.getTitle());
            Log.i(TAG, "New Song: " + newSong.getTitle());

            if (newSong.getTitle().equals(lastSong.getTitle())) {
                Log.v(TAG, "SAME SONG");
            }
            else {
                Log.v(TAG, "SONG CHANGED");
                lastSong = newSong;

                currentPlaylistManager.clearPlaylist();
                currentPlaylistManager.appendSongAtEnd(newSong);
                listenerInformer.informCurrentSongChangeListener(newSong);

            }

        }
    };

    t.schedule(updateMetadataTask, 300, 30000);
}
2012-06-22 07:49
by rahulthewall
Ads