调用&LT时的mediaplay Android中MusicServices崩溃; MediaPlayer的> .pause() [英] MediaPlay in Android MusicServices crashes when calling <MediaPlayer>.pause()

查看:542
本文介绍了调用&LT时的mediaplay Android中MusicServices崩溃; MediaPlayer的> .pause()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序,我们有一个MusicService。目前,它在播放音乐和停止它方面完全正常,但就是无法启动和停止服务。我们呼吁在其他的方法,但 mp.pause()崩溃,除非它是由一个空检查包围。但是,当它检查空值不会在所有的工作。我们有所有这些早期的工作,但我们开始重新格式化的方式在做,因为对的Andr​​oid 4.0(ICS)音乐不停地随机播放,即使我们停止了,但是那反正这一点。

 公共类MusicService延伸服务{
    公共静态MediaPlayer的熔点;    @覆盖
    公众的IBinder onBind(最终意图为arg0){
        返回null;
    }    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        MP = MediaPlayer.create(这一点,R.raw.title_music);
        mp.setLooping(真);
        mp.setVolume(200,200);
    }    @覆盖
    公众诠释onStartCommand(最终意图的意图,最终诠释旗帜,最终诠释startId){
        mp.start();
        返回1;
    }    @覆盖
    公共无效调用onStart(最终意图的意图,最终诠释startId){    }    公众的IBinder onUnBind(最终意图为arg0){
        返回null;
    }    公共静态无效的onStop(){
        mp.stop();
        mp.release();
    }    公共静态无效的onPause(){
        如果(熔点!= NULL){
            mp.pause();
        }
    }    公共静态无效onResume(){
        如果(熔点!= NULL){
            mp.start();
        }
    }
    @覆盖
    公共无效的onDestroy(){
          mp.stop();
          mp.release();
          super.onDestroy();
    }    @覆盖
    公共无效onLowMemory(){
        mp.stop();
        mp.release();
    }}

我们正在使用此开始在另一个活动的服务:

  =意向意向书新(这一点,MusicService.class);
    startService(意向);


解决方案

如果没有记录我真的不能100%肯定地说,这是问题,但它会出现暂停方法被调用,而MP是在无效状态。我建议你​​改变你的onPause方法,以便它读取像这样。

 公共静态无效的onPause(){
    如果(熔点!= NULL){
        如果(mp.isPlaying())
            mp.pause();
    }
}

这将检查,看看它确实正在播放,并为它在任何工作状态下,除了错误状态。在根据文档最少的。

In our app we have a MusicService. Currently, it works perfectly fine in terms of playing music and stopping it, but that is from starting and stopping the service. We are calling it in other methods, but mp.pause() is crashing unless it is surrounded by a null checker. But when it checks for nulls it doesn't work at all. We had all this working earlier, but we started reformatting the way were doing it, because on Android 4.0 (ICS) the Music kept playing randomly even when we stopped it, but thats not the point anyway.

public class MusicService extends Service {
    public static MediaPlayer mp;

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

    @Override
    public void onCreate() {
        super.onCreate();
        mp = MediaPlayer.create(this, R.raw.title_music);
        mp.setLooping(true);
        mp.setVolume(200, 200);
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        mp.start();
        return 1;
    }

    @Override
    public void onStart(final Intent intent, final int startId) {

    }

    public IBinder onUnBind(final Intent arg0) {
        return null;
    }

    public static void onStop() {
        mp.stop();
        mp.release();
    }

    public static void onPause() {
        if (mp!=null) {
            mp.pause();
        }
    }

    public static void onResume() {
        if (mp!=null) {
            mp.start();
        }
    }


    @Override
    public void onDestroy() {
          mp.stop();
          mp.release();
          super.onDestroy();
    }

    @Override
    public void onLowMemory() {
        mp.stop();
        mp.release();
    }

}

We are using this to start the service in another activity:

intent = new Intent(this, MusicService.class);
    startService(intent);

解决方案

Without a log I can't really say with 100% certainty this is the issue, but it would appear that the pause method is being called while mp is in an invalid state. I suggest you change your onPause method so that it reads like so.

public static void onPause() {
    if (mp!=null) {
        if (mp.isPlaying())
            mp.pause();
    }
}

This will check to see if it is actually playing and will work for any state it is in, except an error state. At least according to the documentation.

这篇关于调用&LT时的mediaplay Android中MusicServices崩溃; MediaPlayer的> .pause()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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