设置AAC +播放器应用程序在后台服务 [英] Set Aac+player app as service in background

查看:160
本文介绍了设置AAC +播放器应用程序在后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好即时通讯使用的http:// code .google.com / p / aacde codeR-的Andr​​oid / 继承人我的主要活动全code: http://pastebin.com/FmaVgfNP 但现在我希望把他们的应用程序在后台所以当人们preSS后退按钮的应用程序保持流式音频我尝试使用这样的:基本的应用程序有两个活动1列表视图然后当我在一个项目点击自动播放该站流,但是当我preSS后退按钮我流站,我试图创建一个像这样的服务:

Hi there im using http://code.google.com/p/aacdecoder-android/ heres my main activity full code: http://pastebin.com/FmaVgfNP but now i want to put that app in the background so when people press back button the app stays streaming audio i tried using this: basically the app have two activities one listview then when i click in one item it autoplays the stream for that station but when i press back button my streams stops, i tried to create a service like this:

public class MyService extends Service {
    private static final String TAG = "AACPlayerActivity";


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

        //player = MediaPlayer.create(this, R.raw.braincandy);
        //player.setLooping(false); // Set looping
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        //player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        //player.start();
    }
}

我想这code配置在我的活动服务:

i tried this code to configure service in my activity:

startService(new Intent(this, MyService.class));

在我的主要活动时aacplayer启动

in my main activity when aacplayer starts

然后当停止我把这个:

stopService(new Intent(this, MyService.class));

我试图把在MyService.class的OnCreate函数 multiPlayer.playAsync(的getURL()); 这样的音频流保持流,当人们preSS后退按钮

i tried to put in MyService.class oncreate the function multiPlayer.playAsync( getUrl()); so the audio stream keeps streaming when people press back button.

但即时得到这个错误:

10-16 02:02:07.760: E/AndroidRuntime(25638): java.lang.RuntimeException: Unable to start service com.spoledge.aacplayer.MyService@41b40ca0 with Intent { cmp=com.spoledge.aacplayer/.MyService }: java.lang.NullPointerException

我不知道如果IM与逻辑IM何时我的播放器流音频所以它一直在后台我可以注册行动作为服务不是很明确的权利这样做。

i dont know if im doing right with the logic im not very clear about when my player streams the audio i can register that action as a service so it keeps in background.

非常感谢。

推荐答案

香港专业教育学院使用绑定服务之前做到了这一点。界是更好,因为它可以让你轻松实现暂停/停止/播放等。如果您的活动是由系统丧生,而在后台,它可以重新绑定到该服务时用户将它备份,从而重新启用控制播放。

Ive done this before using a bound service. Bound is better as it allows you to easily implement pause/stop/play, etc. If your activity is killed by the system while in the background, it can re-bind to the service when the user brings it back up, which re-enabled control of playback.

我不知道,如果它是不好的做法,发布链接到自己的博客,但完整的源代码code为这里的http://www.speaking$c$c.com/2012/02/22/creating-a-streaming-audio-app-for-android-with-android-media-mediaplayer-android-media-audiomanager-and-android-app-service/

I'm not sure if it is bad practice to post links to my own blog but the full source code is here at http://www.speakingcode.com/2012/02/22/creating-a-streaming-audio-app-for-android-with-android-media-mediaplayer-android-media-audiomanager-and-android-app-service/

如果链接不是preferred我会在这里复制的解决方案,但它是漫长的:-)好运和快乐编码!

If linking is not preferred I'll duplicate the solution here, but it is lengthy :-) good luck and happy coding!

如果你想使用一个启动的服务像宥正在做的,并没收绑定服务的好处,那么你需要你的MediaPlayer对象完全移动到服务进出活动。您可以实例在你的onCreate在MediaPlayer(),但它会更好的onStartCommand()方法来启动它(调用playAsync())。

If you want to use a started service like yuo are doing, and forfeit the benefits of a bound service, then you need to move your MediaPlayer object entirely into the service and out of the activity. you can instantiate the MediaPlayer in your onCreate() but it will be better to start it (call playAsync()) in the onStartCommand() method.

这篇关于设置AAC +播放器应用程序在后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆