2013-05-01 195 views
0

我正在为我的应用程序中的音频模块工作,所以我创建了简单的音频演示与服务。媒体播放器中停止服务后音频不播放

它的工作可达Play - Pause - Playing in Background但问题很奇怪。

简单而推出的应用程序,我已经添加了两个按钮像

  • Play/Pause(会改变按标题状态)

  • stop(停止服务)

现在我说我的play-pause & Stop工作正常,但当我尝试播放音频后点击stop它的原因在Start

Error : 05-01 13:37:42.671: E/start(8096): java.lang.IllegalStateException

问题Audio_Activity.java

public class Audio_Activity extends Activity { 

    private static final String TAG = "ServicesDemo"; 
    public static Button play_pause; 
    public static Button buttonStop; 
    Audio_Service ms; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ms = new Audio_Service(); 
     play_pause = (Button) findViewById(R.id.play_pause); 
     buttonStop = (Button) findViewById(R.id.buttonStop); 

     play_pause.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if (isMyServiceRunning()) { 
         ms.play_pause(); 
        } else { 
         startService(new Intent(Audio_Activity.this, 
           Audio_Service.class)); 
         play_pause.setText("Pause"); 
        } 
       } 
      }); 

     buttonStop.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if (isMyServiceRunning()) { 
         ms.stop_audio(); 
         stopService(new Intent(Audio_Activity.this, 
           Audio_Service.class)); 
         play_pause.setText("Play"); 
        } else { 
         stopService(new Intent(Audio_Activity.this, 
           Audio_Service.class)); 
         play_pause.setText("Play"); 
        } 
       } 

      }); 

    } 

    public boolean isMyServiceRunning() { 
     ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     for (RunningServiceInfo service: manager 
      .getRunningServices(Integer.MAX_VALUE)) { 
      if (Audio_Service.class.getName().equals(
        service.service.getClassName())) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

Audio_Service.java

public class Audio_Service extends Service implements OnErrorListener, 
OnCompletionListener, OnPreparedListener { 

    public static MediaPlayer player = new MediaPlayer(); 
    Audio_Activity ac = new Audio_Activity(); 

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

    @Override 
    public void onCreate() { 

     Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onDestroy() { 
     try { 
      Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG) 
       .show(); 
     } catch (Exception e) { 

      Log.e("ONdistroy", "" + e); 
     } 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 

     try { 
      Log.e("player_status", "" + player); 
      String url = "MY_WEB_URL"; 
      player.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      player.reset(); 
      player.setDataSource(url); 
      player.prepareAsync(); 
      player.setOnPreparedListener(this); 
      player.setOnErrorListener(this); 
      player.setOnCompletionListener(this); 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("start", "" + e); 
     } 
     // player.start(); 
    } 

    public void play_pause() { 
     try { 
      if (player != null) { 
       if (player.isPlaying()) { 
        ac.play_pause.setText("Play"); 
        player.pause(); 
       } else { 
        ac.play_pause.setText("Pause"); 
        player.start(); 

       } 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("play_pause", "" + e); 
     } 
    } 

    public void stop_audio() { 
     try { 
      if (player != null) { 
       if (player.isPlaying()) { 
        ac.play_pause.setText("Play"); 
        player.stop(); 
        player.release(); 

       } else { 
        ac.play_pause.setText("Play"); 
        player.release(); 

       } 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("stop_service", "" + e); 
     } 
    } 

    public void cleanUp() { 

     if (player != null) { 
      player.stop(); 
      player = null; 
      player.release(); 
     } 

    } 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     // TODO Auto-generated method stub 
     try { 
      Log.d("complete", "called..........."); 

      player.stop(); 
      player.release(); 
      player = null; 
      stopService(new Intent(this, Audio_Service.class)); 
      ac.play_pause.setText("Play"); 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("complete", "" + e); 
     } 

    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     // TODO Auto-generated method stub 
     try { 
      player.stop(); 
      player.release(); 
      player = null; 

      // mp.reset(); 
      ac.play_pause.setText("Play"); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("onError", "" + e); 
     } 

     return false; 
    } 

    @Override 
    public void onPrepared(MediaPlayer mp) { 
     // TODO Auto-generated method stub 
     try { 
      player.start(); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("onPrepare", "" + e); 
     } 
    } 

} 

我已经添加下面的东西Manifest文件

<uses-permission android:name="android.permission.INTERNET"/> 
<service 
android:name=".Audio_Service" 
android:enabled="true" /> 

更新

我知道我可以在理想状态下播放音频的,我必须设置reset();但不知道在哪里。因为如果我设置了onDestroy;cleanUp()stop_audio()onStart()然后再增加一个错误,那就是NullpointerException

请大家帮帮我。这个错误是我最近两天得到的。 纠正我,如果我错了。

坦克需要你的时间在我的问题。

回答

3

您应该使用startService()来运行服务并使用活页夹与它进行通信。这不是一个常见的对象,你需要让Android操作系统了解它,并且你需要像预期的那样行事。

http://developer.android.com/reference/android/app/Service.html

你不应该自己创建一个实例,不应该直接调用它的方法。

+0

我有打电话给我服务'startService(新意图(Audio_Activity.this,Audio_Service.class));''中点击play_pause'事件 – 2013-05-01 09:17:18

+0

但你自己创建新的使用实例,您使用与此实例communicatate常规方法调用。这不是如何使用服务。在调用startService()之后,您需要让OS自己创建实例。您需要等待连接到srevice才能建立,并且您需要使用活页夹与它进行通信。请参阅链接查看更多细节。 – selalerer 2013-05-01 10:22:58

+0

感谢您的链接。这很好。但我解决了我的问题,我必须在'stop_audio()'中添加'player = null;'。 – 2013-05-01 10:34:41

0

尝试后停止音频播放

try { 

    player.reset(); 
    player.setDataSource(url); 
    player.prepare(); 
    player.seekTo(0); 

} catch (IllegalArgumentException e1) { 
    e1.printStackTrace(); 
} catch (SecurityException e1) { 
    e1.printStackTrace(); 
} catch (IllegalStateException e1) { 
    e1.printStackTrace(); 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 
player.start(); 
1

我得到解决后5分钟,我已经做了愚蠢犯错误是忘记加player = null;

这整个代码工作完全没有任何错误,但其中一名开发人员(selalerer)也建议我到Refer this。我也采用了这种方式,这也是完美的。

public void stop_audio() { 
    try { 
     if (player != null) { 
      if (player.isPlaying()) { 
       ac.play_pause.setText("Play"); 
       player.stop(); 
       player.release(); 
       player = null; // forgot this statement 

      } else { 
       ac.play_pause.setText("Play"); 
       player.release(); 
       player = null; // forgot this statement 

      } 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
     Log.e("stop_service", "" + e); 
    } 
}